private static async Task <OAuth20AuthResponse> AuthenticateUsingOAuth20Async(HttpRequestMessage httpRequest, OAuth20AuthenticationConfig authConfig) { var authRequestContent = HttpOAuth20ContentFactory.Create(authConfig); HttpRequestMessage authRequest = new HttpRequestMessage { Content = authRequestContent, Method = HttpMethod.Post, RequestUri = new Uri(authConfig.HttpRequestConfig.AuthUrl), }; authRequest.Headers.UserAgent.TryParseAdd("PostmanRuntime/7.26.8"); AddDefaultHeaders(authRequest); var httpClient = GetHttpClient(); var authResponse = await httpClient.SendAsync(authRequest); var authResponseContent = await authResponse.Content.ReadAsStringAsync(); if (authResponse.StatusCode != HttpStatusCode.OK) { throw new Exception($"{AutomatonConstants.OAuth20.OAUTH_20} failed to authenticate. Reason: {authResponseContent}, Trying to access resource: {httpRequest.RequestUri}, HttpStatusCode: {authResponse.StatusCode}, Configuration: {JsonConvert.SerializeObject(authConfig)}"); } OAuth20AuthResponse oAuthTokens = JsonConvert.DeserializeObject <OAuth20AuthResponse>(authResponseContent); return(oAuthTokens); }
private static async Task <OAuth20AuthResponse> AuthenticateUsingOAuth20Async(HttpRequestMessage httpRequest, OAuth20AuthenticationConfig authConfig) { var authRequestContent = HttpOAuth20ContentFactory.Create(authConfig); HttpRequestMessage authRequest = new HttpRequestMessage { Content = authRequestContent, Method = HttpMethod.Post, RequestUri = new Uri(authConfig.HttpRequestConfig.AuthUrl), }; authRequest.Headers.UserAgent.TryParseAdd("PostmanRuntime/7.26.8"); authRequest.Headers.TryAddWithoutValidation("Accept", "*/*"); authRequest.Headers.TryAddWithoutValidation("Cache-Control", "no-cache"); authRequest.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate, br"); authRequest.Headers.TryAddWithoutValidation("Connection", "keep-alive"); var handler = new HttpClientHandler(); handler.AllowAutoRedirect = true; handler.UseProxy = false; handler.CheckCertificateRevocationList = false; var httpClient = new HttpClient(handler); var authResponse = await httpClient.SendAsync(authRequest); var authResponseContent = await authResponse.Content.ReadAsStringAsync(); if (authResponse.StatusCode != HttpStatusCode.OK) { throw new Exception($"{AutomatonConstants.OAuth20.OAUTH_20} failed to authenticate. Trying to access resource: {httpRequest.RequestUri}, HttpStatusCode: {authResponse.StatusCode}, Reason: {authResponseContent}, Configuration: {JsonConvert.SerializeObject(authConfig)}"); } OAuth20AuthResponse oAuthTokens = JsonConvert.DeserializeObject <OAuth20AuthResponse>(authResponseContent); return(oAuthTokens); }
public static async Task SignOut(OAuth20AuthenticationConfig authConfig, OAuth20AuthResponse authResponse) { HttpRequestMessage revocationRequest = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = new Uri(authConfig.HttpRequestConfig.RevocationUrl) }; var formKeyValuePairs = OAuth20FormUrlEncodedFactory.CreateUsingRevocation(authConfig, authResponse); revocationRequest.Content = new FormUrlEncodedContent(formKeyValuePairs); AddDefaultHeaders(revocationRequest); var httpClient = GetHttpClient(); var revocationResponse = await httpClient.SendAsync(revocationRequest); var authResponseContent = await revocationResponse.Content.ReadAsStringAsync(); if (revocationResponse.StatusCode != HttpStatusCode.OK) { throw new Exception($"{AutomatonConstants.OAuth20.OAUTH_20} failed to authenticate. Trying to access resource: {revocationRequest.RequestUri}, HttpStatusCode: {revocationResponse.StatusCode}, Reason: {authResponseContent}, Configuration: {JsonConvert.SerializeObject(authConfig)}"); } }
public static IEnumerable <KeyValuePair <string, string> > CreateUsingRevocation(OAuth20AuthenticationConfig authConfig, OAuth20AuthResponse authResponse) { var formKeyValuePairs = new List <KeyValuePair <string, string> >(); ValidateDefaultFormValues(authConfig.Grant); formKeyValuePairs.Add(new KeyValuePair <string, string>(AutomatonConstants.OAuth20.CLIENT_ID, authConfig.Grant.ClientId)); formKeyValuePairs.Add(new KeyValuePair <string, string>(AutomatonConstants.OAuth20.CLIENT_SECRET, authConfig.Grant.ClientSecret)); formKeyValuePairs.Add(new KeyValuePair <string, string>(AutomatonConstants.OAuth20.TOKEN, authResponse.refresh_token)); formKeyValuePairs.Add(new KeyValuePair <string, string>(AutomatonConstants.OAuth20.TOKEN_TYPE_HYNT, AutomatonConstants.OAuth20.TOKEN_TYPE_HYNT_REFRESH_TOKEN)); return(formKeyValuePairs); }