/// <summary> /// Sends a JSON web key set document request /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <JsonWebKeySetResponse> GetJsonWebKeySetAsync(this HttpMessageInvoker client, JsonWebKeySetRequest request, CancellationToken cancellationToken = default) { var clone = request.Clone(); clone.Method = HttpMethod.Get; clone.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/jwk-set+json")); clone.Prepare(); HttpResponseMessage response; try { response = await client.SendAsync(clone, cancellationToken).ConfigureAwait(false); string responseContent = null; if (response.Content != null) { responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); } if (!response.IsSuccessStatusCode) { return(await ProtocolResponse.FromHttpResponseAsync <JsonWebKeySetResponse>(response, $"Error connecting to {clone.RequestUri.AbsoluteUri}: {response.ReasonPhrase}").ConfigureAwait(false)); } } catch (Exception ex) { return(ProtocolResponse.FromException <JsonWebKeySetResponse>(ex, $"Error connecting to {clone.RequestUri.AbsoluteUri}. {ex.Message}.")); } return(await ProtocolResponse.FromHttpResponseAsync <JsonWebKeySetResponse>(response)); }
/// <summary> /// Sends a userinfo request. /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <DeviceAuthorizationResponse> RequestDeviceAuthorizationAsync(this HttpMessageInvoker client, DeviceAuthorizationRequest request, CancellationToken cancellationToken = default) { var httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Address); httpRequest.Headers.Accept.Clear(); httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var clone = request.Clone(); ClientCredentialsHelper.PopulateClientCredentials(clone, httpRequest); clone.Parameters.AddOptional(OidcConstants.AuthorizeRequest.Scope, request.Scope); httpRequest.Content = new FormUrlEncodedContent(clone.Parameters); HttpResponseMessage response; try { response = await client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { return(ProtocolResponse.FromException <DeviceAuthorizationResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <DeviceAuthorizationResponse>(response).ConfigureAwait(false)); }
/// <summary> /// Send a dynamic registration request. /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <DynamicClientRegistrationResponse> RegisterClientAsync(this HttpMessageInvoker client, DynamicClientRegistrationRequest request, CancellationToken cancellationToken = default) { var clone = request.Clone(); clone.Method = HttpMethod.Post; clone.Content = new StringContent(JsonSerializer.Serialize(request.Document), Encoding.UTF8, "application/json"); clone.Prepare(); if (request.Token.IsPresent()) { clone.SetBearerToken(request.Token); } HttpResponseMessage response; try { response = await client.SendAsync(clone, cancellationToken).ConfigureAwait(); } catch (Exception ex) { return(ProtocolResponse.FromException <DynamicClientRegistrationResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <DynamicClientRegistrationResponse>(response).ConfigureAwait()); }
/// <summary> /// Sends a userinfo request. /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <UserInfoResponse> GetUserInfoAsync(this HttpMessageInvoker client, UserInfoRequest request, CancellationToken cancellationToken = default) { if (request.Token.IsMissing()) { throw new ArgumentNullException(nameof(request.Token)); } var clone = request.Clone(); clone.Method = HttpMethod.Get; clone.SetBearerToken(request.Token); clone.Prepare(); HttpResponseMessage response; try { response = await client.SendAsync(clone, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { return(ProtocolResponse.FromException <UserInfoResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <UserInfoResponse>(response).ConfigureAwait(false)); }
/// <summary> /// Send a dynamic registration request. /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <DynamicClientRegistrationResponse> RegisterClientAsync(this HttpMessageInvoker client, DynamicClientRegistrationRequest request, CancellationToken cancellationToken = default) { var httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Address) { Content = new StringContent(JsonConvert.SerializeObject(request.Document), Encoding.UTF8, "application/json") }; httpRequest.Headers.Accept.Clear(); httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (request.Token.IsPresent()) { httpRequest.SetBearerToken(request.Token); } HttpResponseMessage response; try { response = await client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { return(ProtocolResponse.FromException <DynamicClientRegistrationResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <DynamicClientRegistrationResponse>(response).ConfigureAwait(false)); }
/// <summary> /// Sends a discovery document request /// </summary> /// <param name="client">The client.</param> /// <param name="address">The endpoint address.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <JsonWebKeyResponse> GetJsonWebKeySetAsync(this HttpMessageInvoker client, string address = null, CancellationToken cancellationToken = default) { HttpResponseMessage response; using (HttpRequestMessage getRequest = new HttpRequestMessage(HttpMethod.Get, address)) { try { response = await client.SendAsync(getRequest, cancellationToken).ConfigureAwait(false); string responseContent = null; if (response.Content != null) { responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); } if (!response.IsSuccessStatusCode) { return(await ProtocolResponse.FromHttpResponseAsync <JsonWebKeyResponse>(response, $"Error connecting to {address}: {response.ReasonPhrase}").ConfigureAwait(false)); } } catch (Exception ex) { return(ProtocolResponse.FromException <JsonWebKeyResponse>(ex, $"Error connecting to {address}. {ex.Message}.")); } return(await ProtocolResponse.FromHttpResponseAsync <JsonWebKeyResponse>(response)); } }
internal static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken cancellationToken = default) { if (!request.Parameters.TryGetValue(OidcConstants.TokenRequest.ClientId, out _)) { if (request.ClientId.IsMissing()) { throw new InvalidOperationException("client_id is missing"); } } var httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Address); httpRequest.Headers.Accept.Clear(); httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); ClientCredentialsHelper.PopulateClientCredentials(request, httpRequest); httpRequest.Content = new FormUrlEncodedContent(request.Parameters); HttpResponseMessage response; try { response = await client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { return(ProtocolResponse.FromException <TokenResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <TokenResponse>(response).ConfigureAwait(false)); }
/// <summary> /// Sends a userinfo request. /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <UserInfoResponse> GetUserInfoAsync(this HttpMessageInvoker client, UserInfoRequest request, CancellationToken cancellationToken = default) { if (request.Token.IsMissing()) { throw new ArgumentNullException(nameof(request.Token)); } var httpRequest = new HttpRequestMessage(HttpMethod.Get, request.Address); httpRequest.Headers.Accept.Clear(); httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpRequest.SetBearerToken(request.Token); HttpResponseMessage response; try { response = await client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { return(ProtocolResponse.FromException <UserInfoResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <UserInfoResponse>(response).ConfigureAwait(false)); }
internal static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken cancellationToken = default) { if (!request.Parameters.TryGetValue(OidcConstants.TokenRequest.ClientId, out _)) { if (request.ClientId.IsMissing()) { throw new InvalidOperationException("client_id is missing"); } } request.Prepare(); request.Method = HttpMethod.Post; HttpResponseMessage response; try { response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { return(ProtocolResponse.FromException <TokenResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <TokenResponse>(response).ConfigureAwait(false)); }
//internal static async Task<TokenResponse> RequestTokenAsync(HttpMessageInvoker client, Request request, CancellationToken? cancellationToken = null) //{ // HttpClientTokenRequestExtensions.<RequestTokenAsync>d__6 variable = new HttpClientTokenRequestExtensions.<RequestTokenAsync>d__6(); // variable.client = client; // variable.request = request; // variable.cancellationToken = cancellationToken; // variable.<>t__builder = AsyncTaskMethodBuilder<TokenResponse>.Create(); // variable.<>1__state = -1; // variable.<>t__builder.Start < HttpClientTokenRequestExtensions.<RequestTokenAsync>d__6>(ref variable); // return variable.<>t__builder.Task; //} internal static async Task <TokenResponse> RequestTokenAsync(HttpMessageInvoker client, Request request, CancellationToken cancellationToken) { HttpResponseMessage response; try { response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { throw ex; } return(await ProtocolResponse.FromHttpResponseAsync <TokenResponse>(response).ConfigureAwait()); }
internal static async Task <IdentityModel.Client.IdentityModelExtensions.TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken?cancellationToken) { request.Prepare(); request.Method = HttpMethod.Post; HttpResponseMessage response; try { response = await client.SendAsync(request, cancellationToken.HasValue?cancellationToken.Value : new CancellationToken()).ConfigureAwait(false); } catch (Exception ex) { return(ProtocolResponse.FromException <IdentityModel.Client.IdentityModelExtensions.TokenResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <IdentityModel.Client.IdentityModelExtensions.TokenResponse>(response).ConfigureAwait(false)); }
internal static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken cancellationToken = default) { request.Prepare(); request.Method = HttpMethod.Post; HttpResponseMessage response; try { response = await client.SendAsync(request, cancellationToken).ConfigureAwait(); } catch (Exception ex) { return(ProtocolResponse.FromException <TokenResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <TokenResponse>(response).ConfigureAwait()); }
/// <summary> /// Sends a userinfo request. /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <DeviceAuthorizationResponse> RequestDeviceAuthorizationAsync(this HttpMessageInvoker client, DeviceAuthorizationRequest request, CancellationToken cancellationToken = default) { var clone = request.Clone(); clone.Parameters.AddOptional(OidcConstants.AuthorizeRequest.Scope, request.Scope); clone.Method = HttpMethod.Post; clone.Prepare(); HttpResponseMessage response; try { response = await client.SendAsync(clone, cancellationToken).ConfigureAwait(); } catch (Exception ex) { return(ProtocolResponse.FromException <DeviceAuthorizationResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <DeviceAuthorizationResponse>(response).ConfigureAwait()); }
/// <summary> /// Sends an OAuth token introspection request. /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <TokenIntrospectionResponse> IntrospectTokenAsync(this HttpMessageInvoker client, TokenIntrospectionRequest request, CancellationToken cancellationToken = default) { var clone = request.Clone(); clone.Method = HttpMethod.Post; clone.Parameters.AddRequired(OidcConstants.TokenIntrospectionRequest.Token, request.Token); clone.Parameters.AddOptional(OidcConstants.TokenIntrospectionRequest.TokenTypeHint, request.TokenTypeHint); clone.Prepare(); HttpResponseMessage response; try { response = await client.SendAsync(clone, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { return(ProtocolResponse.FromException <TokenIntrospectionResponse>(ex)); } return(await ProtocolResponse.FromHttpResponseAsync <TokenIntrospectionResponse>(response).ConfigureAwait(false)); }
/// <summary> /// Sends a discovery document request /// </summary> /// <param name="client">The client.</param> /// <param name="request">The request.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static async Task <DiscoveryDocumentResponse> GetDiscoveryDocumentAsync(this HttpMessageInvoker client, DiscoveryDocumentRequest request, CancellationToken cancellationToken = default) { string address; if (request.Address.IsPresent()) { address = request.Address; } else if (client is HttpClient) { address = ((HttpClient)client).BaseAddress.AbsoluteUri; } else { throw new ArgumentException("An address is required."); } var parsed = DiscoveryEndpoint.ParseUrl(address); var authority = parsed.Authority; var url = parsed.Url; if (request.Policy.Authority.IsMissing()) { request.Policy.Authority = authority; } string jwkUrl = ""; if (!DiscoveryEndpoint.IsSecureScheme(new Uri(url), request.Policy)) { return(ProtocolResponse.FromException <DiscoveryDocumentResponse>(new InvalidOperationException("HTTPS required"), $"Error connecting to {url}. HTTPS required.")); } try { var clone = request.Clone(); clone.Method = HttpMethod.Get; clone.Prepare(); clone.RequestUri = new Uri(url); var response = await client.SendAsync(clone, cancellationToken).ConfigureAwait(false); string responseContent = null; if (response.Content != null) { responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); } if (!response.IsSuccessStatusCode) { return(await ProtocolResponse.FromHttpResponseAsync <DiscoveryDocumentResponse>(response, $"Error connecting to {url}: {response.ReasonPhrase}")); } var disco = await ProtocolResponse.FromHttpResponseAsync <DiscoveryDocumentResponse>(response, request.Policy).ConfigureAwait(false); if (disco.IsError) { return(disco); } try { jwkUrl = disco.JwksUri; if (jwkUrl != null) { var jwkClone = request.Clone <JsonWebKeySetRequest>(); jwkClone.Method = HttpMethod.Get; jwkClone.Address = jwkUrl; jwkClone.Prepare(); var jwkResponse = await client.GetJsonWebKeySetAsync(jwkClone, cancellationToken).ConfigureAwait(false); if (jwkResponse.IsError) { return(await ProtocolResponse.FromHttpResponseAsync <DiscoveryDocumentResponse>(jwkResponse.HttpResponse, $"Error connecting to {jwkUrl}: {jwkResponse.HttpErrorReason}").ConfigureAwait(false)); } disco.KeySet = jwkResponse.KeySet; } return(disco); } catch (Exception ex) { return(ProtocolResponse.FromException <DiscoveryDocumentResponse>(ex, $"Error connecting to {jwkUrl}. {ex.Message}.")); } } catch (Exception ex) { return(ProtocolResponse.FromException <DiscoveryDocumentResponse>(ex, $"Error connecting to {url}. {ex.Message}.")); } }