public async Task GetToken_InvalidClientCredentialsWithOnErrorCallback_OnErrorGetsCalled()
        {
            HttpStatusCode errorStatusCode = HttpStatusCode.Unused;
            string errorMessage = string.Empty;

            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri("http://localhost/authorize"),
                TokenEndpointUrl = new Uri("http://localhost/token"),
                ClientId = "WrongId",
                ClientSecret = "WrongSecret",
                GrantType = GrantType.ClientCredentials,
                OnError = (statusCode, message) =>
                {
                    errorStatusCode = statusCode;
                    errorMessage = message;
                }
            };

            var authorizer = new Authorizer.Authorizer(options, () => server.HttpClient);

            await authorizer.GetToken();

            Assert.IsTrue(errorMessage.Contains("invalid_client"));
            Assert.AreEqual(HttpStatusCode.BadRequest, errorStatusCode);
        }
        public async Task GetToken_ValidClientCredentials_ReturnsValidAccessToken()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri("http://localhost/authorize"),
                TokenEndpointUrl = new Uri("http://localhost/token"),
                ClientId = "MyId",
                ClientSecret = "MySecret",
                GrantType = GrantType.ClientCredentials
            };

            var authorizer = new Authorizer.Authorizer(options, () => server.HttpClient);
            var result = await authorizer.GetToken();
            Assert.NotNull(result.AccessToken);
        }
Example #3
0
        public async Task GetToken_ValidClientCredentials_FormsAuthentication_ReturnsValidAccessToken()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl      = new Uri(_server.BaseAddress, "/connect/authorize"),
                TokenEndpointUrl          = new Uri(_server.BaseAddress, "/connect/token"),
                ClientId                  = "MyId",
                ClientSecret              = "MySecret",
                GrantType                 = GrantType.ClientCredentials,
                CredentialTransportMethod = CredentialTransportMethod.FormAuthenticationCredentials
            };

            var authorizer = new Authorizer.Authorizer(options, () => _httpClient);
            var result     = await authorizer.GetToken();

            Assert.NotNull(result.AccessToken);
        }
Example #4
0
        public void GetToken_ClientCredentialsWithScope_ShouldRequestScope()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri("http://localhost/authorize"),
                TokenEndpointUrl     = new Uri("http://localhost/token"),
                ClientId             = "MyId",
                ClientSecret         = "MySecret",
                GrantType            = GrantType.ClientCredentials,
                Scope = new[] { "testscope" }
            };

            var authorizer = new Authorizer.Authorizer(options, () => server.HttpClient);
            var ex         = Assert.Throws <ProtocolException>(async() => await authorizer.GetToken());

            Assert.IsTrue(ex.Message.Contains("testscope_ok"));
        }
        public void GetToken_InvalidClientCredentialsWithoutOnErrorCallback_ThrowsProtocolException()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri("http://localhost/authorize"),
                TokenEndpointUrl     = new Uri("http://localhost/token"),
                ClientId             = "WrongId",
                ClientSecret         = "WrongSecret",
                GrantType            = GrantType.ClientCredentials
            };

            var authorizer = new Authorizer.Authorizer(options, () => server.HttpClient);

            var ex = Assert.Throws <ProtocolException>(async() => await authorizer.GetToken());

            Assert.IsTrue(ex.Message.Contains("invalid_client"));
            Assert.AreEqual(HttpStatusCode.BadRequest, ex.StatusCode);
        }
        public async Task GetToken_ValidPasswordCredentials_ReturnsValidAccessToken()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri("http://localhost/authorize"),
                TokenEndpointUrl     = new Uri("http://localhost/token"),
                ClientId             = "MyId",
                ClientSecret         = "MySecret",
                Username             = "******",
                Password             = "******",
                GrantType            = GrantType.ResourceOwnerPasswordCredentials
            };

            var authorizer = new Authorizer.Authorizer(options, () => server.HttpClient);
            var result     = await authorizer.GetToken();

            Assert.NotNull(result.AccessToken);
        }
        public void GetToken_InvalidClientCredentialsWithoutOnErrorCallback_ThrowsProtocolException()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri("http://localhost/authorize"),
                TokenEndpointUrl = new Uri("http://localhost/token"),
                ClientId = "WrongId",
                ClientSecret = "WrongSecret",
                GrantType = GrantType.ClientCredentials
            };

            var authorizer = new Authorizer.Authorizer(options, () => server.HttpClient);
            
            var ex = Assert.Throws<ProtocolException>(async () => await authorizer.GetToken());
            
            Assert.IsTrue(ex.Message.Contains("invalid_client"));
            Assert.AreEqual(HttpStatusCode.BadRequest, ex.StatusCode);
        }
Example #8
0
        public async Task GetToken_ClientCredentialsWithScope_ShouldRequestScope()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri(_server.BaseAddress, "/connect/authorize"),
                TokenEndpointUrl     = new Uri(_server.BaseAddress, "/connect/token"),
                ClientId             = "MyId",
                ClientSecret         = "MySecret",
                GrantType            = GrantType.ClientCredentials,
                Scope = new[] { "test" }
            };

            var authorizer = new Authorizer.Authorizer(options, () => _httpClient);
            var token      = new JwtSecurityToken((await authorizer.GetToken()).AccessToken);
            var scope      = token.Claims.FirstOrDefault(x => x.Type == "scope");

            Assert.That(scope, Is.Not.Null);
            Assert.That(scope.Value, Is.EqualTo("test"));
        }
Example #9
0
        public void GetToken_InvalidPasswordCredentialsWithoutOnErrorCallback_ThrowsProtocolException()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri(_server.BaseAddress, "/connect/authorize"),
                TokenEndpointUrl     = new Uri(_server.BaseAddress, "/connect/token"),
                ClientId             = "MyId",
                ClientSecret         = "MySecret",
                Username             = "******",
                Password             = "******",
                GrantType            = GrantType.ResourceOwnerPasswordCredentials
            };

            var authorizer = new Authorizer.Authorizer(options, () => _httpClient);

            var ex = Assert.ThrowsAsync <ProtocolException>(async() => await authorizer.GetToken());

            Assert.IsTrue(ex.Message.Contains("invalid_grant"));
            Assert.AreEqual(HttpStatusCode.BadRequest, ex.StatusCode);
        }
        public void GetToken_InvalidTokenEndpointUrl_ThrowsProtocolException()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri("http://localhost/authorize"),
                TokenEndpointUrl = new Uri("http://localhost/invalid"),
                ClientId = "MyId",
                ClientSecret = "MySecret",
                GrantType = GrantType.ClientCredentials
            };

            var authorizer = new Authorizer.Authorizer(options, () => server.HttpClient);
            var ex = Assert.Throws<ProtocolException>(async () => await authorizer.GetToken());
            Assert.AreEqual(HttpStatusCode.NotFound, ex.StatusCode);
        }
        public void GetToken_PasswordCredentialsWithScope_ShouldRequestScope()
        {
            var options = new AuthorizerOptions
            {
                AuthorizeEndpointUrl = new Uri("http://localhost/authorize"),
                TokenEndpointUrl = new Uri("http://localhost/token"),
                ClientId = "MyId",
                ClientSecret = "MySecret",
                Username = "******",
                Password = "******",
                GrantType = GrantType.ResourceOwnerPasswordCredentials,
                Scope = new[] { "othertestscope" }
            };

            var authorizer = new Authorizer.Authorizer(options, () => server.HttpClient);
            var ex = Assert.Throws<ProtocolException>(async () => await authorizer.GetToken());
            Assert.IsTrue(ex.Message.Contains("othertestscope_ok"));
        }