コード例 #1
0
ファイル: OAuth2Client.cs プロジェクト: zzekikaya/OAuth2
        /// <summary>
        /// Returns URI of service which should be called in order to start authentication process.
        /// This URI should be used for rendering login link.
        /// </summary>
        /// <param name="state">
        /// Any additional information that will be posted back by service.
        /// </param>
        /// <param name="cancellationToken"></param>
        public virtual Task <string> GetLoginLinkUriAsync(string state = null, CancellationToken cancellationToken = default)
        {
            var client  = _factory.CreateClient(AccessCodeServiceEndpoint);
            var request = _factory.CreateRequest(AccessCodeServiceEndpoint);

            if (String.IsNullOrEmpty(Configuration.Scope))
            {
                request.AddObject(new
                {
                    response_type = "code",
                    client_id     = Configuration.ClientId,
                    redirect_uri  = Configuration.RedirectUri,
                    state
                });
            }
            else
            {
                request.AddObject(new
                {
                    response_type = "code",
                    client_id     = Configuration.ClientId,
                    redirect_uri  = Configuration.RedirectUri,
                    scope         = Configuration.Scope,
                    state
                });
            }
            return(Task.FromResult(client.BuildUri(request).ToString()));
        }
コード例 #2
0
        /// <summary>
        /// Issues request for request token and returns result.
        /// </summary>
        /// <returns>The task the request token gets queried on</returns>
        private async Task QueryRequestToken()
        {
            var client = _factory.CreateClient(RequestTokenServiceEndpoint);

            client.Authenticator = OAuth1Authenticator.ForRequestToken(
                Configuration.ClientId,
                Configuration.ClientSecret,
                Configuration.RedirectUri);

            var request = _factory.CreateRequest(RequestTokenServiceEndpoint, Method.POST);

            BeforeGetAccessToken(
                new BeforeAfterRequestArgs
            {
                Client        = client,
                Request       = request,
                Configuration = Configuration
            });

            var response = await client.ExecuteAndVerify(request).ConfigureAwait(false);

            AfterGetAccessToken(
                new BeforeAfterRequestArgs
            {
                Response = response,
            });

            var collection = response.Content.ParseQueryString();

            AccessToken       = collection.GetOrThrowUnexpectedResponse(_oAuthTokenKey);
            AccessTokenSecret = collection.GetOrThrowUnexpectedResponse(_oAuthTokenSecretKey);
        }
コード例 #3
0
ファイル: OAuthClient.cs プロジェクト: laripk/OAuth2
        /// <summary>
        /// Issues request for request token and returns result.
        /// </summary>
        private void QueryRequestToken()
        {
            var client = _factory.CreateClient(RequestTokenServiceEndpoint);

            client.Authenticator = OAuth1Authenticator.ForRequestToken(
                Configuration.ClientId, Configuration.ClientSecret, Configuration.RedirectUri);

            var request = _factory.CreateRequest(RequestTokenServiceEndpoint, Method.POST);

            BeforeGetAccessToken(new BeforeAfterRequestArgs
            {
                Client        = client,
                Request       = request,
                Configuration = Configuration
            });

            var response = client.ExecuteAndVerify(request);

            AfterGetAccessToken(new BeforeAfterRequestArgs
            {
                Response = response,
            });

            var collection = HttpUtility.ParseQueryString(response.Content);

            AccessToken       = collection.GetOrThrowMissingValue(OAuthTokenKey);
            AccessTokenSecret = collection.GetOrThrowMissingValue(OAuthTokenSecretKey);
        }
コード例 #4
0
        /// <summary>
        /// Returns URI of service which should be called in order to start authentication process.
        /// This URI should be used for rendering login link.
        /// </summary>
        /// <param name="state">Any additional information that will be posted back by service.</param>
        /// <returns>A string containing the login link URI</returns>
        public virtual async Task <string> GetLoginLinkUri(string state = null)
        {
            var client  = _factory.CreateClient(AccessCodeServiceEndpoint);
            var request = _factory.CreateRequest(AccessCodeServiceEndpoint);

            request.AddObject(
                new
            {
                response_type = "code",
                client_id     = Configuration.ClientId,
                redirect_uri  = Configuration.RedirectUri,
                scope         = Configuration.Scope,
                state
            },
                new[] { (string.IsNullOrEmpty(Configuration.Scope) ? "scope" : null) },
                PropertyFilterMode.Exclude);
            await BeforeGetLoginLinkUri(
                new BeforeAfterRequestArgs()
            {
                Client        = client,
                Request       = request,
                Configuration = Configuration,
            });

            return(client.BuildUri(request).ToString());
        }
