Example #1
0
 /// <summary>
 /// Creates an exception based on one of the existing values defined in
 /// GeneralErrors, AuthorizationRequestErrors or TokenRequestErrors,
 /// providing a root cause.
 /// </summary>
 public static AuthzException fromTemplate(
     AuthzException ex,
     Exception rootCause)
 {
     return(new AuthzException(
                ex.type,
                ex.code,
                ex.error,
                ex.errorDescription,
                ex.errorUri,
                rootCause));
 }
Example #2
0
 /// <summary>
 /// Creates an exception based on one of the existing values defined in
 /// AuthorizationRequestErrors or TokenRequestErrors, adding information
 /// retrieved from OAuth error response.
 /// </summary>
 public static AuthzException fromOAuthTemplate(
     AuthzException ex,
     String errorOverride,
     String errorDescriptionOverride,
     Uri errorUriOverride)
 {
     return(new AuthzException(
                ex.type,
                ex.code,
                (errorOverride != null) ? errorOverride : ex.error,
                (errorDescriptionOverride != null) ? errorDescriptionOverride : ex.errorDescription,
                (errorUriOverride != null) ? errorUriOverride : ex.errorUri,
                null));
 }
Example #3
0
        /// <summary>
        /// Performs a token refresh.
        /// </summary>
        /// <returns>The JSOn answer received by the server.</returns>
        /// <param name="url">The url of the openid serevr</param>
        /// <param name="clientId">Client identifier.</param>
        /// <param name="refreshToken">Refresh token.</param>
        public async Task <JsonValue> RefreshAsync(String url, String clientId, String refreshToken)
        {
            JsonValue json = null;

            try
            {
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var parameters = new Dictionary <string, string> {
                    { "scope", "openid" }, { "refresh_token", refreshToken }, { "grant_type", "refresh_token" }
                };

                var clientAuthParams = new Dictionary <string, string> {
                    { "client_id", clientId }
                };
                if (clientAuthParams != null)
                {
                    foreach (KeyValuePair <string, string> clientParam in clientAuthParams)
                    {
                        parameters.Add(clientParam.Key, clientParam.Value);
                    }
                }

                var requestBody     = new FormUrlEncodedContent(parameters);
                var responseMessage = await client.PostAsync(url, requestBody).ConfigureAwait(false);

                var responseString = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);

                json = JsonValue.Parse(responseString);
            }
            catch (IOException e)
            {
                throw AuthzException.fromTemplate(AuthzException.GeneralErrors.NETWORK_ERROR, e);
            }
            catch (ArgumentException e)
            {
                throw AuthzException.fromTemplate(AuthzException.GeneralErrors.JSON_DESERIALIZATION_ERROR, e);
            }
            catch (Exception e)
            {
                throw AuthzException.fromTemplate(AuthzException.GeneralErrors.NETWORK_ERROR, e);
            }

            if (json.ContainsKey(AuthzException.PARAM_ERROR))
            {
                AuthzException ex;
                try
                {
                    string error = json[AuthzException.PARAM_ERROR];
                    ex = AuthzException.fromOAuthTemplate(
                        AuthzException.TokenRequestErrors.byString(error), error,
                        json.ContainsKey(AuthzException.PARAM_ERROR_DESCRIPTION) ? json[AuthzException.PARAM_ERROR_DESCRIPTION] : null,
                        parseUriIfAvailable(json.ContainsKey(AuthzException.PARAM_ERROR_URI) ? json[AuthzException.PARAM_ERROR_URI] : null));
                }
                catch (Exception e)
                {
                    ex = AuthzException.fromTemplate(AuthzException.GeneralErrors.JSON_DESERIALIZATION_ERROR, e);
                }

                throw ex;
            }

            return(json);
        }