IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Question: Why access_token is not updated with new scopes during refresh token request?

Open Iamcerba opened this issue 8 years ago • 4 comments

Hi Guys,

I have the following setup (for IdentityServer4 v1.5.2):

// Existing API resource
new ApiResource() 
{
    Enabled = true,
    Name = "ExistingService",
    DisplayName = "ExistingService",
    Description = "Existing service API",
    ApiSecrets = new List<Secret>()
    {
        new Secret("secret".Sha256())
    },
    Scopes = new List<Scope>()
    {
        new Scope()
        {
            Name = "existingsrv",
            DisplayName = "ExistingService",
            Description = "Grants an access to the existing service"
        }
    },
    UserClaims = new List<string>()
    {
        JwtClaimTypes.Subject,
        JwtClaimTypes.Name,
        JwtClaimTypes.Role,
        "permission"
    }
}

// Client
new Client
{
    Enabled = true,
    ClientId = "ios.client",
    ClientName = "iOS application",
    AllowedGrantTypes = new List<string>()
    {
        "password",
        "activation_code"
    },
    ClientSecrets =
    {
        new Secret("secret".Sha256())
    },
    RequireClientSecret = true,
    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,
        IdentityServerConstants.StandardScopes.Phone,
        "existingsrv"
    },
    AlwaysSendClientClaims = true,
    AlwaysIncludeUserClaimsInIdToken = true,
    AccessTokenType = AccessTokenType.Reference,
    AccessTokenLifetime = 3600,
    AllowOfflineAccess = true,
    RefreshTokenExpiration = TokenExpiration.Sliding,
    RefreshTokenUsage = TokenUsage.OneTimeOnly,
    AbsoluteRefreshTokenLifetime = 2592000,
    SlidingRefreshTokenLifetime = 1296000,
    UpdateAccessTokenClaimsOnRefresh = true
}

Use case:

  1. I would like to add new ApiResource:
// New API resource
new ApiResource() 
{
    Enabled = true,
    Name = "NewService",
    DisplayName = "NewService",
    Description = "New service API",
    ApiSecrets = new List<Secret>()
    {
        new Secret("secret".Sha256())
    },
    Scopes = new List<Scope>()
    {
        new Scope()
        {
            Name = "newsrv",
            DisplayName = "NewService",
            Description = "Grants an access to the new service"
        }
    },
    UserClaims = new List<string>()
    {
        JwtClaimTypes.Subject,
        JwtClaimTypes.Name,
        JwtClaimTypes.Role,
        "permission"
    }
}
  1. Then I update existing client with new scope:
// Existing client
new Client
{
    ClientId = "ios.client",
   ...
    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile,
        IdentityServerConstants.StandardScopes.Email,
        IdentityServerConstants.StandardScopes.Phone,
        "existingsrv",
        "newsrv" // New scope
    },
    ...
}

The question is:

All existing reference tokens are not updated with new scopes during refresh token request (Please find info about updated reference token from Data column in PersistedGrants table):

{
	"Audiences": ["https://identityserve.azurewebsites.net/resources", "ExistingService"],
	"Issuer": "https://identityserver.azurewebsites.net",
	"CreationTime": "2018-02-08T07:20:23.5750403Z",
	"Lifetime": 3600,
	"Type": "access_token",
	"ClientId": "ios.client",
	"AccessTokenType": 1,
	"Claims": [{
		"Type": "client_id",
		"Value": "ios.client",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "scope",
		"Value": "email",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "scope",
		"Value": "openid",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "scope",
		"Value": "phone",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "scope",
		"Value": "profile",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "scope",
		"Value": "existingsrv",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "scope",
		"Value": "offline_access",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "sub",
		"Value": "760db5c0-0993-e711-80c2-000d3a223498",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "auth_time",
		"Value": "1518074423",
		"ValueType": "http://www.w3.org/2001/XMLSchema#integer"
	}, {
		"Type": "idp",
		"Value": "local",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "amr",
		"Value": "activation_code",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "role",
		"Value": "Member",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "name",
		"Value": "[email protected]",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "given_name",
		"Value": "Test",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}, {
		"Type": "family_name",
		"Value": "Test",
		"ValueType": "http://www.w3.org/2001/XMLSchema#string"
	}],
	"Version": 4
}

As a result I have to re login to have an access to new ApiResource. Is it correct?

I have checked the code and seems like only old scopes are taken into account during refresh: https://github.com/IdentityServer/IdentityServer4/blob/becb2206826b1039687b3ec895fad5e1b26ef0c5/src/IdentityServer4/ResponseHandling/TokenResponseGenerator.cs#L204

In addition I'm seeing the following messages in logs for all users which were authenticated before adding new ApiResource:

  1. Failed token introspection: "API not authorized to introspect token", for API name: "NewService"
  2. Expected scope ["newsrv"] is missing in token

Iamcerba avatar Feb 08 '18 08:02 Iamcerba

Refresh tokens can't be used to gain access to scopes beyond those of the original grant. That's covered somewhere in the spec.... I don't recall where.

brockallen avatar Feb 11 '18 16:02 brockallen

I understood, thanks. Looks like you are talking about RFC 6749:

"The requested scope MUST NOT include any scope not originally granted by the resource owner, and if omitted is treated as equal to the scope originally granted by the resource owner."

Iamcerba avatar Feb 11 '18 17:02 Iamcerba

@Iamcerba So when the server is updated with new resource, how can client keep the login state to avoid invalid token scope? Is it recommend to customize the refresh token flow?

goxiaoy avatar Dec 27 '19 03:12 goxiaoy

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 26 '20 04:01 lock[bot]