namespace Best.HTTP.Request.Authenticators
{
///
/// An implementation for Bearer Token authentication.
///
///
/// Bearer Token authentication is a method used to access protected resources on a server.
/// It involves including a bearer token in the Authorization header of an HTTP request to prove the identity of the requester.
///
public class BearerTokenAuthenticator : IAuthenticator
{
///
/// Initializes a new instance of the BearerTokenAuthenticator class with the specified Bearer Token.
///
/// The Bearer Token to use for authentication.
public string Token { get; private set; }
///
/// Sets up the required Authorization header with the Bearer Token for the HTTP request.
///
/// The HTTP request for which the Authorization header should be added.
///
/// When sending an HTTP request to a server that requires Bearer Token authentication,
/// this method sets the Authorization header with the Bearer Token to prove the identity of the requester.
/// This allows the requester to access protected resources on the server.
///
public BearerTokenAuthenticator(string token) => this.Token = token;
public void SetupRequest(HTTPRequest request)
{
if (!string.IsNullOrEmpty(this.Token))
request.SetHeader("Authorization", $"Bearer {this.Token}");
}
///
/// Handles the server response with a 401 (Unauthorized) status code and a WWW-Authenticate header.
/// This authenticator does not handle challenges and always returns false.
///
/// The HTTP request that received the 401 response.
/// The HTTP response containing the 401 (Unauthorized) status.
/// false, as this authenticator does not handle challenges.
///
/// Bearer Token authentication typically does not require handling challenges,
/// as the Bearer Token is included directly in the Authorization header of the request.
/// This method always returns false, as no additional challenge processing is needed.
///
public bool HandleChallange(HTTPRequest req, HTTPResponse resp) => false;
}
}