/*
         * Use this operation to update the access token if it has expired
         */
        public OAuthResponse GetAccessToken(OAuthEnvironment environment, String refreshToken, IList <String> scopes)
        {
            //Validate request
            ValidateEnvironmentAndScopes(environment, scopes);
            ValidateInput("RefreshToken", refreshToken);

            //Get credentials
            CredentialUtil.Credentials credentials = GetCredentials(environment);

            //Format scopes
            String formattedScopes = OAuth2Util.FormatScopesForRequest(scopes);

            // Create request payload
            Dictionary <String, String> payloadParams = new Dictionary <string, string>
            {
                { Constants.PAYLOAD_GRANT_TYPE, Constants.PAYLOAD_VALUE_REFRESH_TOKEN },
                { Constants.PAYLOAD_REFRESH_TOKEN, refreshToken },
                { Constants.PAYLOAD_SCOPE, formattedScopes }
            };
            String requestPayload = OAuth2Util.CreateRequestPayload(payloadParams);

            OAuthResponse oAuthResponse = FetchToken(environment, requestPayload, TokenType.USER);

            return(oAuthResponse);
        }
        private OAuthResponse FetchToken(OAuthEnvironment environment, String requestPayload, TokenType tokenType)
        {
            //Get credentials
            CredentialUtil.Credentials credentials = GetCredentials(environment);

            //Initialize client
            RestClient client = new RestClient
            {
                BaseUrl = new Uri(environment.ApiEndpoint())
            };

            //Create request
            RestRequest request = new RestRequest(Method.POST);

            //Add headers
            request.AddHeader(Constants.HEADER_AUTHORIZATION, OAuth2Util.CreateAuthorizationHeader(credentials));

            //Set request payload
            request.AddParameter(Constants.HEADER_CONTENT_TYPE, requestPayload, ParameterType.RequestBody);


            //Call the API
            IRestResponse response = client.Execute(request);

            //Parse response
            OAuthResponse oAuthResponse = HandleApiResponse(response, tokenType);

            return(oAuthResponse);
        }
        /*
         * Use this operation to get the Authorization URL to redirect the user to.
         * Once the user authenticates and approves the consent, the callback need to be
         * captured by the redirect URL setup by the app
         */
        public String GenerateUserAuthorizationUrl(OAuthEnvironment environment, IList <String> scopes, String state)
        {
            //Validate request
            ValidateEnvironmentAndScopes(environment, scopes);

            //Get credentials
            CredentialUtil.Credentials credentials = GetCredentials(environment);

            //Format scopes
            String formattedScopes = OAuth2Util.FormatScopesForRequest(scopes);

            //Prepare URL
            StringBuilder sb = new StringBuilder();

            sb.Append(environment.WebEndpoint()).Append("?");

            //Prepare request payload
            Dictionary <String, String> queryParams = new Dictionary <string, string>
            {
                { Constants.PAYLOAD_CLIENT_ID, credentials.Get(CredentialType.APP_ID) },
                { Constants.PAYLOAD_RESPONSE_TYPE, Constants.PAYLOAD_VALUE_CODE },
                { Constants.PAYLOAD_REDIRECT_URI, credentials.Get(CredentialType.REDIRECT_URI) },
                { Constants.PAYLOAD_SCOPE, formattedScopes }
            };

            if (state != null)
            {
                queryParams.Add(Constants.PAYLOAD_STATE, state);
            }

            sb.Append(OAuth2Util.CreateRequestPayload(queryParams));

            log.Debug("Authorization url " + sb);
            return(sb.ToString());
        }
Example #4
0
        public void FormatScopesForRequest_Success()
        {
            IList <String> scopes = new List <String>()
            {
                "https://api.ebay.com/oauth/api_scope/buy.marketing",
                "https://api.ebay.com/oauth/api_scope"
            };

            String formattedScopes = OAuth2Util.FormatScopesForRequest(scopes);

            Assert.Equal("https://api.ebay.com/oauth/api_scope/buy.marketing+https://api.ebay.com/oauth/api_scope", formattedScopes);
        }
Example #5
0
        public void CreateRequestPayload_Success()
        {
            Dictionary <String, String> payloadParams = new Dictionary <string, string>
            {
                { Constants.PAYLOAD_GRANT_TYPE, Constants.PAYLOAD_VALUE_AUTHORIZATION_CODE },
                { Constants.PAYLOAD_REDIRECT_URI, "TestURI" },
                { Constants.PAYLOAD_CODE, "TestCode" }
            };
            String requestPayload = OAuth2Util.CreateRequestPayload(payloadParams);

            Assert.NotNull(requestPayload);
            Assert.Equal("grant_type=authorization_code&redirect_uri=TestURI&code=TestCode", requestPayload);
        }
