Esempio n. 1
0
        /// <summary>
        /// RevokeAsync
        /// </summary>
        /// <param name="request">request</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns>Task of TokenRevocationResponse</returns>
        public virtual async Task <TokenRevocationResponse> RevokeAsync(
            TokenRevocationRequest request,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrWhiteSpace(request.Token))
            {
                throw new ArgumentNullException(nameof(request.Token));
            }

            HttpRequestMessage msgRequest = new HttpRequestMessage(HttpMethod.Post, Address);

            JsonToken jobject = new JsonToken();

            jobject.token = request.Token;//Refresh token or bearer access token
            var json          = JsonConvert.SerializeObject(jobject);
            var stringContent = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");

            msgRequest.Content = stringContent;

            if (OAuth2Client.AdvancedLoggerEnabled != false)
            {
                OAuth2Client.AdvancedLogger.Log("Request url- " + Address);
                OAuth2Client.AdvancedLogger.Log("Request headers- ");
                OAuth2Client.AdvancedLogger.Log("Authorization Header: " + Client.DefaultRequestHeaders.Authorization.ToString());//check
                OAuth2Client.AdvancedLogger.Log("ContentType header: " + "application/json");
                OAuth2Client.AdvancedLogger.Log("Accept header: " + "application/json");
                OAuth2Client.AdvancedLogger.Log("Request Body: " + await msgRequest.Content.ReadAsStringAsync().ConfigureAwait(false));
            }

            try
            {
                var response = await Client.PostAsync("", msgRequest.Content).ConfigureAwait(false);

                HttpResponseHeaders headers = response.Headers;


                if (response.StatusCode == HttpStatusCode.OK)
                {
                    string intuit_tid;
                    IEnumerable <string> values;
                    if (headers.TryGetValues("intuit_tid", out values))
                    {
                        intuit_tid = values.First();
                    }
                    else
                    {
                        intuit_tid = "None";
                    }

                    if (OAuth2Client.AdvancedLoggerEnabled != false)
                    {
                        if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
                        {
                            OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode + ", Token Revoked successfully");
                        }
                        else
                        {
                            OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode + ", Token Revoked successfully");
                        }
                    }
                    return(new TokenRevocationResponse());
                }
                else if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    string intuit_tid;
                    IEnumerable <string> values;
                    if (headers.TryGetValues("intuit_tid", out values))
                    {
                        intuit_tid = values.First();
                    }
                    else
                    {
                        intuit_tid = "None";
                    }

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

                    if (OAuth2Client.AdvancedLoggerEnabled != false)
                    {
                        if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
                        {
                            OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode);
                        }
                        else
                        {
                            OAuth2Client.AdvancedLogger.Log("Response Intuit_Tid header - " + intuit_tid + ", Response Status Code- " + response.StatusCode + ", Response Body- " + content);
                        }
                    }
                    return(new TokenRevocationResponse(content)); //errorDetail can be added here if required.
                }
                else
                {
                    string errorDetail = "";


                    if (headers.WwwAuthenticate != null)
                    {
                        errorDetail = headers.WwwAuthenticate.ToString();
                    }

                    if (errorDetail != null && errorDetail != "")
                    {
                        if (OAuth2Client.AdvancedLoggerEnabled != false)
                        {
                            if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
                            {
                                OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode);
                            }
                            else
                            {
                                OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode + ", Error Details- " + response.ReasonPhrase + ": " + errorDetail);
                            }
                        }
                        return(new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase + ": " + errorDetail));
                    }
                    else
                    {
                        if (OAuth2Client.AdvancedLoggerEnabled != false)
                        {
                            if (OAuth2Client.ShowInfoLogs == true)//log just intuit_tid for info logging mode
                            {
                                OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode);
                            }
                            else
                            {
                                OAuth2Client.AdvancedLogger.Log("Response: Status Code- " + response.StatusCode + ", Error Details- " + response.ReasonPhrase + ": " + errorDetail);
                            }
                        }
                        return(new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase));
                    }
                }
            }
            catch (System.Exception ex)
            {
                return(new TokenRevocationResponse(ex));
            }
        }
        /// <summary>
        /// RevokeAsync
        /// </summary>
        /// <param name="request">request</param>
        /// <param name="cancellationToken">cancellationToken</param>
        /// <returns>Task of TokenRevocationResponse</returns>
        public virtual async Task <TokenRevocationResponse> RevokeAsync(
            TokenRevocationRequest request,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrWhiteSpace(request.Token))
            {
                throw new ArgumentNullException(nameof(request.Token));
            }

            HttpRequestMessage msgRequest = new HttpRequestMessage(HttpMethod.Post, Address);

            JsonToken jobject = new JsonToken();

            jobject.token = request.Token;//Refresh token or bearer access token
            var json          = JsonConvert.SerializeObject(jobject);
            var stringContent = new System.Net.Http.StringContent(json, Encoding.UTF8, "application/json");

            msgRequest.Content = stringContent;



            try
            {
                var response = await Client.PostAsync("", msgRequest.Content).ConfigureAwait(false);


                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(new TokenRevocationResponse());
                }
                else if (response.StatusCode == HttpStatusCode.BadRequest)
                {
                    var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return(new TokenRevocationResponse(content)); //errorDetail can be added here if required.
                }
                else
                {
                    string errorDetail = "";

                    HttpResponseHeaders headers = response.Headers;
                    if (headers.WwwAuthenticate != null)
                    {
                        errorDetail = headers.WwwAuthenticate.ToString();
                    }



                    if (errorDetail != null && errorDetail != "")
                    {
                        return(new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase + ": " + errorDetail));
                    }
                    else
                    {
                        return(new TokenRevocationResponse(response.StatusCode, response.ReasonPhrase));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new TokenRevocationResponse(ex));
            }
        }