IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

UpdateSecurityStampAsync with aspnetIdentity - Endpoints

Open jmatheti opened this issue 9 years ago • 8 comments

  • [x] I read and understood how to enable logging

Issue / Steps to reproduce the problem

When using aspnetidentity integrated with idmsrv, on updatesecuritystampasync the user's security stamp is being changed however idm srv is not verifying the user changes and kept the endpoints renewing/reissuing the access token ( authorize & tokenvalidation end points)

on singning in update the security stamp var test= _userManager.UpdateSecurityStampAsync(_userManager.FindByEmailAsync(model.Email).Result).Result;

client configured to be implicit grant

browser 1 - login

browser 2 - login (using the same user account as above)

expected to take the user to login page on Client 1 if I try to login again.

on everytime login the system is asked to updated the user security stamp to invalidate the previous login cookies. the change can be seen on the aspnetuser's table.

however , Profile Service IsActiveAsync ( at the moment checking only for user active or not) I believe this should also consider to make sure there is no change to the user.

Relevant parts of the log file

browser 1 login
2017-09-04 16:09:36.787 +10:00 [Information] Profile service returned to the following claim types: "sub AspNet.Identity.SecurityStamp preferred_username name email email_verified Ip"
2017-09-04 16:09:36.788 +10:00 [Debug] End userinfo request
browser 2 login
2017-09-04 16:11:58.602 +10:00 [Information] Profile service returned to the following claim types: "sub AspNet.Identity.SecurityStamp preferred_username name email email_verified Ip"

