Skip to content

Commit

Permalink
Merge pull request #1 from adriangodong/master
Browse files Browse the repository at this point in the history
Merge Adrian Godong work into my master branch.
  • Loading branch information
itaibh authored Jan 8, 2018
2 parents 9c27c00 + 33d659e commit cd70453
Show file tree
Hide file tree
Showing 18 changed files with 188 additions and 8 deletions.
9 changes: 9 additions & 0 deletions Octokit.Reactive/Clients/IObservableAccessTokensClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Octokit.Reactive
{
public interface IObservableAccessTokensClient
{
IObservable<AccessToken> Create(int installationId);
}
}
7 changes: 7 additions & 0 deletions Octokit.Reactive/Clients/IObservableInstallationsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Octokit.Reactive
{
public interface IObservableInstallationsClient
{
IObservableAccessTokensClient AccessTokens { get; }
}
}
24 changes: 24 additions & 0 deletions Octokit.Reactive/Clients/ObservableAccessTokensClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Reactive.Threading.Tasks;

namespace Octokit.Reactive
{
class ObservableAccessTokensClient : IObservableAccessTokensClient
{
readonly IAccessTokensClient _client;
readonly IConnection _connection;

public ObservableAccessTokensClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");

_client = client.Installations.AccessTokens;
_connection = client.Connection;
}

public IObservable<AccessToken> Create(int installationId)
{
return _client.Create(installationId).ToObservable();
}
}
}
19 changes: 19 additions & 0 deletions Octokit.Reactive/Clients/ObservableInstallationsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Octokit.Reactive
{
class ObservableInstallationsClient : IObservableInstallationsClient
{
readonly IInstallationsClient _client;
readonly IConnection _connection;

public ObservableInstallationsClient(IGitHubClient client)
{
Ensure.ArgumentNotNull(client, "client");

_client = client.Installations;
_connection = client.Connection;
AccessTokens = new ObservableAccessTokensClient(client);
}

public IObservableAccessTokensClient AccessTokens { get; }
}
}
1 change: 1 addition & 0 deletions Octokit.Reactive/IObservableGitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface IObservableGitHubClient : IApiInfoProvider

IObservableAuthorizationsClient Authorization { get; }
IObservableActivitiesClient Activity { get; }
IObservableInstallationsClient Installations { get; }
IObservableIssuesClient Issue { get; }
IObservableMiscellaneousClient Miscellaneous { get; }
IObservableOauthClient Oauth { get; }
Expand Down
2 changes: 2 additions & 0 deletions Octokit.Reactive/ObservableGitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ObservableGitHubClient(IGitHubClient gitHubClient)
_gitHubClient = gitHubClient;
Authorization = new ObservableAuthorizationsClient(gitHubClient);
Activity = new ObservableActivitiesClient(gitHubClient);
Installations = new ObservableInstallationsClient(gitHubClient);
Issue = new ObservableIssuesClient(gitHubClient);
Miscellaneous = new ObservableMiscellaneousClient(gitHubClient);
Oauth = new ObservableOauthClient(gitHubClient);
Expand Down Expand Up @@ -69,6 +70,7 @@ public void SetRequestTimeout(TimeSpan timeout)

public IObservableAuthorizationsClient Authorization { get; private set; }
public IObservableActivitiesClient Activity { get; private set; }
public IObservableInstallationsClient Installations { get; private set; }
public IObservableIssuesClient Issue { get; private set; }
public IObservableMiscellaneousClient Miscellaneous { get; private set; }
public IObservableOauthClient Oauth { get; private set; }
Expand Down
6 changes: 5 additions & 1 deletion Octokit/Authentication/AuthenticationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public enum AuthenticationType
/// <summary>
/// Delegated access to a third party
/// </summary>
Oauth
Oauth,
/// <summary>
/// Credential for GitHub App using signed JWT
/// </summary>
Bearer
}
}
3 changes: 2 additions & 1 deletion Octokit/Authentication/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Authenticator
{
{ AuthenticationType.Anonymous, new AnonymousAuthenticator() },
{ AuthenticationType.Basic, new BasicAuthenticator() },
{ AuthenticationType.Oauth, new TokenAuthenticator() }
{ AuthenticationType.Oauth, new TokenAuthenticator() },
{ AuthenticationType.Bearer, new BearerTokenAuthenticator() }
};

public Authenticator(ICredentialStore credentialStore)
Expand Down
23 changes: 23 additions & 0 deletions Octokit/Authentication/BearerTokenAuthenticator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Globalization;

