Implement GetOrCreateApplicationAuthentication

Implements the endpoint for creating an application authorization token.
This commit is contained in:
Haacked
2013-10-09 16:28:59 -07:00
parent 9d71d406fa
commit 33ad79c0fe
19 changed files with 653 additions and 4 deletions
@@ -25,6 +25,59 @@ namespace Octokit.Reactive.Clients
{
return _client.Get(id).ToObservable();
}
/// <summary>
/// This method will create a new authorization for the specified OAuth application, only if an authorization
/// for that application doesnt already exist for the user. It returns the users token for the application
/// if one exists. Otherwise, it creates one.
/// </summary>
/// <param name="clientId">Client ID for the OAuth application that is requesting the token.</param>
/// <param name="clientSecret">The client secret</param>
/// <param name="authorization">Definse the scopes and metadata for the token</param>
/// <exception cref="AuthorizationException">Thrown when the user does not have permission to make
/// this request. Check </exception>
/// <returns></returns>
public IObservable<Authorization> GetOrCreateApplicationAuthentication(
string clientId,
string clientSecret,
AuthorizationUpdate authorization)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId");
Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret");
Ensure.ArgumentNotNull(authorization, "authorization");
return _client.GetOrCreateApplicationAuthentication(clientId, clientSecret, authorization)
.ToObservable();
}
/// <summary>
/// This method will create a new authorization for the specified OAuth application, only if an authorization
/// for that application doesnt already exist for the user. It returns the users token for the application
/// if one exists. Otherwise, it creates one.
/// </summary>
/// <param name="clientId">Client ID for the OAuth application that is requesting the token.</param>
/// <param name="clientSecret">The client secret</param>
/// <param name="authorization">Defines the scopes and metadata for the token</param>
/// <param name="twoFactorAuthenticationCode"></param>
/// <exception cref="AuthorizationException">Thrown when the user does not have permission to make
/// this request. Check </exception>
/// <exception cref="TwoFactorChallengeFailedException">Thrown when the two-factor code is not
/// valid.</exception>
/// <returns></returns>
public IObservable<Authorization> GetOrCreateApplicationAuthentication(
string clientId,
string clientSecret,
AuthorizationUpdate authorization,
string twoFactorAuthenticationCode)
{
Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId");
Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret");
Ensure.ArgumentNotNull(authorization, "authorization");
Ensure.ArgumentNotNullOrEmptyString(twoFactorAuthenticationCode, "twoFactorAuthenticationCode");
return _client.GetOrCreateApplicationAuthentication(clientId, clientSecret, authorization, twoFactorAuthenticationCode)
.ToObservable();
}
public IObservable<Authorization> Update(int id, AuthorizationUpdate authorization)
{
@@ -0,0 +1,37 @@
using System;
using System.Reactive.Linq;
using Octokit.Reactive;
namespace Octokit
{
public static class AuthorizationExtensions
{
public static IObservable<Authorization> GetOrCreateApplicationAuthentication(
this IObservableAuthorizationsClient authorizationsClient,
string clientId,
string clientSecret,
AuthorizationUpdate authorization,
Func<TwoFactorRequiredException, IObservable<TwoFactorChallengeResult>> twoFactorChallengeHandler
)
{
Ensure.ArgumentNotNull(authorizationsClient, "authorizationsClient");
Ensure.ArgumentNotNullOrEmptyString(clientId, "clientId");
Ensure.ArgumentNotNullOrEmptyString(clientSecret, "clientSecret");
Ensure.ArgumentNotNull(authorization, "authorization");
return authorizationsClient.GetOrCreateApplicationAuthentication(clientId, clientSecret, authorization)
.Catch<Authorization, TwoFactorRequiredException>(exception => twoFactorChallengeHandler(exception)
.SelectMany(result =>
result.ResendCodeRequested
? authorizationsClient.GetOrCreateApplicationAuthentication(
clientId,
clientSecret,
authorization,
twoFactorChallengeHandler)
: authorizationsClient.GetOrCreateApplicationAuthentication(clientId,
clientSecret,
authorization,
result.AuthenticationCode)));
}
}
}
@@ -13,6 +13,44 @@ namespace Octokit.Reactive
[SuppressMessage("Microsoft.Naming", "CA1716:IdentifiersShouldNotMatchKeywords", MessageId = "Get",
Justification = "It's fiiiine. It's fine. Trust us.")]
IObservable<Authorization> Get(int id);
/// <summary>
/// This method will create a new authorization for the specified OAuth application, only if an authorization
/// for that application doesnt already exist for the user. It returns the users token for the application
/// if one exists. Otherwise, it creates one.
/// </summary>
/// <param name="clientId">Client ID for the OAuth application that is requesting the token.</param>
/// <param name="clientSecret">The client secret</param>
/// <param name="authorization">Defines the scopes and metadata for the token</param>
/// <exception cref="AuthorizationException">Thrown when the user does not have permission to make
/// this request. Check </exception>
/// <exception cref="TwoFactorRequiredException">Thrown when the current account has two-factor
/// authentication enabled.</exception>
/// <returns></returns>
IObservable<Authorization> GetOrCreateApplicationAuthentication(
string clientId,
string clientSecret,
AuthorizationUpdate authorization);
/// <summary>
/// This method will create a new authorization for the specified OAuth application, only if an authorization
/// for that application doesnt already exist for the user. It returns the users token for the application
/// if one exists. Otherwise, it creates one.
/// </summary>
/// <param name="clientId">Client ID for the OAuth application that is requesting the token.</param>
/// <param name="clientSecret">The client secret</param>
/// <param name="authorization">Defines the scopes and metadata for the token</param>
/// <param name="twoFactorAuthenticationCode"></param>
/// <exception cref="AuthorizationException">Thrown when the user does not have permission to make
/// this request. Check </exception>
/// <exception cref="TwoFactorChallengeFailedException">Thrown when the two-factor code is not
/// valid.</exception>
/// <returns></returns>
IObservable<Authorization> GetOrCreateApplicationAuthentication(
string clientId,
string clientSecret,
AuthorizationUpdate authorization,
string twoFactorAuthenticationCode);
IObservable<Authorization> Update(int id, AuthorizationUpdate authorization);
IObservable<Authorization> Create(AuthorizationUpdate authorization);
IObservable<Unit> Delete(int id);
+1
View File
@@ -89,6 +89,7 @@
<Compile Include="Clients\ObservableRepositoriesClient.cs" />
<Compile Include="Clients\ObservableSshKeysClient.cs" />
<Compile Include="Clients\ObservableUsersClient.cs" />
<Compile Include="Helpers\AuthorizationExtensions.cs" />
<Compile Include="IObservableAuthorizationsClient.cs" />
<Compile Include="IObservableMiscellaneousClient.cs" />
<Compile Include="IObservableGitHubClient.cs" />