예제 #1
0
        internal async Task <OAuthResponse> GetTokenFromCode(string code)
        {
            if (string.IsNullOrEmpty(Definition.TokenUrl))
            {
                return(OAuthResponse.WithError("BadImplementation",
                                               "Provider returns code in authorize request but there is not access token URL."));
            }

            return(await GetToken(Definition.TokenUrl,
                                  Definition.TokenAuthorizationHeaders,
                                  BuildTokenRequestFields(code)));
        }
예제 #2
0
        internal virtual OAuthResponse GetTokenResponse(string response)
        {
            switch (Definition.TokenResponseSerialization)
            {
            case TokenResponseSerialization.JSON:
                return(ReadOAuthResponseFromJson(response));

            case TokenResponseSerialization.UrlEncoded:
                return(ReadOAuthResponseFromUrl("http://abc.com?" + response));

            default:
                return(OAuthResponse.WithError("TokenResponseError", "Unknow serialization."));
            }
        }
예제 #3
0
        internal async Task <OAuthResponse> RefreshToken(OAuthAccessToken token)
        {
            if (!Definition.RefreshesToken)
            {
                return(OAuthResponse.WithError("NotSupported",
                                               "Provider does not support token refresh."));
            }

            return(await GetToken(Definition.TokenUrl,
                                  Definition.RefreshTokenAuthorizationHeaders,
                                  BuildRefreshTokenRequestFields(
                                      string.IsNullOrEmpty(token.RefreshToken) ?
                                      token.Token
                    :
                                      token.RefreshToken)));
        }
예제 #4
0
        private OAuthResponse ReadOAuthResponseFromJson(string json)
        {
            var jObject = JObject.Parse(json);

            var error = jObject.GetStringValue(_errorParameter);

            if (!string.IsNullOrEmpty(error))
            {
                return(OAuthResponse.WithError(error, jObject.GetStringValue(_errorDescriptionParameter)));
            }

            return(OAuthResponse.WithToken(new OAuthAccessToken(
                                               jObject.GetStringValue(_accessTokenParatemeter),
                                               "bearer".Equals(jObject.GetStringValue(_tokenTypeParameter), StringComparison.OrdinalIgnoreCase) ?
                                               TokenType.Bearer
                    :
                                               TokenType.Unknown,
                                               jObject.GetStringValue(_refreshTokenParameter),
                                               GetExpireDate(jObject.GetStringValue(_expiresInParameter)))));
        }
예제 #5
0
        internal virtual OAuthResponse ReadOAuthResponseFromUrl(string url)
        {
            var parameters = ReadResponseParameter(url);

            if (parameters.ContainsKey(_errorParameter))
            {
                return(OAuthResponse.WithError(parameters[_errorParameter],
                                               parameters.ContainsKey(_errorDescriptionParameter) ?
                                               parameters[_errorDescriptionParameter]
                        :
                                               null
                                               ));
            }

            if (parameters.ContainsKey(_codeParameter))
            {
                return(OAuthResponse.WithCode(parameters[_codeParameter]));
            }

            var tokenTypeValue = parameters.ContainsKey(_tokenTypeParameter)
                ?
                                 parameters[_tokenTypeParameter]
                : string.Empty;

            return(OAuthResponse.WithToken(new OAuthAccessToken(
                                               parameters[Definition.TokenResponseUrlParameter],
                                               "bearer".Equals(tokenTypeValue, StringComparison.OrdinalIgnoreCase) ?
                                               TokenType.Bearer
                    :
                                               TokenType.Url,
                                               GetExpireDate(parameters.ContainsKey(Definition.ExpiresParameter) ?
                                                             parameters[Definition.ExpiresParameter]
                        :
                                                             string.Empty)
                                               )));
        }