コード例 #1
0
        public async Task RefreshToken(int refreshBeforeInSeconds, int expiryTimeInSeconds, int expectedStsCallCount, string expectedToken)
        {
            var config = new OidcAuthenticationConfig
            {
                ClientId               = "bob",
                ClientSecret           = "bobsecret",
                Scopes                 = new[] { "bob.scope.all" },
                Uri                    = "http://localhost/authendpoint",
                RefreshBeforeInSeconds = refreshBeforeInSeconds
            };

            var mockHttp    = new MockHttpMessageHandler();
            var mockRequest = mockHttp.When(HttpMethod.Post, config.Uri)
                              .WithFormData("client_id", config.ClientId)
                              .WithFormData("client_secret", config.ClientSecret)
                              .Respond(HttpStatusCode.OK, "application/json",
                                       JsonConvert.SerializeObject(new OidcAuthenticationToken
            {
                AccessToken = expectedToken,
                ExpiresIn   = expiryTimeInSeconds
            }));

            var handler = new OidcAuthenticationHandler(
                new HttpClientFactory(
                    new Dictionary <string, HttpClient>
            {
                { new Uri(config.Uri).Host, mockHttp.ToHttpClient() },
            }),
                config,
                _bigBrother);

            await handler.GetTokenAsync(_cancellationToken);

            await Task.Delay(TimeSpan.FromSeconds(1), _cancellationToken);

            var token = await handler.GetTokenAsync(_cancellationToken);

            Assert.Equal(expectedStsCallCount, mockHttp.GetMatchCount(mockRequest));
            Assert.Equal($"Bearer {expectedToken}", token);
        }
コード例 #2
0
        public async Task AuthorisationTokenSuccess(string expectedAccessToken)
        {
            var config = new OidcAuthenticationConfig
            {
                ClientId     = "bob",
                ClientSecret = "bobsecret",
                Scopes       = new[] { "bob.scope.all" },
                Uri          = "http://localhost/authendpoint"
            };

            var mockHttp    = new MockHttpMessageHandler(BackendDefinitionBehavior.Always);
            var mockRequest = mockHttp.When(HttpMethod.Post, config.Uri)
                              .WithFormData("client_id", config.ClientId)
                              .WithFormData("client_secret", config.ClientSecret)
                              .Respond(HttpStatusCode.OK, "application/json",
                                       JsonConvert.SerializeObject(new OidcAuthenticationToken
            {
                AccessToken = "6015CF7142BA060F5026BE9CC442C12ED7F0D5AECCBAA0678DEEBC51C6A1B282"
            }));
            var httpClient = mockHttp.ToHttpClient();

            var handler = new OidcAuthenticationHandler(
                new HttpClientFactory(
                    new Dictionary <string, HttpClient>
            {
                { new Uri(config.Uri).Host, httpClient },
            }),
                config,
                _bigBrother);

            var token = await handler.GetTokenAsync(_cancellationToken);

            Assert.Equal(1, mockHttp.GetMatchCount(mockRequest));
            Assert.Null(httpClient.DefaultRequestHeaders.Authorization);
            Assert.Equal($"Bearer {expectedAccessToken}", token);
        }