コード例 #5
0
ファイル: OAuth2Client.cs プロジェクト: tngraf/OAuth2
        /// <summary>
        /// Returns URI of service which should be called in order to start authentication process.
        /// This URI should be used for rendering login link.
        /// </summary>
        /// <param name="state">
        /// Any additional information that will be posted back by service.
        /// </param>
        public virtual string GetLoginLinkUri(string state = null)
        {
            var client  = _factory.CreateClient(AccessCodeServiceEndpoint);
            var request = _factory.CreateRequest(AccessCodeServiceEndpoint);

            if (String.IsNullOrEmpty(Configuration.Scope))
            {
                request.AddObject(new
                {
                    response_type = "code",
                    client_id     = Configuration.ClientId,
                    redirect_uri  = Configuration.RedirectUri,
                    state
                });
            }
            else
            {
                request.AddObject(new
                {
                    response_type = "code",
                    client_id     = Configuration.ClientId,
                    redirect_uri  = Configuration.RedirectUri,
                    scope         = Configuration.Scope,
                    state
                });
            }
            return(client.BuildUri(request).ToString());
        }
コード例 #6
0
ファイル: OAuthClient.cs プロジェクト: weizhong1988/OAuth2
        /// <summary>
        /// Issues request for request token and returns result.
        /// </summary>
        private void QueryRequestToken()
        {
            var client = _factory.CreateClient(RequestTokenServiceEndpoint);

            client.Authenticator = OAuth1Authenticator.ForRequestToken(
                Configuration.ClientId, Configuration.ClientSecret, Configuration.RedirectUri);

            var request = _factory.CreateRequest(RequestTokenServiceEndpoint, Method.POST);

            var response   = client.ExecuteAndVerify(request);
            var collection = HttpUtility.ParseQueryString(response.Content);

            AccessToken       = collection.GetOrThrowUnexpectedResponse(OAuthTokenKey);
            AccessTokenSecret = collection.GetOrThrowUnexpectedResponse(OAuthTokenSecretKey);
        }
コード例 #7
0
 public void SetUp()
 {
     requestFactory = Substitute.For<IRequestFactory>();
     requestFactory.CreateClient().Execute(requestFactory.CreateRequest()).StatusCode = HttpStatusCode.OK;
     descendant = new FacebookClientDescendant(
         requestFactory, Substitute.For<IClientConfiguration>());
 }
コード例 #8
0
 public void SetUp()
 {
     _requestFactory = Substitute.For <IRequestFactory>();
     _requestFactory.CreateClient().Execute(_requestFactory.CreateRequest()).StatusCode = HttpStatusCode.OK;
     _descendant = new DigitalOceanClientDescendant(
         _requestFactory, Substitute.For <IClientConfiguration>());
 }
コード例 #9
0
 public void SetUp()
 {
     requestFactory = Substitute.For<IRequestFactory>();
     requestFactory.CreateClient().Execute(requestFactory.CreateRequest()).StatusCode = HttpStatusCode.OK;
     descendant = new DigitalOceanClientDescendant(
         requestFactory, Substitute.For<IClientConfiguration>());
 }
コード例 #10
0
ファイル: OAuthClientTests.cs プロジェクト: kekekeks/OAuth2
 public void SetUp()
 {
     factory = Substitute.For<IRequestFactory>();
     factory.CreateClient().Execute(factory.CreateRequest()).StatusCode = HttpStatusCode.OK;
     descendant = new OAuthClientDescendant(
         factory, Substitute.For<IClientConfiguration>());
 }
コード例 #11
0
        public static HttpClient CreateClient(this IRequestFactory factory, EndPoint endpoint, AuthenticationHeaderValue auth)
        {
            var client = factory.CreateClient(auth);

            client.BaseAddress = new Uri(endpoint.BaseUri);
            return(client);
        }
コード例 #12
0
        public static IRestClient CreateClient(this IRequestFactory factory, Endpoint endpoint)
        {
            var client = factory.CreateClient();

            client.BaseUrl = endpoint.BaseUri;
            return(client);
        }
コード例 #13
0
 public AddressSearchService(IRequestFactory requestFactory)
 {
     if (requestFactory == null)
     {
         throw new ArgumentNullException(nameof(requestFactory));
     }
     _client = requestFactory.CreateClient();
 }
