//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OAuth2 { using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading; using System.Threading.Tasks; using DotNetOpenAuth.Messaging; using Validation; /// /// An that applies a bearer token to each outbound HTTP request. /// internal class BearerTokenHttpMessageHandler : DelegatingHandler { /// /// Initializes a new instance of the class. /// /// The bearer token. /// The inner handler. public BearerTokenHttpMessageHandler(string bearerToken, HttpMessageHandler innerHandler) : base(innerHandler) { Requires.NotNullOrEmpty(bearerToken, "bearerToken"); this.BearerToken = bearerToken; } /// /// Initializes a new instance of the class. /// /// The client associated with the authorization. /// The authorization. /// The inner handler. public BearerTokenHttpMessageHandler(ClientBase client, IAuthorizationState authorization, HttpMessageHandler innerHandler) : base(innerHandler) { Requires.NotNull(client, "client"); Requires.NotNull(authorization, "authorization"); Requires.That(!string.IsNullOrEmpty(authorization.AccessToken), "authorization.AccessToken", "AccessToken must be non-empty"); this.Client = client; this.Authorization = authorization; } /// /// Gets the bearer token. /// /// /// The bearer token. /// internal string BearerToken { get; private set; } /// /// Gets the authorization. /// internal IAuthorizationState Authorization { get; private set; } /// /// Gets the OAuth 2 client associated with the . /// internal ClientBase Client { get; private set; } /// /// Sends an HTTP request to the inner handler to send to the server as an asynchronous operation. /// /// The HTTP request message to send to the server. /// A cancellation token to cancel operation. /// /// Returns . The task object representing the asynchronous operation. /// protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { string bearerToken = this.BearerToken; if (bearerToken == null) { ErrorUtilities.VerifyProtocol(!this.Authorization.AccessTokenExpirationUtc.HasValue || this.Authorization.AccessTokenExpirationUtc >= DateTime.UtcNow || this.Authorization.RefreshToken != null, ClientStrings.AuthorizationExpired); if (this.Authorization.AccessTokenExpirationUtc.HasValue && this.Authorization.AccessTokenExpirationUtc.Value < DateTime.UtcNow) { ErrorUtilities.VerifyProtocol(this.Authorization.RefreshToken != null, ClientStrings.AccessTokenRefreshFailed); await this.Client.RefreshAuthorizationAsync(this.Authorization, cancellationToken: cancellationToken); } bearerToken = this.Authorization.AccessToken; } request.Headers.Authorization = new AuthenticationHeaderValue(Protocol.BearerHttpAuthorizationScheme, bearerToken); return await base.SendAsync(request, cancellationToken); } } }