using System; using System.Reactive; using System.Reactive.Threading.Tasks; using Octokit.Reactive.Internal; namespace Octokit.Reactive { public class ObservableAuthorizationsClient : IObservableAuthorizationsClient { readonly IAuthorizationsClient _client; readonly IConnection _connection; public ObservableAuthorizationsClient(IGitHubClient client) { Ensure.ArgumentNotNull(client, "client"); _client = client.Authorization; _connection = client.Connection; } /// /// Get all s for the authenticated user. This method requires basic auth. /// /// /// See API documentation for more /// details. /// /// A list of s for the authenticated user. public IObservable GetAll() { return GetAll(ApiOptions.None); } /// /// Get all s for the authenticated user. This method requires basic auth. /// /// /// See API documentation for more /// details. /// /// Options for changing the API response /// A list of s for the authenticated user. public IObservable GetAll(ApiOptions options) { Ensure.ArgumentNotNull(options, "options"); return _connection.GetAndFlattenAllPages(ApiUrls.Authorizations(), options); } /// /// Get a specific for the authenticated user. This method requires basic auth. /// /// /// See API documentation for /// more details. /// /// The id of the /// An public IObservable Get(int id) { return _client.Get(id).ToObservable(); } /// /// 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 . public IObservable Create(NewAuthorization newAuthorization) { Ensure.ArgumentNotNull(newAuthorization, "newAuthorization"); return _client.Create(newAuthorization).ToObservable(); } /// /// 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 . public IObservable Create( NewAuthorization newAuthorization, string twoFactorAuthenticationCode) { Ensure.ArgumentNotNull(newAuthorization, "newAuthorization"); Ensure.ArgumentNotNullOrEmptyString(twoFactorAuthenticationCode, "twoFactorAuthenticationCode"); return _client.Create(newAuthorization, twoFactorAuthenticationCode).ToObservable(); } /// /// 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 . public IObservable Create( string clientId, string clientSecret, NewAuthorization newAuthorization) { Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId"); Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret"); Ensure.ArgumentNotNull(newAuthorization, "newAuthorization"); return _client.Create(clientId, clientSecret, newAuthorization).ToObservable(); } /// /// 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 . public IObservable Create( string clientId, string clientSecret, NewAuthorization newAuthorization, string twoFactorAuthenticationCode) { Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId"); Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret"); Ensure.ArgumentNotNull(newAuthorization, "newAuthorization"); Ensure.ArgumentNotNullOrEmptyString(twoFactorAuthenticationCode, "twoFactorAuthenticationCode"); return _client.Create(clientId, clientSecret, newAuthorization, twoFactorAuthenticationCode).ToObservable(); } /// /// This method will create a new authorization for the specified OAuth application, only if an authorization /// for that application doesn’t already exist for the user. It returns the user’s token for the application /// if one exists. Otherwise, it creates one. /// /// /// See API /// documentation for more details. /// /// Client ID for the OAuth application that is requesting the token /// The client secret /// Defines the scopes and metadata for the token /// Thrown when the user does not have permission to make /// this request. Check /// Thrown when the current account has two-factor /// authentication enabled. /// public IObservable GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization) { Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId"); Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret"); Ensure.ArgumentNotNull(newAuthorization, "newAuthorization"); return _client.GetOrCreateApplicationAuthentication(clientId, clientSecret, newAuthorization) .ToObservable(); } /// /// This method will create a new authorization for the specified OAuth application, only if an authorization /// for that application doesn’t already exist for the user. It returns the user’s token for the application /// if one exists. Otherwise, it creates one. /// /// /// See API /// documentation for more details. /// /// Client ID for the OAuth application that is requesting the token /// The client secret /// Defines the scopes and metadata for the token /// The two-factor authentication code provided by the user /// Thrown when the user does not have permission to make /// this request. Check /// Thrown when the two-factor code is not /// valid. /// public IObservable GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization, string twoFactorAuthenticationCode) { Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId"); Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret"); Ensure.ArgumentNotNull(newAuthorization, "newAuthorization"); Ensure.ArgumentNotNullOrEmptyString(twoFactorAuthenticationCode, "twoFactorAuthenticationCode"); return _client.GetOrCreateApplicationAuthentication( clientId, clientSecret, newAuthorization, twoFactorAuthenticationCode) .ToObservable(); } /// /// 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 . public IObservable CheckApplicationAuthentication(string clientId, string accessToken) { Ensure.ArgumentNotNullOrEmptyString("clientId", clientId); Ensure.ArgumentNotNullOrEmptyString("accessToken", accessToken); return _client.CheckApplicationAuthentication(clientId, accessToken) .ToObservable(); } /// /// 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 public IObservable ResetApplicationAuthentication(string clientId, string accessToken) { Ensure.ArgumentNotNullOrEmptyString("clientId", clientId); Ensure.ArgumentNotNullOrEmptyString("accessToken", accessToken); return _client.ResetApplicationAuthentication(clientId, accessToken) .ToObservable(); } /// /// 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 /// public IObservable RevokeApplicationAuthentication(string clientId, string accessToken) { Ensure.ArgumentNotNullOrEmptyString("clientId", clientId); Ensure.ArgumentNotNullOrEmptyString("accessToken", accessToken); return _client.RevokeApplicationAuthentication(clientId, accessToken) .ToObservable(); } /// /// Revokes every 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 /// [Obsolete("This feature is no longer supported in the GitHub API and will be removed in a future release")] public IObservable RevokeAllApplicationAuthentications(string clientId) { Ensure.ArgumentNotNullOrEmptyString("clientId", clientId); return _client.RevokeAllApplicationAuthentications(clientId) .ToObservable(); } /// /// Update the specified by the id. /// /// The id of the /// The changes to make to the authorization /// public IObservable Update(int id, AuthorizationUpdate authorizationUpdate) { Ensure.ArgumentNotNull(authorizationUpdate, "authorizationUpdate"); return _client.Update(id, authorizationUpdate).ToObservable(); } /// /// 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. public IObservable Delete(int id) { return _client.Delete(id).ToObservable(); } /// /// 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. public IObservable Delete(int id, string twoFactorAuthenticationCode) { return _client.Delete(id, twoFactorAuthenticationCode).ToObservable(); } } }