コード例 #14
0
        /// <summary>
        /// Get User Profile Infor from Yahoo Api
        /// https://social.yahooapis.com/v1/user/{UserProfileGUID}/profile?format=json
        /// </summary>
        /// <returns></returns>
        protected async Task <UserInfo> GetUserInfo()
        {
            string url          = string.Format(AuthApiEndPoints.UserInfoServiceEndpoint.Resource, UserProfileGUID);
            var    tempEndPoint = new EndPoint
            {
                BaseUri  = AuthApiEndPoints.UserInfoServiceEndpoint.BaseUri,
                Resource = url
            };
            var client  = _factory.CreateClient(tempEndPoint, new AuthenticationHeaderValue("Bearer", Auth.AccessToken));
            var request = _factory.CreateRequest(tempEndPoint);

            var response = await client.GetAsync(request.RequestUri);

            var result = await response.Content.ReadAsStringAsync();

            var userInfo = JsonConvert.DeserializeObject <UserInfo>(result);

            return(userInfo);
        }
コード例 #15
0
 public void SetUp()
 {
     factory = Substitute.For<IRequestFactory>();
     var client = Substitute.For<IRestClient>();
     var request = Substitute.For<IRestRequest>();
     var response = Substitute.For<IRestResponse>();
     factory.CreateClient().Returns(client);
     factory.CreateRequest(null).ReturnsForAnyArgs(request);
     client.Execute(request).Returns(Task.FromResult(response));
     response.StatusCode.Returns(HttpStatusCode.OK);
     descendant = new VkClientDescendant(factory, Substitute.For<IClientConfiguration>());
 }
コード例 #16
0
        public void SetUp()
        {
            factory = Substitute.For <IRequestFactory>();
            var client   = Substitute.For <IRestClient>();
            var request  = Substitute.For <IRestRequest>();
            var response = Substitute.For <IRestResponse>();

            factory.CreateClient().Returns(client);
            factory.CreateRequest(null).ReturnsForAnyArgs(request);
            client.Execute(request).Returns(Task.FromResult(response));
            response.StatusCode.Returns(HttpStatusCode.OK);
            descendant = new VkClientDescendant(factory, Substitute.For <IClientConfiguration>());
        }
コード例 #17
0
        /// <summary>
        /// Returns URI of service which should be called in order to start authentication process.
        /// This URI should be used for rendering login link.
        /// </summary>
        /// <param name="state">
        /// Any additional information that will be posted back by service.
        /// </param>
        /// <param name="cancellationToken"></param>
        /// <param name="queryParameters">Additional query parameters</param>
        public virtual Task <string> GetLoginLinkUriAsync(string state = null, CancellationToken cancellationToken = default, NameValueCollection queryParameters = null)
        {
            var client  = _factory.CreateClient(AccessCodeServiceEndpoint);
            var request = _factory.CreateRequest(AccessCodeServiceEndpoint);

            if (String.IsNullOrEmpty(Configuration.Scope))
            {
                request.AddObject(new
                {
                    response_type = "code",
                    client_id     = Configuration.ClientId,
                    redirect_uri  = Configuration.RedirectUri,
                    state
                });
            }
            else
            {
                request.AddObject(new
                {
                    response_type = "code",
                    client_id     = Configuration.ClientId,
                    redirect_uri  = Configuration.RedirectUri,
                    scope         = Configuration.Scope,
                    state
                });
            }

            if (queryParameters != null)
            {
                foreach (string queryParameterKey in queryParameters.AllKeys)
                {
                    request.AddQueryParameter(queryParameterKey, queryParameters[queryParameterKey]);
                }
            }
            return(Task.FromResult(client.BuildUri(request).ToString()));
        }
コード例 #18
0
        public void SetUp()
        {
            _restRequest  = Substitute.For <IRestRequest>();
            _restResponse = Substitute.For <IRestResponse>();

            _restResponse.StatusCode.Returns(HttpStatusCode.OK);
            _restResponse.Content.Returns("response");

            _restClient = Substitute.For <IRestClient>();
            _restClient.ExecuteAsync(_restRequest, CancellationToken.None).Returns(_restResponse);

            _factory = Substitute.For <IRequestFactory>();
            _factory.CreateClient().Returns(_restClient);
            _factory.CreateRequest().Returns(_restRequest);

            _descendant = new FacebookClientDescendant(_factory, Substitute.For <IClientConfiguration>());
        }
