Security icon indicating copy to clipboard operation
Security copied to clipboard

OIDC handler bug in user info response handling for multiple claims of same type

Open brockallen opened this issue 10 years ago • 11 comments

Imagine user info endpoint returns this:

{"sub":"88421113","email":"[email protected]","email_verified":true,"role":["role1","role2"]}

This line: https://github.com/aspnet/Security/blob/dev/src/Microsoft.AspNetCore.Authentication.OpenIdConnect/OpenIdConnectHandler.cs#L788

Adds the role claim with this as the value: [ "role1", "role2" ].

It should break the array into multiple claims. Here's an extension method that already does this: https://github.com/IdentityModel/IdentityModel2/blob/dev/src/IdentityModel/Client/JObjectExtensions.cs#L12

brockallen avatar Sep 17 '16 15:09 brockallen

Any comment? This makes the userinfo feature pretty much unusable...

leastprivilege avatar Sep 21 '16 06:09 leastprivilege

Triage is tomorrow, we'll take a look.

Tratcher avatar Sep 21 '16 19:09 Tratcher

@PinpointTownes thoughts?

Assigning to @troydai for further investigation.

muratg avatar Sep 22 '16 22:09 muratg

@muratg IMHO, it's clearly worth fixing this bug (it also impacted a bunch of ASOS/OpenIddict users and my recommendation was to use the identity token to flow "array" claims instead of using the userinfo endpoint, since Wilson fully supports that).

Note: @brockallen's approach is simple and fine, but it doesn't preserve the claim value type. It's not critical but it's something to remember before reusing his snippet as-is. You might want to take a look at how it's implemented in Wilson, instead: https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/master/src/System.IdentityModel.Tokens.Jwt/JwtPayload.cs#L307

kevinchalet avatar Sep 22 '16 22:09 kevinchalet

@brockallen do you know an existing authentication server and client application can return claims in this way? It is helpful for me to build a repo quickly. Otherwise, I'll try to build a similar example or mock an auth server.

troydai avatar Sep 23 '16 05:09 troydai

@PinpointTownes You cannot solely use the id_token to flow claims because spec:

5.4. Requesting Claims using Scope Values The Claims requested by the profile, email, address, and phone scope values are returned from the UserInfo Endpoint, as described in Section 5.3.2, when a response_type value is used that results in an Access Token being issued. However, when no Access Token is issued (which is the case for the response_type value id_token), the resulting Claims are returned in the ID Token.

leastprivilege avatar Sep 23 '16 06:09 leastprivilege

@troydai IdentityServer does

here's the host: https://github.com/IdentityServer/IdentityServer4

and here's a client using hybrid flow and userinfo https://github.com/IdentityServer/IdentityServer4.Samples/tree/dev/Clients/src/MvcHybrid

leastprivilege avatar Sep 23 '16 06:09 leastprivilege

@leastprivilege thanks.

troydai avatar Sep 23 '16 18:09 troydai

As of today i am still getting this issue. Is there some workaround or approach changed. Using IdentityServer4 with AspNetCore 2.0.

From: OnUserInformationReceived event in IdentityServer4 options.Events = new OpenIdConnectEvents() { OnUserInformationReceived = context => { //context.User return Task.FromResult(0); } };

Value of context.User: {{ "given_name": "User", "family_name": "FName", "address": "delhi", "role": [ "localadmin", "globaladmin" ], "sub": "d5f1a5b2-f50b-4884-a1cb-ecc3d610d242" }}

ERROR: InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JToken.

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandlerd__12.MoveNext()

InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JToken.

Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler+d__12.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) System.Runtime.CompilerServices.TaskAwaiter.GetResult() Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+d__6.MoveNext() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware+d__7.MoveNext()

@leastprivilege @brockallen @troydai Any words ?

yadavvineet avatar Nov 19 '17 10:11 yadavvineet

I found another workaround for this using a custom ClaimAction derived class suggested by @Tratcher in another thread. But i do believe that a system claim action should be provided in base asslembly to support this type of operation.

yadavvineet avatar Nov 19 '17 11:11 yadavvineet

@Yadavvineet can you share this aproach for I have the same issue

stamo avatar Jan 29 '18 17:01 stamo