mirror of
https://github.com/zoriya/octokit.net.git
synced 2025-12-05 23:06:10 +00:00
35 lines
1.4 KiB
C#
35 lines
1.4 KiB
C#
using System;
|
|
using System.Globalization;
|
|
|
|
namespace Octokit.Internal
|
|
{
|
|
class TokenAuthenticator : IAuthenticationHandler
|
|
{
|
|
///<summary>
|
|
///Authenticate a request using the OAuth2 Token (sent in a header) authentication scheme
|
|
///</summary>
|
|
///<param name="request">The request to authenticate</param>
|
|
///<param name="credentials">The credentials to attach to the request</param>
|
|
///<remarks>
|
|
///See the <a href="http://developer.github.com/v3/#oauth2-token-sent-in-a-header">OAuth2 Token (sent in a header) documentation</a> for more information.
|
|
///</remarks>
|
|
public void Authenticate(IRequest request, Credentials credentials)
|
|
{
|
|
Ensure.ArgumentNotNull(request, nameof(request));
|
|
Ensure.ArgumentNotNull(credentials, nameof(credentials));
|
|
Ensure.ArgumentNotNull(credentials.Password, nameof(credentials.Password));
|
|
|
|
var token = credentials.GetToken();
|
|
if (credentials.Login != null)
|
|
{
|
|
throw new InvalidOperationException("The Login is not null for a token authentication request. You " +
|
|
"probably did something wrong.");
|
|
}
|
|
if (token != null)
|
|
{
|
|
request.Headers["Authorization"] = string.Format(CultureInfo.InvariantCulture, "Token {0}", token);
|
|
}
|
|
}
|
|
}
|
|
}
|