Example #1
0
        public void GetTokenByCodeRequest_WithTokenNull_ThrowsArgumentNullException()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new OAuth2RequestBuilder();

            requestBuilder.GetTokenByCodeRequest("url", null, "clientId", "clientSecret");
        }
Example #2
0
        public void GetTokenByPinRequest_WithEndpointUrlNull_ThrowsArgumentNullException()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new OAuth2RequestBuilder();

            requestBuilder.GetTokenByPinRequest(null, "123", "clientId", "clientSecret");
        }
Example #3
0
        public void GetTokenByRefreshTokenRequest_WithClientSecretNull_ThrowsArgumentNullException()
        {
            var client         = new ImgurClient("123", "1234");
            var requestBuilder = new OAuth2RequestBuilder();

            requestBuilder.GetTokenByRefreshTokenRequest("url", "ABChjfhjhjdhfjksdfsdfsdfs", "clientId", null);
        }
        public void GetTokenByCodeRequest_WithTokenNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new OAuth2RequestBuilder();
            var exception      =
                Record.Exception(() => OAuth2RequestBuilder.GetTokenByCodeRequest("url", null, "clientId", "clientSecret"));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal(argNullException.ParamName, "code");
        }
Example #5
0
        /// <summary>
        ///     If a user has authorized their account but you no longer have a valid access_token for them,
        ///     then a new one can be generated by using the refreshToken.
        ///     <para>
        ///         When your application receives a refresh token, it is important to store
        ///         that refresh token for future use.
        ///     </para>
        ///     <para>
        ///         If your application loses the refresh token, you will have to prompt the user
        ///         for their login information again.
        ///     </para>
        /// </summary>
        /// <param name="refreshToken">The refresh token.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when a null reference is passed to a method that does not accept it as a
        ///     valid argument.
        /// </exception>
        /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
        /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
        /// <returns></returns>
        public async Task <IOAuth2Token> GetTokenByRefreshTokenInternalAsync(string refreshToken)
        {
            IOAuth2Token token;

            using (
                var request = OAuth2RequestBuilder.GetTokenByRefreshTokenRequest(TokenEndpointUrl, refreshToken,
                                                                                 ApiClient.ClientId, ApiClient.ClientSecret))
            {
                token = await SendRequestAsync <OAuth2Token>(request).ConfigureAwait(false);
            }

            return(token);
        }
Example #6
0
        /// <summary>
        ///     After the user authorizes, the pin is returned as a code to your application
        ///     via the redirect URL you specified during registration, in the form of a regular query string parameter.
        /// </summary>
        /// <param name="code">The code from the query string.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when a null reference is passed to a method that does not accept it as a
        ///     valid argument.
        /// </exception>
        /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
        /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
        /// <returns></returns>
        private async Task <IOAuth2Token> GetTokenByCodeInternalAsync(string code)
        {
            IOAuth2Token token;

            using (
                var request = OAuth2RequestBuilder.GetTokenByCodeRequest(TokenEndpointUrl, code, ApiClient.ClientId,
                                                                         ApiClient.ClientSecret))
            {
                token = await SendRequestAsync <OAuth2Token>(request).ConfigureAwait(false);
            }

            return(token);
        }
        public async Task GetTokenByRefreshTokenRequest_WithRefreshTokenEqual()
        {
            var request = OAuth2RequestBuilder.GetTokenRequest("https://api.imgur.com/oauth2/token",
                                                               "ABChjfhjhjdhfjksdfsdfsdfs", "123", "1234");

            Assert.NotNull(request);
            Assert.Equal("https://api.imgur.com/oauth2/token", request.RequestUri.ToString());
            Assert.Equal(HttpMethod.Post, request.Method);
            Assert.NotNull(request.Content);

            var expected = await request.Content.ReadAsStringAsync();

            Assert.Equal(
                "client_id=123&client_secret=1234&grant_type=refresh_token&refresh_token=ABChjfhjhjdhfjksdfsdfsdfs",
                expected);
        }
        public async Task GetTokenByCodeRequest_WithCodeEqual()
        {
            var requestBuilder = new OAuth2RequestBuilder();

            var request = OAuth2RequestBuilder.GetTokenByCodeRequest("https://api.imgur.com/oauth2/token", "123code", "123",
                                                                     "1234");

            Assert.NotNull(request);
            Assert.Equal("https://api.imgur.com/oauth2/token", request.RequestUri.ToString());
            Assert.Equal(HttpMethod.Post, request.Method);
            Assert.NotNull(request.Content);

            var expected = await request.Content.ReadAsStringAsync().ConfigureAwait(false);

            Assert.Equal("client_id=123&client_secret=1234&grant_type=authorization_code&code=123code", expected);
        }
        public void GetTokenByRefreshTokenRequest_WithEndpointUrlNull_ThrowsArgumentNullException()
        {
            var exception =
                Record.Exception(() =>
                                 OAuth2RequestBuilder.GetTokenRequest(null,
                                                                      "ABChjfhjhjdhfjksdfsdfsdfs",
                                                                      "clientId",
                                                                      "clientSecret"));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal("url", argNullException.ParamName);
        }