browser 1 login again ( requesting auth server to issue a new token by contacting connect/authorize following connect/token

2017-09-04 16:13:53.378 +10:00 [Debug] Request path "/connect/authorize" matched to endpoint type Authorize
2017-09-04 16:13:53.379 +10:00 [Debug] Mapping found for endpoint: Authorize, creating handler: "IdentityServer4.Endpoints.AuthorizeEndpoint"
2017-09-04 16:13:53.380 +10:00 [Information] Invoking IdentityServer endpoint: "IdentityServer4.Endpoints.AuthorizeEndpoint" for "/connect/authorize"
2017-09-04 16:13:53.381 +10:00 [Debug] Start authorize request
2017-09-04 16:13:53.382 +10:00 [Debug] User in authorize request: "b419e1e4-0099-49b1-be66-a06fa5165daf"
2017-09-04 16:13:53.382 +10:00 [Debug] Start authorize request protocol validation
2017-09-04 16:13:53.401 +10:00 [Debug] "JsClient1" found in database: True
2017-09-04 16:13:53.407 +10:00 [Debug] Found "openid, profile" identity scopes in database
2017-09-04 16:13:53.418 +10:00 [Debug] Found "testapi1" API scopes in database
2017-09-04 16:13:53.424 +10:00 [Debug] Found "openid, profile" identity scopes in database
2017-09-04 16:13:53.435 +10:00 [Debug] Found "testapi1" API scopes in database
2017-09-04 16:13:53.436 +10:00 [Debug] Calling into custom validator: "IdentityServer4.Validation.DefaultCustomAuthorizeRequestValidator"
2017-09-04 16:13:53.438 +10:00 [Information] ValidatedAuthorizeRequest
"{
  \"ClientId\": \"JsClient1\",
  \"ClientName\": \"JavaScript Client1\",
  \"RedirectUri\": \"http://localhost:5006/callback.html\",
  \"AllowedRedirectUris\": [
    \"http://localhost:5006/callback.html\"
  ],
  \"SubjectId\": \"b419e1e4-0099-49b1-be66-a06fa5165daf\",
  \"ResponseType\": \"id_token token\",
  \"ResponseMode\": \"fragment\",
  \"GrantType\": \"implicit\",
  \"RequestedScopes\": \"openid profile testapi1\",
  \"State\": \"3fba16c985db49d08dbb76c8ef54a03d\",
  \"Nonce\": \"b6f473bee01148289700223eb18bb979\",
  \"SessionId\": \"a53ebc9bd132c7c2a066e88a4b5273f0\",
  \"Raw\": {
    \"client_id\": \"JsClient1\",
    \"redirect_uri\": \"http://localhost:5006/callback.html\",
    \"response_type\": \"id_token token\",
    \"scope\": \"openid profile testapi1\",
    \"state\": \"3fba16c985db49d08dbb76c8ef54a03d\",
    \"nonce\": \"b6f473bee01148289700223eb18bb979\"
  }
}"
2017-09-04 16:13:53.447 +10:00 [Debug] "CitcCFPMqxSC6QgCxgK1n4PNX2p5xKRzHGvVzhiyk14=" found in database: True
2017-09-04 16:13:53.451 +10:00 [Debug] Creating Implicit Flow response.
2017-09-04 16:13:53.451 +10:00 [Debug] Getting claims for access token for client: "JsClient1"
2017-09-04 16:13:53.451 +10:00 [Debug] Getting claims for access token for subject: "b419e1e4-0099-49b1-be66-a06fa5165daf"
2017-09-04 16:13:53.457 +10:00 [Debug] Claim types from profile service that were filtered: "sub"
2017-09-04 16:13:53.459 +10:00 [Debug] Getting claims for identity token for subject: "b419e1e4-0099-49b1-be66-a06fa5165daf" and client: "JsClient1"
2017-09-04 16:13:53.464 +10:00 [Debug] Claim types from profile service that were filtered: "sub"
2017-09-04 16:13:53.467 +10:00 [Information] Authorize endpoint response
"{
  \"SubjectId\": \"b419e1e4-0099-49b1-be66-a06fa5165daf\",
  \"ClientId\": \"JsClient1\",
  \"RedirectUri\": \"http://localhost:5006/callback.html\",
  \"State\": \"3fba16c985db49d08dbb76c8ef54a03d\",
  \"Scope\": \"openid profile testapi1\"
}"
2017-09-04 16:13:53.626 +10:00 [Debug] CORS request made for path: "/.well-known/openid-configuration" from origin: "http://localhost:5006"
2017-09-04 16:13:53.629 +10:00 [Debug] Origin "http://localhost:5006" is allowed: True
2017-09-04 16:13:53.629 +10:00 [Debug] CorsPolicyService allowed origin: "http://localhost:5006"
2017-09-04 16:13:53.629 +10:00 [Debug] Request path "/.well-known/openid-configuration" matched to endpoint type Discovery
2017-09-04 16:13:53.629 +10:00 [Debug] Mapping found for endpoint: Discovery, creating handler: "IdentityServer4.Endpoints.DiscoveryEndpoint"
2017-09-04 16:13:53.631 +10:00 [Information] Invoking IdentityServer endpoint: "IdentityServer4.Endpoints.DiscoveryEndpoint" for "/.well-known/openid-configuration"
2017-09-04 16:13:53.631 +10:00 [Debug] Start discovery request
2017-09-04 16:13:53.653 +10:00 [Debug] Found "openid, profile, api1, testapi1, testapi2" as all scopes in database
2017-09-04 16:13:53.693 +10:00 [Debug] CORS request made for path: "/.well-known/openid-configuration/jwks" from origin: "http://localhost:5006"
2017-09-04 16:13:53.695 +10:00 [Debug] Origin "http://localhost:5006" is allowed: True
2017-09-04 16:13:53.696 +10:00 [Debug] CorsPolicyService allowed origin: "http://localhost:5006"
2017-09-04 16:13:53.696 +10:00 [Debug] Request path "/.well-known/openid-configuration/jwks" matched to endpoint type Discovery
2017-09-04 16:13:53.696 +10:00 [Debug] Mapping found for endpoint: Discovery, creating handler: "IdentityServer4.Endpoints.DiscoveryEndpoint"
2017-09-04 16:13:53.698 +10:00 [Information] Invoking IdentityServer endpoint: "IdentityServer4.Endpoints.DiscoveryEndpoint" for "/.well-known/openid-configuration/jwks"
2017-09-04 16:13:53.698 +10:00 [Debug] Start key discovery request
2017-09-04 16:13:53.727 +10:00 [Debug] CORS request made for path: "/connect/userinfo" from origin: "http://localhost:5006"
2017-09-04 16:13:53.729 +10:00 [Debug] Origin "http://localhost:5006" is allowed: True
2017-09-04 16:13:53.729 +10:00 [Debug] CorsPolicyService allowed origin: "http://localhost:5006"
2017-09-04 16:13:53.736 +10:00 [Debug] CORS request made for path: "/connect/userinfo" from origin: "http://localhost:5006"
2017-09-04 16:13:53.738 +10:00 [Debug] Origin "http://localhost:5006" is allowed: True
2017-09-04 16:13:53.738 +10:00 [Debug] CorsPolicyService allowed origin: "http://localhost:5006"
2017-09-04 16:13:53.739 +10:00 [Debug] Request path "/connect/userinfo" matched to endpoint type UserInfo
2017-09-04 16:13:53.739 +10:00 [Debug] Mapping found for endpoint: UserInfo, creating handler: "IdentityServer4.Endpoints.UserInfoEndpoint"
2017-09-04 16:13:53.740 +10:00 [Information] Invoking IdentityServer endpoint: "IdentityServer4.Endpoints.UserInfoEndpoint" for "/connect/userinfo"
2017-09-04 16:13:53.741 +10:00 [Debug] Start userinfo request
2017-09-04 16:13:53.743 +10:00 [Information] ValidateAsync: Locating bearer token
2017-09-04 16:13:53.743 +10:00 [Debug] Bearer token found in header
2017-09-04 16:13:53.743 +10:00 [Debug] Token found: "AuthorizationHeader"
2017-09-04 16:13:53.784 +10:00 [Debug] "JsClient1" found in database: True
2017-09-04 16:13:53.804 +10:00 [Debug] "JsClient1" found in database: True
2017-09-04 16:13:53.804 +10:00 [Debug] Token validation success
"{
  \"ValidateLifetime\": true,
  \"AccessTokenType\": \"Jwt\",
  \"ExpectedScope\": \"openid\",
  \"Claims\": {
    \"nbf\": 1504505633,
    \"exp\": 1504505663,
    \"iss\": \"http://localhost:5000\",
    \"aud\": [
      \"http://localhost:5000/resources\",
      \"testapi1\"
    ],
    \"client_id\": \"JsClient1\",
    \"sub\": \"b419e1e4-0099-49b1-be66-a06fa5165daf\",
    \"auth_time\": 1504505366,
    \"idp\": \"local\",
    \"AspNet.Identity.SecurityStamp\": \"c1c3979f-b99e-4f38-bdac-a566d59a9c81\",
    \"preferred_username\": \"[email protected]\",
    \"name\": \"[email protected]\",
    \"email\": \"[email protected]\",
    \"email_verified\": false,
    \"Ip\": \"::1\",
    \"scope\": [
      \"openid\",
      \"profile\",
      \"testapi1\"
    ],
    \"amr\": \"pwd\"
  }
}"
2017-09-04 16:13:53.804 +10:00 [Debug] Scopes in access token: "openid profile testapi1"
2017-09-04 16:13:53.808 +10:00 [Debug] Found "openid, profile" identity scopes in database
2017-09-04 16:13:53.809 +10:00 [Debug] Requested claim types: "sub name family_name given_name middle_name nickname preferred_username profile picture website gender birthdate zoneinfo locale updated_at"
2017-09-04 16:13:53.814 +10:00 [Information] Profile service returned to the following claim types: "sub AspNet.Identity.SecurityStamp preferred_username name email email_verified Ip"
2017-09-04 16:13:53.815 +10:00 [Debug] End userinfo request
2017-09-04 16:13:53.885 +10:00 [Debug] Request path "/connect/checksession" matched to endpoint type CheckSession
2017-09-04 16:13:53.885 +10:00 [Debug] Mapping found for endpoint: CheckSession, creating handler: "IdentityServer4.Endpoints.CheckSessionEndpoint"
2017-09-04 16:13:53.886 +10:00 [Information] Invoking IdentityServer endpoint: "IdentityServer4.Endpoints.CheckSessionEndpoint" for "/connect/checksession"
2017-09-04 16:13:53.887 +10:00 [Debug] Rendering check session result
2017-09-04 16:13:53.950 +10:00 [Debug] CORS request made for path: "/.well-known/openid-configuration" from origin: "http://localhost:5006"
2017-09-04 16:13:53.954 +10:00 [Debug] Origin "http://localhost:5006" is allowed: True
2017-09-04 16:13:53.954 +10:00 [Debug] CorsPolicyService allowed origin: "http://localhost:5006"
2017-09-04 16:13:53.954 +10:00 [Debug] Request path "/.well-known/openid-configuration" matched to endpoint type Discovery
2017-09-04 16:13:53.954 +10:00 [Debug] Mapping found for endpoint: Discovery, creating handler: "IdentityServer4.Endpoints.DiscoveryEndpoint"
2017-09-04 16:13:53.957 +10:00 [Information] Invoking IdentityServer endpoint: "IdentityServer4.Endpoints.DiscoveryEndpoint" for "/.well-known/openid-configuration"
2017-09-04 16:13:53.957 +10:00 [Debug] Start discovery request
2017-09-04 16:13:53.990 +10:00 [Debug] Found "openid, profile, api1, testapi1, testapi2" as all scopes in database
2017-09-04 16:13:54.015 +10:00 [Debug] Request path "/connect/checksession" matched to endpoint type CheckSession
2017-09-04 16:13:54.015 +10:00 [Debug] Mapping found for endpoint: CheckSession, creating handler: "IdentityServer4.Endpoints.CheckSessionEndpoint"
2017-09-04 16:13:54.017 +10:00 [Information] Invoking IdentityServer endpoint: "IdentityServer4.Endpoints.CheckSessionEndpoint" for "/connect/checksession"
2017-09-04 16:13:54.017 +10:00 [Debug] Rendering check session result

