Ejemplo n.º 1
0
        static async Task <ManagedToken> ExecuteClientCredentialsRequestAsync(
            ManagedToken managedToken,
            IServiceProvider serviceProvider,
            IOAuth2CredentialManager oAuth2CredentialManager,
            CancellationToken cancellationToken = default)
        {
            var creds = await oAuth2CredentialManager.GetOAuth2CredentialsAsync(managedToken.CredentialsKey);

            var client   = new HttpClient();
            var response = await client.RequestClientCredentialsTokenAsync(
                new ClientCredentialsTokenRequest
            {
                Address      = creds.DiscoveryDocumentResponse.TokenEndpoint,
                ClientId     = creds.ClientId,
                ClientSecret = creds.ClientSecret,
                Scope        = managedToken.RequestedScope
            },
                cancellationToken);

            if (response.IsError)
            {
                throw new Exception(response.Error);
            }
            managedToken.RefreshToken = response.RefreshToken;
            managedToken.AccessToken  = response.AccessToken;
            managedToken.ExpiresIn    = response.ExpiresIn;
            return(managedToken);
        }
Ejemplo n.º 2
0
        public async Task OnGetAsync()
        {
            Creds = await _oAuth2CredentialManager.GetOAuth2CredentialsAsync("test");

            if (Creds == null)
            {
                await _oAuth2CredentialManager.AddCredentialsAsync("test", new OAuth2Credentials
                {
                    Authority    = "https://demo.identityserver.io",
                    ClientId     = "m2m",
                    ClientSecret = "secret"
                });

                // this call will do all the discovery work if it hasn't been done yet.
                Creds = await _oAuth2CredentialManager.GetOAuth2CredentialsAsync("test");
            }
            ManagedToken = await _globalTokenManager.GetManagedTokenAsync("test", true);
        }
Ejemplo n.º 3
0
        public async Task <ManagedToken> GetManagedTokenAsync(string key, bool forceRefresh = false, CancellationToken cancellationToken = default)
        {
            LockReleaser releaser = await _lock.Lock(new TimeSpan(0, 0, 30));

            try
            {
                var managedToken = await GetUnSafeManagedTokenAsync(key);

                if (managedToken == null)
                {
                    return(null);
                }
                if (string.IsNullOrEmpty(managedToken.RefreshToken))
                {
                    if (forceRefresh)
                    {
                        var func = _customTokenRequest.GetTokenRequestFunc(managedToken.RequestFunctionKey);
                        if (func == null)
                        {
                            throw new Exception($"forceRefresh requested, but no token request function exists. RequestFunctionKey={managedToken.RequestFunctionKey}");
                        }
                        var mT = await func(managedToken, _serviceProvider, _oAuth2CredentialManager, cancellationToken);

                        if (mT == null)
                        {
                            throw new Exception($"Custom Token Request function return a null. RequestFunctionKey={managedToken.RequestFunctionKey}");
                        }
                        managedToken = await AddUnSafeManagedTokenAsync(key, mT);
                    }
                    return(managedToken);
                }

                DateTimeOffset now = DateTimeOffset.UtcNow;
                if (!forceRefresh)
                {
                    if (managedToken.ExpirationDate > now || string.IsNullOrWhiteSpace(managedToken.RefreshToken))
                    {
                        return(managedToken);
                    }
                }
                var creds = await _oAuth2CredentialManager.GetOAuth2CredentialsAsync(managedToken.CredentialsKey);

                if (creds == null)
                {
                    throw new Exception($"GetOAuth2CredentialsAsync failed: key={managedToken.CredentialsKey}");
                }
                var client   = new HttpClient();
                var response = await client.RequestRefreshTokenAsync(new RefreshTokenRequest
                {
                    Address      = creds.DiscoveryDocumentResponse.TokenEndpoint,
                    ClientId     = creds.ClientId,
                    ClientSecret = creds.ClientSecret,
                    RefreshToken = managedToken.RefreshToken
                });

                if (response.IsError)
                {
                    // REMOVE the managed token, because the refresh failed
                    await _tokenStorage.RemoveManagedTokensAsync();

                    throw new Exception($"RequestRefreshTokenAsync failed for key={key}",
                                        new Exception(response.Error));
                }
                managedToken.RefreshToken = response.RefreshToken;
                managedToken.AccessToken  = response.AccessToken;
                managedToken.ExpiresIn    = response.ExpiresIn;
                managedToken = await AddUnSafeManagedTokenAsync(key, managedToken);

                return(managedToken);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(null);
            }
            finally
            {
                releaser.Dispose();
            }
        }
Ejemplo n.º 4
0
        public async Task OnGetAsync()
        {
            Creds = await _oAuth2CredentialManager.GetOAuth2CredentialsAsync("test");

            ManagedToken = await _globalTokenManager.GetManagedTokenAsync("test", true);
        }