Example #10
0
        public async Task GetTokenByPinRequest_WithPinAreEqual()
        {
            var requestBuilder = new OAuth2RequestBuilder();

            var request = requestBuilder.GetTokenByPinRequest("https://api.imgur.com/oauth2/token", "4899", "123",
                                                              "1234");

            Assert.IsNotNull(request);
            Assert.AreEqual("https://api.imgur.com/oauth2/token", request.RequestUri.ToString());
            Assert.AreEqual(HttpMethod.Post, request.Method);
            Assert.IsNotNull(request.Content);

            var expected = await request.Content.ReadAsStringAsync();

            Assert.AreEqual("client_id=123&client_secret=1234&grant_type=pin&pin=4899", expected);
        }
        public void GetTokenByCodeRequest_WithClientSecretNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new OAuth2RequestBuilder();

            var exception =
                Record.Exception(
                    () =>
                    OAuth2RequestBuilder.GetTokenByCodeRequest("https://api.imgur.com/oauth2/token", "123code", "clientId",
                                                               null));

            Assert.NotNull(exception);
            Assert.IsType <ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException)exception;

            Assert.Equal(argNullException.ParamName, "clientSecret");
        }
Example #12
0
        internal async Task <IOAuth2Token> GetTokenInternalAsync(string refreshToken,
                                                                 CancellationToken cancellationToken = default)
        {
            using (var request = OAuth2RequestBuilder.GetTokenRequest(TokenEndpointUrl,
                                                                      refreshToken,
                                                                      _apiClient.ClientId,
                                                                      _apiClient.ClientSecret))
            {
                var httpResponse = await _httpClient.SendAsync(request, cancellationToken)
                                   .ConfigureAwait(false);

                httpResponse.EnsureSuccessStatusCode();

                var response = await httpResponse.Content.ReadAsStringAsync()
                               .ConfigureAwait(false);

                return(_responseConverter.ConvertOAuth2TokenResponse(response));
            }
        }
Example #13
0
        /// <summary>
        ///     After the user authorizes, the pin is returned as a code to your application
        ///     via the redirect URL you specified during registration, in the form of a regular query string parameter.
        /// </summary>
        /// <param name="code">The code from the query string.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when a null reference is passed to a method that does not accept it as a
        ///     valid argument.
        /// </exception>
        /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
        /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
        /// <returns></returns>
        public async Task <IOAuth2Token> GetTokenByCodeAsync(string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                throw new ArgumentNullException(nameof(code));
            }

            if (string.IsNullOrWhiteSpace(ApiClient.ClientSecret))
            {
                throw new ArgumentNullException(nameof(ApiClient.ClientSecret));
            }

            IOAuth2Token token;

            using (
                var request = OAuth2RequestBuilder.GetTokenByCodeRequest(TokenEndpointUrl, code, ApiClient.ClientId,
                                                                         ApiClient.ClientSecret))
            {
                token = await SendRequestAsync <OAuth2Token>(request).ConfigureAwait(false);
            }

            return(token);
        }
Example #14
0
        /// <summary>
        ///     If a user has authorized their account but you no longer have a valid access_token for them,
        ///     then a new one can be generated by using the refreshToken.
        ///     <para>
        ///         When your application receives a refresh token, it is important to store
        ///         that refresh token for future use.
        ///     </para>
        ///     <para>
        ///         If your application loses the refresh token, you will have to prompt the user
        ///         for their login information again.
        ///     </para>
        /// </summary>
        /// <param name="refreshToken">The refresh token.</param>
        /// <exception cref="ArgumentNullException">
        ///     Thrown when a null reference is passed to a method that does not accept it as a
        ///     valid argument.
        /// </exception>
        /// <exception cref="ImgurException">Thrown when an error is found in a response from an Imgur endpoint.</exception>
        /// <exception cref="MashapeException">Thrown when an error is found in a response from a Mashape endpoint.</exception>
        /// <returns></returns>
        public IOAuth2Token GetTokenByRefreshTokenAsync(string refreshToken)
        {
            if (string.IsNullOrWhiteSpace(refreshToken))
            {
                throw new ArgumentNullException(nameof(refreshToken));
            }

            if (string.IsNullOrWhiteSpace(ApiClient.ClientSecret))
            {
                throw new ArgumentNullException(nameof(ApiClient.ClientSecret));
            }

            IOAuth2Token token;

            using (
                var request = OAuth2RequestBuilder.GetTokenByRefreshTokenRequest(TokenEndpointUrl, refreshToken,
                                                                                 ApiClient.ClientId, ApiClient.ClientSecret))
            {
                token = SendRequestAsync <OAuth2Token>(request).Data;
            }

            return(token);
        }
Example #15
0
        public void GetTokenByCodeRequest_WithClientIdNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new OAuth2RequestBuilder();

            requestBuilder.GetTokenByCodeRequest("url", "123code", null, "secret");
        }
Example #16
0
        public void GetTokenByRefreshTokenRequest_WithEndpointUrlNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new OAuth2RequestBuilder();

            requestBuilder.GetTokenByRefreshTokenRequest(null, "ABChjfhjhjdhfjksdfsdfsdfs", "clientId", "clientSecret");
        }
Example #17
0
        public void GetTokenByCodeRequest_WithClientSecretNull_ThrowsArgumentNullException()
        {
            var requestBuilder = new OAuth2RequestBuilder();

            requestBuilder.GetTokenByCodeRequest("https://api.imgur.com/oauth2/token", "123code", "clientId", null);
        }