jmatheti avatar Sep 04 '17 06:09 jmatheti

You should open this in the ASp.NET Identity repo: https://github.com/IdentityServer/IdentityServer4.AspNetIdentity/

brockallen avatar Sep 04 '17 11:09 brockallen

Sorry brock maybe it wasn't clear.

The end points in the identityserver authorize & token should be considering the security change in the user.

with the current design, the user gets authorized though there is a security change on user, so token creation as well.

may it should be something like this ( just come across this).

https://github.com/tibold/IdentityServer4.Contrib.AspNetIdentity/blob/master/src/IdentityServer4.Contrib.AspNetIdentity/ProfileService.cs

but the one in the idm src (https://github.com/IdentityServer/IdentityServer4/blob/dev/src/IdentityServer4/Services/DefaultProfileService.cs) is missing an important security consideration.

Thanks for the wonderful library.

jmatheti avatar Sep 04 '17 23:09 jmatheti

The end points in the identityserver authorize & token should be considering the security change in the user.

Yes, IsActive in the profile service is for you to decide if the user should no longer be allowed to get tokens. The ASP.NET Identity implementation is here: https://github.com/IdentityServer/IdentityServer4.AspNetIdentity/blob/dev/src/IdentityServer4.AspNetIdentity/ProfileService.cs

brockallen avatar Sep 05 '17 00:09 brockallen

Also, in the GetProfileDataAsync(ProfileDataRequestContext context), I think it should be,

context.FilterClaims(principal.Claims);

Here is what I update for my testing, https://github.com/rizamarhaban/IdentityServer4Demo/blob/master/Ids4AspNetIdentity/ProfileService.cs

rizamarhaban avatar Sep 05 '17 00:09 rizamarhaban

another problem to deal - IsActiveContext model is not most updated claims. This may not be related to idmsrv. I suspect aspnetIdentity and IUserClaimsPrincipalFactory is not strongly coupled. UpdateSecurityStamp updates in database but not OWIN claims. I mean after the signin the authorize end point claims are outdated in this case.

jmatheti avatar Sep 05 '17 03:09 jmatheti

In any event, this thread should continue on the ASP.NET Identity repo -- the security stamp is not an IdSvr feature.

brockallen avatar Sep 05 '17 12:09 brockallen

Thanks allen.

For those who come here with the similar issue as a temporary, refresh the cookie after the signin will update the security stamp in the pipeline.

_signInManager.RefreshSignInAsync(user);

jmatheti avatar Sep 06 '17 04:09 jmatheti

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 14 '20 08:01 lock[bot]