Beispiel #1
0
 internal static HttpRequestMessage BuildRequestTokenHttpRequestMessage(this OAuth2HttpClient httpClient)
 {
     return(new HttpRequestMessage(HttpMethod.Post, httpClient.BuildTokenUri())
     {
         Content = httpClient.BuildFormUrlEncodedContent()
     });
 }
Beispiel #2
0
        public OAuth2HttpClientTests(OAuth2Fixture fixture)
        {
            var services = fixture.BuildServiceProvider();

            _mockHttp         = services.GetService <MockHttpMessageHandler>();
            _client           = services.GetRequiredService <OAuth2HttpClient>();
            _options          = services.GetRequiredService <IOptions <AuthorizerOptions> >().Value;
            _resourceEndpoint = fixture.Configuration.GetValue <Uri>("OAuth2:ResourceEndpoint");
        }
Beispiel #3
0
        internal static OAuth2HttpClientHandler GetHandler(this OAuth2HttpClient httpClient)
        {
            const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;

            var httpClientBaseType = typeof(HttpMessageInvoker);
            var field = httpClientBaseType.GetField("handler", flags) ?? httpClientBaseType.GetField("_handler", flags);

            return((OAuth2HttpClientHandler)field?.GetValue(httpClient));
        }
        public OAuth2HttpClientTests(OAuth2Fixture fixture)
        {
            if (fixture.Configuration.GetValue("HttpClient:Mock", true))
            {
                _mockHttp = new MockHttpMessageHandler();
            }
            var services = fixture.BuildOAuth2HttpClient(_mockHttp);

            _client           = services.GetRequiredService <OAuth2HttpClient>();
            _options          = services.GetRequiredService <IOptions <AuthorizerOptions> >().Value;
            _resourceEndpoint = fixture.Configuration.GetValue <Uri>("OAuth2:ResourceEndpoint");
        }
        public OAuthNetHttpUnitTest()
        {
            _httpClient = new OAuth2HttpClient
            {
                ClientId     = "client-id",
                ClientSecret = "dGhpcy1pcy1hLWNsaWVudC1zZWNyZXQ=",
                Username     = "******",
                Password     = "******",
                GrantType    = "password"
            };

            _httpClient.Scope.Add("scope1");
            _httpClient.Scope.Add("scope2");
        }
Beispiel #6
0
        internal static Uri BuildTokenUri(this OAuth2HttpClient httpClient)
        {
            var tempTokenUrl = (httpClient.TokenUrl ?? "/token");

            if (!tempTokenUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase) && httpClient.BaseAddress == default)
            {
                throw new OperationCanceledException(
                          "OAuth2HttpClient property BaseAddress must be provides when TokenUrl is null or empty");
            }

            return(tempTokenUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase)
                ? new Uri(tempTokenUrl)
                : new Uri(httpClient.BaseAddress, tempTokenUrl));
        }
Beispiel #7
0
        internal static FormUrlEncodedContent BuildFormUrlEncodedContent(this OAuth2HttpClient httpClient)
        {
            var payload = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("grant_type", httpClient.GrantType),
                new KeyValuePair <string, string>("client_id", httpClient.ClientId),
                new KeyValuePair <string, string>("client_secret", httpClient.ClientSecret),
                new KeyValuePair <string, string>("scope", string.Join(" ", httpClient.Scope)),
            };

            if (httpClient.HasCredentials)
            {
                payload.Add(new KeyValuePair <string, string>("username", httpClient.Username));
                payload.Add(new KeyValuePair <string, string>("password", httpClient.Password));
            }

            return(new FormUrlEncodedContent(payload));
        }