コード例 #19
0
        public static string GetToken(HttpContext context)
        {
            mFactory = new RequestFactory();
            var request = mFactory.CreateRequest();

            request.Resource = "/oauth/authorize";
            ConfigVars.Instance.RedirectUri = string.Format("http://localhost:{0}/login/oauthcallback", context.Request.Host.Port);

            request.AddObject(new
            {
                response_type = "code",
                client_id     = ConfigVars.Instance.ClientId,
                redirect_uri  = ConfigVars.Instance.RedirectUri,
            });

            var client = mFactory.CreateClient();

            client.BaseUrl = ConfigVars.Instance.AuthServerBaseUrl;
            return(client.BuildUri(request).ToString());
        }
コード例 #20
0
ファイル: DashOAuthClient.cs プロジェクト: LazyTarget/DashAPI
        protected override UserInfo GetUserInfo()
        {
            var client = _factory.CreateClient(UserInfoServiceEndpoint);

            //client.Authenticator = new OAuth2UriQueryParameterAuthenticator(AccessToken);
            client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(AccessToken, "Bearer");
            var request = _factory.CreateRequest(UserInfoServiceEndpoint);

            BeforeGetUserInfo(new BeforeAfterRequestArgs
            {
                Client        = client,
                Request       = request,
                Configuration = Configuration
            });

            var response = client.ExecuteAndVerify(request);

            var result = ParseUserInfo(response.Content);

            result.ProviderName = Name;

            return(result);
        }
コード例 #21
0
        public void SetUp()
        {
            _restRequest  = Substitute.For <IRestRequest>();
            _restResponse = Substitute.For <IRestResponse>();

            _restResponse.StatusCode.Returns(HttpStatusCode.OK);
            _restResponse.Content.Returns("response");

            _restClient = Substitute.For <IRestClient>();
            _restClient.ExecuteTaskAsync(_restRequest, CancellationToken.None).Returns(_restResponse);

            _factory = Substitute.For <IRequestFactory>();
            _factory.CreateClient().Returns(_restClient);
            _factory.CreateRequest().Returns(_restRequest);

            var configuration = Substitute.For <IClientConfiguration>();

            configuration.ClientId.Returns("client_id");
            configuration.ClientSecret.Returns("client_secret");
            configuration.RedirectUri.Returns("http://redirect-uri.net");
            configuration.Scope.Returns("scope");

            _descendant = new OAuth2ClientDescendant(_factory, configuration);
        }
コード例 #22
0
ファイル: OAuth2ClientTests.cs プロジェクト: laripk/OAuth2
        public void SetUp()
        {
            restRequest = Substitute.For<IRestRequest>();
            restResponse = Substitute.For<IRestResponse>();

            restResponse.StatusCode.Returns(HttpStatusCode.OK);
            restResponse.Content.Returns("response");

            restClient = Substitute.For<IRestClient>();
            restClient.Execute(restRequest).Returns(restResponse);

            factory = Substitute.For<IRequestFactory>();
            factory.CreateClient().Returns(restClient);
            factory.CreateRequest().Returns(restRequest);

            var configuration = Substitute.For<IClientConfiguration>();

            configuration.ClientId.Returns("client_id");
            configuration.ClientSecret.Returns("client_secret");
            configuration.RedirectUri.Returns("http://redirect-uri.net");
            configuration.Scope.Returns("scope");

            descendant = new OAuth2ClientDescendant(factory, configuration);
        }
コード例 #23
0
        public void SetUp()
        {
            restRequest  = Substitute.For <IRestRequest>();
            restResponse = Substitute.For <IRestResponse>();

            restResponse.StatusCode.Returns(HttpStatusCode.OK);
            restResponse.RawBytes.Returns(_encoding.GetBytes("response"));

            restClient = Substitute.For <IRestClient>();
            restClient.Execute(restRequest).Returns(Task.FromResult(restResponse));

            factory = Substitute.For <IRequestFactory>();
            factory.CreateClient().Returns(restClient);
            factory.CreateRequest(null).ReturnsForAnyArgs(restRequest);

            var configuration = Substitute.For <IClientConfiguration>();

            configuration.ClientId.Returns("client_id");
            configuration.ClientSecret.Returns("client_secret");
            configuration.RedirectUri.Returns("http://redirect-uri.net");
            configuration.Scope.Returns("scope");

            descendant = new OAuth2ClientDescendant(factory, configuration);
        }
コード例 #24
0
        public static HttpClient CreateClient(this IRequestFactory factory, AuthenticationHeaderValue auth)
        {
            var client = factory.CreateClient(auth);

            return(client);
        }