Example #6
0
        public void CreateAuthorizationHeader_Success()
        {
            String path = @"../../../ebay-config-sample.yaml";

            CredentialUtil.Load(path);
            CredentialUtil.Credentials credentials = CredentialUtil.GetCredentials(OAuthEnvironment.PRODUCTION);
            String authorizationHeader             = OAuth2Util.CreateAuthorizationHeader(credentials);

            Assert.NotNull(authorizationHeader);
            Boolean headerStartsWithBasic = authorizationHeader.StartsWith("Basic ", StringComparison.Ordinal);

            Assert.True(headerStartsWithBasic);
        }
        /*
         * Use this operation to update the access token if it has expired
         */
        public OAuthResponse GetAccessToken(OAuthEnvironment environment, String refreshToken, IList <String> scopes)
        {
            //Validate request
            ValidateEnvironmentAndScopes(environment, scopes);
            ValidateInput("RefreshToken", refreshToken);

            //Get credentials
            CredentialUtil.Credentials credentials = GetCredentials(environment);

            //Format scopes
            String formattedScopes = OAuth2Util.FormatScopesForRequest(scopes);

            // Create request payload
            Dictionary <String, String> payloadParams = new Dictionary <string, string>
            {
                { Constants.PAYLOAD_GRANT_TYPE, Constants.PAYLOAD_VALUE_REFRESH_TOKEN },
                { Constants.PAYLOAD_REFRESH_TOKEN, refreshToken },
                { Constants.PAYLOAD_SCOPE, formattedScopes }
            };
            String requestPayload = OAuth2Util.CreateRequestPayload(payloadParams);

            OAuthResponse oAuthResponse;

            //Check for token in cache
            oAuthResponse = accessTokenCache.GetValue(environment);
            if (oAuthResponse != null && oAuthResponse.AccessToken != null && oAuthResponse.AccessToken.Token != null)
            {
                log.Info("Returning access token from cache for " + environment.ConfigIdentifier());
                return(oAuthResponse);
            }

            oAuthResponse = FetchToken(environment, requestPayload, TokenType.USER);

            if (oAuthResponse != null && oAuthResponse.AccessToken != null)
            {
                accessTokenCache.UpdateValue(environment, oAuthResponse, oAuthResponse.AccessToken.ExpiresOn);
            }

            return(oAuthResponse);
        }
        /*
         * Use this operation to get the refresh and access tokens.
         */
        public OAuthResponse ExchangeCodeForAccessToken(OAuthEnvironment environment, String code)
        {
            //Validate request
            ValidateInput("Environment", environment);
            ValidateInput("Code", code);

            //Get credentials
            CredentialUtil.Credentials credentials = GetCredentials(environment);

            // Create request payload
            Dictionary <String, String> payloadParams = new Dictionary <string, string>
            {
                { Constants.PAYLOAD_GRANT_TYPE, Constants.PAYLOAD_VALUE_AUTHORIZATION_CODE },
                { Constants.PAYLOAD_REDIRECT_URI, credentials.Get(CredentialType.REDIRECT_URI) },
                { Constants.PAYLOAD_CODE, code }
            };
            String requestPayload = OAuth2Util.CreateRequestPayload(payloadParams);

            OAuthResponse oAuthResponse = FetchToken(environment, requestPayload, TokenType.USER);

            return(oAuthResponse);
        }
        /*
         * Use this operation to get an OAuth access token using a client credentials grant.
         * The access token retrieved from this process is called an Application access token.
         */
        public OAuthResponse GetApplicationToken(OAuthEnvironment environment, IList <String> scopes)
        {
            //Validate request
            ValidateEnvironmentAndScopes(environment, scopes);
            OAuthResponse oAuthResponse = null;

            //Check for token in cache
            if (appTokenCache != null)
            {
                oAuthResponse = appTokenCache.GetValue(environment);
                if (oAuthResponse != null && oAuthResponse.AccessToken != null && oAuthResponse.AccessToken.Token != null)
                {
                    log.Info("Returning token from cache for " + environment.ConfigIdentifier());
                    return(oAuthResponse);
                }
            }

            //App token not in cache, fetch it and set into cache
            String formattedScopes = OAuth2Util.FormatScopesForRequest(scopes);

            //Prepare request payload
            Dictionary <String, String> payloadParams = new Dictionary <string, string>
            {
                { Constants.PAYLOAD_GRANT_TYPE, Constants.PAYLOAD_VALUE_CLIENT_CREDENTIALS },
                { Constants.PAYLOAD_SCOPE, formattedScopes }
            };
            String requestPayload = OAuth2Util.CreateRequestPayload(payloadParams);

            oAuthResponse = FetchToken(environment, requestPayload, TokenType.APPLICATION);
            //Update value in cache
            if (oAuthResponse != null && oAuthResponse.AccessToken != null)
            {
                appTokenCache.UpdateValue(environment, oAuthResponse, oAuthResponse.AccessToken.ExpiresOn);
            }

            return(oAuthResponse);
        }
Example #10
0
        public void FormatScopesForRequest_NullScopes()
        {
            String formattedScopes = OAuth2Util.FormatScopesForRequest(null);

            Assert.Null(formattedScopes);
        }