internal Authorizer(AuthorizerOptions options, Func<HttpClient> createHttpClient)
 {
     if (options == null) throw new ArgumentNullException("options");
     if (createHttpClient == null) throw new ArgumentNullException("createHttpClient");
     this.options = options;
     this.createHttpClient = createHttpClient;
 }
        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);
        }
Exemple #3
0
 internal Authorizer(AuthorizerOptions options, Func <HttpClient> createHttpClient)
 {
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     if (createHttpClient == null)
     {
         throw new ArgumentNullException("createHttpClient");
     }
     this.options          = options;
     this.createHttpClient = createHttpClient;
 }
        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);
        }
        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 OAuthHttpHandlerOptions()
 {
     AuthorizerOptions = new AuthorizerOptions();
 }
 public Authorizer(AuthorizerOptions options)
     : this(options, () => new HttpClient())
 {
 }
 internal Authorizer(AuthorizerOptions options, Func<HttpClient> createHttpClient)
 {
     _options = options ?? throw new ArgumentNullException(nameof(options));
     _createHttpClient = createHttpClient ?? throw new ArgumentNullException(nameof(createHttpClient));
 }
        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"));
        }
 public Authorizer(AuthorizerOptions options)
     : this(options, () => new HttpClient())
 {
 }