namespace Octokit.Internal
{
class BearerTokenAuthenticator: IAuthenticationHandler
{
public void Authenticate(IRequest request, Credentials credentials)
{
Ensure.ArgumentNotNull(request, "request");
Ensure.ArgumentNotNull(credentials, "credentials");
Ensure.ArgumentNotNull(credentials.Password, "credentials.Password");

if (credentials.Login != null)
{
throw new InvalidOperationException("The Login is not null for a token authentication request. You " +
"probably did something wrong.");
}

request.Headers["Authorization"] = string.Format(CultureInfo.InvariantCulture, "Bearer {0}", credentials.Password);
}
}
}
18 changes: 18 additions & 0 deletions Octokit/Clients/AccessTokensClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Threading.Tasks;

namespace Octokit
{
class AccessTokensClient : ApiClient, IAccessTokensClient
{
private const string AcceptHeader = "application/vnd.github.machine-man-preview+json";

public AccessTokensClient(IApiConnection apiConnection) : base(apiConnection)
{
}

public Task<AccessToken> Create(int installationId)
{
return ApiConnection.Post<AccessToken>(ApiUrls.AccessTokens(installationId), null, AcceptHeader);
}
}
}
9 changes: 9 additions & 0 deletions Octokit/Clients/IAccessTokensClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace Octokit
{
public interface IAccessTokensClient
{
Task<AccessToken> Create(int installationId);
}
}
7 changes: 7 additions & 0 deletions Octokit/Clients/IInstallationsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Octokit
{
public interface IInstallationsClient
{
IAccessTokensClient AccessTokens { get; }
}
}
12 changes: 12 additions & 0 deletions Octokit/Clients/InstallationsClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Octokit
{
class InstallationsClient : ApiClient, IInstallationsClient
{
public InstallationsClient(IApiConnection apiConnection) : base(apiConnection)
{
AccessTokens = new AccessTokensClient(apiConnection);
}

public IAccessTokensClient AccessTokens { get; private set; }
}
}
4 changes: 4 additions & 0 deletions Octokit/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public GitHubClient(IConnection connection)
Enterprise = new EnterpriseClient(apiConnection);
Gist = new GistsClient(apiConnection);
Git = new GitDatabaseClient(apiConnection);
Installations = new InstallationsClient(apiConnection);
Issue = new IssuesClient(apiConnection);
Migration = new MigrationClient(apiConnection);
Miscellaneous = new MiscellaneousClient(connection);
Expand Down Expand Up @@ -171,6 +172,9 @@ public Uri BaseAddress
/// </remarks>
public IActivitiesClient Activity { get; private set; }

public IInstallationsClient Installations { get; private set; }


/// <summary>
/// Access GitHub's Issue API.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Octokit/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ public static Uri NotificationSubscription(int id)
return "notifications/threads/{0}/subscription".FormatUri(id);
}

public static Uri AccessTokens(int installationId)
{
return "installations/{0}/access_tokens".FormatUri(installationId);
}

/// <summary>
/// Returns the <see cref="Uri"/> that returns all of the issues across all the authenticated user’s visible
/// repositories including owned repositories, member repositories, and organization repositories:
Expand Down
16 changes: 10 additions & 6 deletions Octokit/Http/Credentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ private Credentials()
AuthenticationType = AuthenticationType.Anonymous;
}

public Credentials(string token)
public Credentials(string token) : this(token, AuthenticationType.Oauth)
{
Ensure.ArgumentNotNullOrEmptyString(token, "token");

Login = null;
Password = token;
AuthenticationType = AuthenticationType.Oauth;
}

public Credentials(string login, string password)
Expand All @@ -32,6 +27,15 @@ public Credentials(string login, string password)
AuthenticationType = AuthenticationType.Basic;
}

public Credentials(string token, AuthenticationType authenticationType)
{
Ensure.ArgumentNotNullOrEmptyString(token, "token");

Login = null;
Password = token;
AuthenticationType = authenticationType;
}

public string Login
{
get;
Expand Down
2 changes: 2 additions & 0 deletions Octokit/IGitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public interface IGitHubClient : IApiInfoProvider
/// </remarks>
IActivitiesClient Activity { get; }

IInstallationsClient Installations { get; }

/// <summary>
/// Access GitHub's Issue API.
/// </summary>
Expand Down
29 changes: 29 additions & 0 deletions Octokit/Models/Response/AccessToken.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Diagnostics;
using System.Globalization;

namespace Octokit
{
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class AccessToken
{
public AccessToken() { }

public AccessToken(string token, DateTime expiresAt)
{
Token = token;
ExpiresAt = expiresAt;
}

public string Token { get; protected set; }
public DateTime ExpiresAt { get; protected set; }

internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "Token: {0}, ExpiresAt: {1}", Token, ExpiresAt);
}
}
}
}

0 comments on commit cd70453

Please sign in to comment.