using System.Threading.Tasks;
using System;
using System.Collections.Generic;
namespace Octokit
{
///
/// A client for GitHub's OAuth API.
///
///
/// See the OAuth API documentation for more details.
///
public class AuthorizationsClient : ApiClient, IAuthorizationsClient
{
///
/// Initializes a new GitHub OAuth API client.
///
/// An API connection
public AuthorizationsClient(IApiConnection apiConnection) : base(apiConnection)
{
}
///
/// Gets all s for the authenticated user.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
///
/// Thrown when the current user does not have permission to make the request.
///
/// Thrown when a general API error occurs.
/// A list of s for the authenticated user.
[ManualRoute("GET", "/authorizations")]
public Task> GetAll()
{
return GetAll(ApiOptions.None);
}
///
/// Gets all s for the authenticated user.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// Options for changing the API response
///
/// Thrown when the current user does not have permission to make the request.
///
/// Thrown when a general API error occurs.
/// A list of s for the authenticated user.
[ManualRoute("GET", "/authorizations")]
public Task> GetAll(ApiOptions options)
{
Ensure.ArgumentNotNull(options, nameof(options));
return ApiConnection.GetAll(ApiUrls.Authorizations(), options);
}
///
/// Gets a specific for the authenticated user.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// The Id of the to get
///
/// Thrown when the current user does not have permission to make this request.
///
/// Thrown when a general API error occurs.
/// The specified .
[ManualRoute("GET", "/authorizations/{id}")]
public Task Get(int authorizationId)
{
return ApiConnection.Get(ApiUrls.Authorizations(authorizationId), null);
}
///
/// Creates a new personal token for the authenticated user.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// Describes the new authorization to create
///
/// Thrown when the current user does not have permission to make this request.
///
///
/// Thrown when the current account has two-factor authentication enabled and an authentication code is required.
///
/// Thrown when a general API error occurs.
/// The created .
[ManualRoute("POST", "/authorizations")]
public Task Create(NewAuthorization newAuthorization)
{
Ensure.ArgumentNotNull(newAuthorization, nameof(newAuthorization));
var requestData = new
{
scopes = newAuthorization.Scopes,
note = newAuthorization.Note,
note_url = newAuthorization.NoteUrl,
fingerprint = newAuthorization.Fingerprint
};
var endpoint = ApiUrls.Authorizations();
return ApiConnection.Post(endpoint, requestData);
}
///
/// Creates a new personal token for the authenticated user.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// The two-factor authentication code in response to the current user's previous challenge
/// Describes the new authorization to create
///
/// Thrown when the current user does not have permission to make this request.
///
///
/// Thrown when the current account has two-factor authentication enabled and an authentication code is required.
///
/// Thrown when a general API error occurs.
/// The created .
[ManualRoute("POST", "/authorizations")]
public Task Create(
NewAuthorization newAuthorization,
string twoFactorAuthenticationCode)
{
Ensure.ArgumentNotNull(newAuthorization, nameof(newAuthorization));
Ensure.ArgumentNotNullOrEmptyString(twoFactorAuthenticationCode, nameof(twoFactorAuthenticationCode));
var requestData = new
{
scopes = newAuthorization.Scopes,
note = newAuthorization.Note,
note_url = newAuthorization.NoteUrl,
fingerprint = newAuthorization.Fingerprint
};
var endpoint = ApiUrls.Authorizations();
return ApiConnection.Post(endpoint, requestData, null, twoFactorAuthenticationCode);
}
///
/// Creates a new authorization for the specified OAuth application if an authorization for that application
/// doesn’t already exist for the user; otherwise, it fails.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// Client Id of the OAuth application for the token
/// The client secret
/// Describes the new authorization to create
///
/// Thrown when the current user does not have permission to make this request.
///
///
/// Thrown when the current account has two-factor authentication enabled and an authentication code is required.
///
/// Thrown when a general API error occurs.
/// The created .
[ManualRoute("POST", "/authorizations")]
public Task Create(
string clientId,
string clientSecret,
NewAuthorization newAuthorization)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
Ensure.ArgumentNotNullOrEmptyString(clientSecret, nameof(clientSecret));
Ensure.ArgumentNotNull(newAuthorization, nameof(newAuthorization));
var requestData = new
{
client_id = clientId,
client_secret = clientSecret,
scopes = newAuthorization.Scopes,
note = newAuthorization.Note,
note_url = newAuthorization.NoteUrl,
fingerprint = newAuthorization.Fingerprint
};
var endpoint = ApiUrls.Authorizations();
return ApiConnection.Post(endpoint, requestData);
}
///
/// Creates a new authorization for the specified OAuth application if an authorization for that application
/// doesn’t already exist for the user; otherwise, it fails.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// Client Id of the OAuth application for the token
/// The client secret
/// The two-factor authentication code in response to the current user's previous challenge
/// Describes the new authorization to create
///
/// Thrown when the current user does not have permission to make this request.
///
///
/// Thrown when the current account has two-factor authentication enabled and an authentication code is required.
///
/// Thrown when a general API error occurs.
/// The created .
[ManualRoute("POST", "/authorizations")]
public Task Create(
string clientId,
string clientSecret,
NewAuthorization newAuthorization,
string twoFactorAuthenticationCode)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
Ensure.ArgumentNotNullOrEmptyString(clientSecret, nameof(clientSecret));
Ensure.ArgumentNotNull(newAuthorization, nameof(newAuthorization));
Ensure.ArgumentNotNullOrEmptyString(twoFactorAuthenticationCode, nameof(twoFactorAuthenticationCode));
var requestData = new
{
client_id = clientId,
client_secret = clientSecret,
scopes = newAuthorization.Scopes,
note = newAuthorization.Note,
note_url = newAuthorization.NoteUrl,
fingerprint = newAuthorization.Fingerprint
};
var endpoint = ApiUrls.Authorizations();
return ApiConnection.Post(endpoint, requestData, null, null, twoFactorAuthenticationCode);
}
///
/// Creates a new authorization for the specified OAuth application if an authorization for that application doesn’t already
/// exist for the user; otherwise, returns the user’s existing authorization for that application.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// Client Id of the OAuth application for the token
/// The client secret
/// Describes the new authorization to create
///
/// Thrown when the current user does not have permission to make this request.
///
///
/// Thrown when the current account has two-factor authentication enabled and an authentication code is required.
///
/// Thrown when a general API error occurs.
/// The created .
[ManualRoute("PUT", "/authorizations/clients/{id}")]
public Task GetOrCreateApplicationAuthentication(
string clientId,
string clientSecret,
NewAuthorization newAuthorization)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
Ensure.ArgumentNotNullOrEmptyString(clientSecret, nameof(clientSecret));
Ensure.ArgumentNotNull(newAuthorization, nameof(newAuthorization));
var requestData = new
{
client_secret = clientSecret,
scopes = newAuthorization.Scopes,
note = newAuthorization.Note,
note_url = newAuthorization.NoteUrl,
fingerprint = newAuthorization.Fingerprint
};
var endpoint = ApiUrls.AuthorizationsForClient(clientId);
return ApiConnection.Put(endpoint, requestData);
}
///
/// Creates a new authorization for the specified OAuth application if an authorization for that application doesn’t already
/// exist for the user; otherwise, returns the user’s existing authorization for that application.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// Client Id of the OAuth application for the token
/// The client secret
/// Describes the new authorization to create
/// The two-factor authentication code in response to the current user's previous challenge
///
/// Thrown when the current user does not have permission to make this request.
///
///
/// Thrown when the current account has two-factor authentication enabled and an authentication code is required.
///
/// Thrown when a general API error occurs.
/// The created .
[ManualRoute("PUT", "/authorizations/clients/{id}")]
public async Task GetOrCreateApplicationAuthentication(
string clientId,
string clientSecret,
NewAuthorization newAuthorization,
string twoFactorAuthenticationCode)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
Ensure.ArgumentNotNullOrEmptyString(clientSecret, nameof(clientSecret));
Ensure.ArgumentNotNull(newAuthorization, nameof(newAuthorization));
Ensure.ArgumentNotNullOrEmptyString(twoFactorAuthenticationCode, nameof(twoFactorAuthenticationCode));
var requestData = new
{
client_secret = clientSecret,
scopes = newAuthorization.Scopes,
note = newAuthorization.Note,
note_url = newAuthorization.NoteUrl,
fingerprint = newAuthorization.Fingerprint
};
try
{
var endpoint = ApiUrls.AuthorizationsForClient(clientId);
return await ApiConnection.Put(endpoint, requestData, twoFactorAuthenticationCode).ConfigureAwait(false);
}
catch (AuthorizationException e)
{
throw new TwoFactorChallengeFailedException(twoFactorAuthenticationCode, e);
}
}
///
/// Checks the validity of an OAuth token without running afoul of normal rate limits for failed login attempts.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// Client Id of the OAuth application for the token
/// The OAuth token to check
/// The valid .
[ManualRoute("POST", "/applications/{client_id}/token")]
public Task CheckApplicationAuthentication(string clientId, string accessToken)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
Ensure.ArgumentNotNullOrEmptyString(accessToken, nameof(accessToken));
var requestData = new
{
access_token = accessToken
};
var endpoint = ApiUrls.ApplicationAuthorization(clientId);
return ApiConnection.Post(endpoint, requestData);
}
///
/// Resets a valid OAuth token for an OAuth application without end user involvement.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// ClientID of the OAuth application for the token
/// The OAuth token to reset
/// The valid with a new OAuth token
[ManualRoute("PATCH", "/applications/{client_id}/token")]
public Task ResetApplicationAuthentication(string clientId, string accessToken)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
Ensure.ArgumentNotNullOrEmptyString(accessToken, nameof(accessToken));
var requestData = new
{
access_token = accessToken
};
var endpoint = ApiUrls.ApplicationAuthorization(clientId);
return ApiConnection.Patch(endpoint, requestData);
}
///
/// Revokes a single OAuth token for an OAuth application.
///
///
/// This method requires authentication.
/// See the API documentation for more information.
///
/// ClientID of the OAuth application for the token
/// The OAuth token to revoke
/// A for the request's execution.
[ManualRoute("DELETE", "/applications/{client_id}/token")]
public Task RevokeApplicationAuthentication(string clientId, string accessToken)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, nameof(clientId));
Ensure.ArgumentNotNullOrEmptyString(accessToken, nameof(accessToken));
var requestData = new
{
access_token = accessToken
};
var endpoint = ApiUrls.ApplicationAuthorization(clientId);
return ApiConnection.Delete(endpoint, requestData);
}
///
/// Updates the specified .
///
///
/// This method requires authentication.
/// See the API
/// documentation for more details.
///
/// Id of the to update
/// Describes the changes to make to the authorization
///
/// Thrown when the current user does not have permission to make the request.
///
/// Thrown when a general API error occurs.
/// The updated .
[ManualRoute("PATCH", "/authorizations/{id}")]
public Task Update(int authorizationId, AuthorizationUpdate authorizationUpdate)
{
Ensure.ArgumentNotNull(authorizationUpdate, nameof(authorizationUpdate));
return ApiConnection.Patch(
ApiUrls.Authorizations(authorizationId),
authorizationUpdate);
}
///
/// Deletes the specified .
///
///
/// This method requires authentication.
/// See the API
/// documentation for more details.
///
/// The system-wide Id of the authorization to delete
///
/// Thrown when the current user does not have permission to make the request.
///
/// Thrown when a general API error occurs.
/// A for the request's execution.
[ManualRoute("DELETE", "/authorizations/{id}")]
public Task Delete(int authorizationId)
{
return ApiConnection.Delete(ApiUrls.Authorizations(authorizationId));
}
///
/// Deletes the specified .
///
///
/// This method requires authentication.
/// See the API
/// documentation for more details.
///
/// The system-wide Id of the authorization to delete
/// Two factor authorization code
///
/// Thrown when the current user does not have permission to make the request.
///
/// Thrown when a general API error occurs.
/// A for the request's execution.
[ManualRoute("DELETE", "/authorizations/{id}")]
public Task Delete(int authorizationId, string twoFactorAuthenticationCode)
{
return ApiConnection.Delete(ApiUrls.Authorizations(authorizationId), twoFactorAuthenticationCode);
}
}
}