Exemplo n.º 1
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 handler = new OidcAuthenticationHandler(config);

            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();

            await handler.GetToken(httpClient);

            Assert.Equal(1, mockHttp.GetMatchCount(mockRequest));
            Assert.NotNull(httpClient.DefaultRequestHeaders.Authorization);
            Assert.Equal(expectedAccessToken, httpClient.DefaultRequestHeaders.Authorization.Parameter);
            Assert.Equal("Bearer", httpClient.DefaultRequestHeaders.Authorization.Scheme);
        }
Exemplo n.º 2
0
        public async Task RefreshToken(int refreshBeforeInSeconds, int expiryTimeInSeconds, int expectedStsCallCount)
        {
            var config = new OidcAuthenticationConfig
            {
                ClientId               = "bob",
                ClientSecret           = "bobsecret",
                Scopes                 = new[] { "bob.scope.all" },
                Uri                    = "http://localhost/authendpoint",
                RefreshBeforeInSeconds = refreshBeforeInSeconds
            };

            var handler = new OidcAuthenticationHandler(config);

            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 = "6015CF7142BA060F5026BE9CC442C12ED7F0D5AECCBAA0678DEEBC51C6A1B282",
                ExpiresIn   = expiryTimeInSeconds
            }));

            var httpClient = mockHttp.ToHttpClient();

            await handler.GetToken(httpClient);

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

            await handler.GetToken(httpClient);

            Assert.Equal(expectedStsCallCount, mockHttp.GetMatchCount(mockRequest));
        }
Exemplo n.º 3
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);
        }