Esempio n. 1
0
        private ITokenManager GetTestTokenManager(
            Token token     = null,
            string userId   = null,
            string clientId = null,
            IEnumerable <string> grantedTokens = null,
            IEnumerable <string> grantedScopes = null)
        {
            userId        = userId ?? "userId";
            clientId      = clientId ?? "clientId";
            grantedTokens = grantedTokens ?? Enumerable.Empty <string>();
            var parsedScopes = grantedScopes?.Select(s => ApplicationScope.CanonicalScopes.TryGetValue(s, out var found) ? found : new ApplicationScope(clientId, s)) ??
                               new[] { ApplicationScope.OpenId, ApplicationScope.OfflineAccess };

            var mock = new Mock <ITokenManager>();

            if (token == null)
            {
                mock.Setup(m => m.ExchangeTokenAsync(It.IsAny <OpenIdConnectMessage>()))
                .ReturnsAsync(AuthorizationGrant.Invalid(ProtocolErrorProvider.InvalidGrant()));
            }
            else
            {
                mock.Setup(m => m.ExchangeTokenAsync(It.IsAny <OpenIdConnectMessage>()))
                .ReturnsAsync(AuthorizationGrant.Valid(
                                  userId,
                                  clientId,
                                  grantedTokens,
                                  parsedScopes,
                                  token));
            }

            return(mock.Object);
        }
Esempio n. 2
0
        public async Task <AuthorizationGrant> ExchangeTokenAsync(OpenIdConnectMessage message)
        {
            switch (message.GrantType)
            {
            case OpenIdConnectGrantTypes.AuthorizationCode:
                return(await _codeIssuer.ExchangeAuthorizationCodeAsync(message));

            case OpenIdConnectGrantTypes.RefreshToken:
                return(await _refreshTokenIssuer.ExchangeRefreshTokenAsync(message));

            default:
                return(AuthorizationGrant.Invalid(_errorProvider.InvalidGrantType(message.GrantType)));
            }
        }
        public Task <AuthorizationGrant> ExchangeAuthorizationCodeAsync(OpenIdConnectMessage message)
        {
            var code = _dataFormat.Unprotect(message.Code);

            if (code == null)
            {
                return(Task.FromResult(AuthorizationGrant.Invalid(_errorProvider.InvalidAuthorizationCode())));
            }

            var userId   = code.UserId;
            var clientId = code.ClientId;
            var scopes   = code.Scopes;
            var resource = code.Resource;
            var nonce    = code.Nonce;

            var tokenTypes    = code.GrantedTokens;
            var grantedScopes = scopes.SelectMany(s => s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
                                .Select(s => ApplicationScope.CanonicalScopes.TryGetValue(s, out var canonicalScope) ? canonicalScope : new ApplicationScope(resource, s))
                                .ToList();

            return(Task.FromResult(AuthorizationGrant.Valid(userId, clientId, tokenTypes, grantedScopes, code)));
        }