IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

[Question] Best practices for Restricting Scopes by User

Open adamholdenyall opened this issue 10 years ago • 4 comments

EDIT: I found an old question in the IdentityServer3 issues that made it clear I was just confused on the role of scopes.

https://github.com/IdentityServer/IdentityServer3/issues/1856

The OAuth Spec talks about how you can filter scopes on access token requests. For example, I ask for "openid", "api1", "api2" and the server determines I only have access to "openid" and "api1" and returns a token only containing those scopes and their associated claims.

I have added a simple class to the example project and injected it into an extension of the DefaultTokenService:

    public class SubjectToScopeRepo
    {
        public IEnumerable<string> GetScopesFor(string subject)
        {
            var globallyAllowedScopes = new List<string>
            {
                StandardScopes.OpenId.Name,
                StandardScopes.Email.Name,
                StandardScopes.OfflineAccess.Name,
            };
            List<string> perUserScopes = new List<string>();
            switch (subject)
            {
                case "818727"://alice
                    perUserScopes = new List<string>
                    {
                        "api1",
                        "api2",
                    };
                    break;
                case "88421113"://bob
                    perUserScopes = new List<string>
                    {
                        "api2",
                        "api3"
                    };
                    break;
            }
            return globallyAllowedScopes.Union(perUserScopes);
        }
    }
    public class MyTokenService : DefaultTokenService
    {
        private readonly SubjectToScopeRepo _subjectToScopeRegistry;

        public MyTokenService(IdentityServerContext context, IClaimsProvider claimsProvider, ITokenHandleStore tokenHandles, ITokenSigningService signingService, IEventService events, ILoggerFactory loggerFactory, SubjectToScopeRepo subjectToScopeRegistry) : base(context, claimsProvider, tokenHandles, signingService, events, loggerFactory)
        {
            _subjectToScopeRegistry = subjectToScopeRegistry;
        }

        public override Task<Token> CreateAccessTokenAsync(TokenCreationRequest request)
        {
            var allowedScopes = _subjectToScopeRegistry.GetScopesFor(request.Subject.GetSubjectId());
            request.Scopes = request.Scopes.Where(s => allowedScopes.Contains(s.Name));
            return base.CreateAccessTokenAsync(request);
        }
    }

As you can see, if both Bob and Alice were to request a token with the "api1","api2", and "api3" scopes, Alice would get a token that only contained 1 and 2, Bob would only get 2 and 3.

Is this an appropriate practice? Is there a more specialized mechanism to implement this in IdentityServer4? The spec itself is pretty narrow on prescriptions, so I'm wondering if it's just the case that IdentityServer is as well.

adamholdenyall avatar Jun 24 '16 15:06 adamholdenyall

Hi, why was this closed? did you get your answer? I am playing the quickstarts, and I would like to restrict API1 for Alice, and API2 for Bob

benaichouchem avatar Feb 02 '17 11:02 benaichouchem

Authorization is not a concern of identity server - check the subject ID in your APIs to find out who the user is and if it has access to the current API.

https://leastprivilege.com/2016/12/16/identity-vs-permissions/

leastprivilege avatar Feb 02 '17 12:02 leastprivilege

@adamholdenyall can you share full example in which you have done "injected it into an extension of the DefaultTokenService".

sw-ms-pradipbaria avatar Apr 24 '19 13:04 sw-ms-pradipbaria

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