From d613cef1967f2b92954f16acb0386d4a9c4dbc44 Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Fri, 2 Jan 2015 13:09:50 +0930 Subject: [PATCH] introduce ApplicationAuthorization which contains raw token result --- .../IObservableAuthorizationsClient.cs | 6 ++-- .../Clients/ObservableAuthorizationsClient.cs | 6 ++-- .../Helpers/AuthorizationExtensions.cs | 4 +-- .../Clients/AuthorizationsClientTests.cs | 14 ++++---- .../Reactive/AuthorizationExtensionsTests.cs | 14 ++++---- Octokit/Clients/AuthorizationsClient.cs | 32 +++++++++---------- Octokit/Clients/IAuthorizationsClient.cs | 6 ++-- Octokit/Helpers/AuthorizationExtensions.cs | 2 +- .../Response/ApplicationAuthorization.cs | 23 +++++++++++++ Octokit/Models/Response/Authorization.cs | 6 ---- Octokit/Octokit-Mono.csproj | 1 + Octokit/Octokit-MonoAndroid.csproj | 1 + Octokit/Octokit-Monotouch.csproj | 1 + Octokit/Octokit-Portable.csproj | 1 + Octokit/Octokit-netcore45.csproj | 1 + Octokit/Octokit.csproj | 1 + 16 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 Octokit/Models/Response/ApplicationAuthorization.cs diff --git a/Octokit.Reactive/Clients/IObservableAuthorizationsClient.cs b/Octokit.Reactive/Clients/IObservableAuthorizationsClient.cs index 2283f454..0bc48385 100644 --- a/Octokit.Reactive/Clients/IObservableAuthorizationsClient.cs +++ b/Octokit.Reactive/Clients/IObservableAuthorizationsClient.cs @@ -48,7 +48,7 @@ namespace Octokit.Reactive /// Thrown when the current account has two-factor /// authentication enabled. /// - IObservable GetOrCreateApplicationAuthentication( + IObservable GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization); @@ -71,7 +71,7 @@ namespace Octokit.Reactive /// Thrown when the two-factor code is not /// valid. /// - IObservable GetOrCreateApplicationAuthentication( + IObservable GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization, @@ -82,7 +82,7 @@ namespace Octokit.Reactive /// /// Information about the new authorization to create /// - IObservable Create(NewAuthorization newAuthorization); + IObservable Create(NewAuthorization newAuthorization); /// /// Update the specified by the id. diff --git a/Octokit.Reactive/Clients/ObservableAuthorizationsClient.cs b/Octokit.Reactive/Clients/ObservableAuthorizationsClient.cs index 9459be6e..1999b9d8 100644 --- a/Octokit.Reactive/Clients/ObservableAuthorizationsClient.cs +++ b/Octokit.Reactive/Clients/ObservableAuthorizationsClient.cs @@ -62,7 +62,7 @@ namespace Octokit.Reactive /// Thrown when the current account has two-factor /// authentication enabled. /// - public IObservable GetOrCreateApplicationAuthentication( + public IObservable GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization) @@ -93,7 +93,7 @@ namespace Octokit.Reactive /// Thrown when the two-factor code is not /// valid. /// - public IObservable GetOrCreateApplicationAuthentication( + public IObservable GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization, @@ -117,7 +117,7 @@ namespace Octokit.Reactive /// /// Information about the new authorization to create /// - public IObservable Create(NewAuthorization newAuthorization) + public IObservable Create(NewAuthorization newAuthorization) { Ensure.ArgumentNotNull(newAuthorization, "authorization"); diff --git a/Octokit.Reactive/Helpers/AuthorizationExtensions.cs b/Octokit.Reactive/Helpers/AuthorizationExtensions.cs index 645a9df0..36a110eb 100644 --- a/Octokit.Reactive/Helpers/AuthorizationExtensions.cs +++ b/Octokit.Reactive/Helpers/AuthorizationExtensions.cs @@ -28,7 +28,7 @@ namespace Octokit /// Callback used to retrieve the two-factor authentication code /// from the user /// - public static IObservable GetOrCreateApplicationAuthentication( + public static IObservable GetOrCreateApplicationAuthentication( this IObservableAuthorizationsClient authorizationsClient, string clientId, string clientSecret, @@ -42,7 +42,7 @@ namespace Octokit Ensure.ArgumentNotNull(newAuthorization, "authorization"); return authorizationsClient.GetOrCreateApplicationAuthentication(clientId, clientSecret, newAuthorization) - .Catch(exception => twoFactorChallengeHandler(exception) + .Catch(exception => twoFactorChallengeHandler(exception) .SelectMany(result => result.ResendCodeRequested ? authorizationsClient.GetOrCreateApplicationAuthentication( diff --git a/Octokit.Tests/Clients/AuthorizationsClientTests.cs b/Octokit.Tests/Clients/AuthorizationsClientTests.cs index 8f456423..43d72993 100644 --- a/Octokit.Tests/Clients/AuthorizationsClientTests.cs +++ b/Octokit.Tests/Clients/AuthorizationsClientTests.cs @@ -83,7 +83,7 @@ namespace Octokit.Tests.Clients authEndpoint.Create(new NewAuthorization()); - client.Received().Post(Arg.Is(u => u.ToString() == "authorizations") + client.Received().Post(Arg.Is(u => u.ToString() == "authorizations") , Args.NewAuthorization); } } @@ -113,7 +113,7 @@ namespace Octokit.Tests.Clients authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data); - client.Received().Put(Arg.Is(u => u.ToString() == "authorizations/clients/clientId"), + client.Received().Put(Arg.Is(u => u.ToString() == "authorizations/clients/clientId"), Args.Object); } @@ -126,7 +126,7 @@ namespace Octokit.Tests.Clients authEndpoint.GetOrCreateApplicationAuthentication("clientId", "secret", data, "two-factor"); - client.Received().Put( + client.Received().Put( Arg.Is(u => u.ToString() == "authorizations/clients/clientId"), Args.Object, "two-factor"); @@ -137,8 +137,8 @@ namespace Octokit.Tests.Clients { var data = new NewAuthorization(); var client = Substitute.For(); - client.Put(Args.Uri, Args.Object, Args.String) - .ThrowsAsync( + client.Put(Args.Uri, Args.Object, Args.String) + .ThrowsAsync( new AuthorizationException( new ApiResponse { StatusCode = HttpStatusCode.Unauthorized })); var authEndpoint = new AuthorizationsClient(client); @@ -159,7 +159,7 @@ namespace Octokit.Tests.Clients "secret", Arg.Any(), "two-factor-code") - .Returns(Task.Factory.StartNew(() => new Authorization {Token = "xyz"})); + .Returns(Task.Factory.StartNew(() => new ApplicationAuthorization { Token = "xyz" })); var result = await client.GetOrCreateApplicationAuthentication("clientId", "secret", @@ -191,7 +191,7 @@ namespace Octokit.Tests.Clients "secret", Arg.Any(), "two-factor-code") - .Returns(Task.Factory.StartNew(() => new Authorization { Token = "OAUTHSECRET" })); + .Returns(Task.Factory.StartNew(() => new ApplicationAuthorization { Token = "OAUTHSECRET" })); var result = await client.GetOrCreateApplicationAuthentication("clientId", "secret", diff --git a/Octokit.Tests/Reactive/AuthorizationExtensionsTests.cs b/Octokit.Tests/Reactive/AuthorizationExtensionsTests.cs index 0e3a9364..29f80080 100644 --- a/Octokit.Tests/Reactive/AuthorizationExtensionsTests.cs +++ b/Octokit.Tests/Reactive/AuthorizationExtensionsTests.cs @@ -16,11 +16,11 @@ namespace Octokit.Tests.Reactive { var firstResponse = new TwoFactorRequiredException(TwoFactorType.AuthenticatorApp); var twoFactorChallengeResult = new TwoFactorChallengeResult("two-factor-code"); - var secondResponse = new Authorization {Token = "OAUTHSECRET"}; + var secondResponse = new ApplicationAuthorization { Token = "OAUTHSECRET" }; var client = Substitute.For(); client.GetOrCreateApplicationAuthentication(Args.String, Args.String, Args.NewAuthorization) - .Returns(Observable.Throw(firstResponse)); + .Returns(Observable.Throw(firstResponse)); client.GetOrCreateApplicationAuthentication( Args.String, Args.String, @@ -51,11 +51,11 @@ namespace Octokit.Tests.Reactive TwoFactorChallengeResult.RequestResendCode, new TwoFactorChallengeResult("two-factor-code") }); - var secondResponse = new Authorization { Token = "OAUTHSECRET" }; + var secondResponse = new ApplicationAuthorization { Token = "OAUTHSECRET" }; var client = Substitute.For(); client.GetOrCreateApplicationAuthentication(Args.String, Args.String, Args.NewAuthorization) - .Returns(Observable.Throw(firstResponse)); + .Returns(Observable.Throw(firstResponse)); client.GetOrCreateApplicationAuthentication( Args.String, Args.String, @@ -90,13 +90,13 @@ namespace Octokit.Tests.Reactive var data = new NewAuthorization(); var client = Substitute.For(); client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any()) - .Returns(Observable.Throw(new TwoFactorRequiredException())); + .Returns(Observable.Throw(new TwoFactorRequiredException())); client.GetOrCreateApplicationAuthentication("clientId", "secret", Arg.Any(), "wrong-code") - .Returns(Observable.Throw(twoFactorFailedException)); - var observer = Substitute.For>(); + .Returns(Observable.Throw(twoFactorFailedException)); + var observer = Substitute.For>(); client.GetOrCreateApplicationAuthentication( "clientId", diff --git a/Octokit/Clients/AuthorizationsClient.cs b/Octokit/Clients/AuthorizationsClient.cs index de603cdb..4061bf8b 100644 --- a/Octokit/Clients/AuthorizationsClient.cs +++ b/Octokit/Clients/AuthorizationsClient.cs @@ -10,7 +10,7 @@ namespace Octokit /// A client for GitHub's OAuth API. /// /// - /// See the OAuth API documentation for more details. + /// See the OAuth API documentation for more details. /// public class AuthorizationsClient : ApiClient, IAuthorizationsClient { @@ -29,7 +29,7 @@ namespace Octokit /// /// /// This method requires authentication. - /// See the API documentation for more information. + /// See the API documentation for more information. /// /// /// Thrown when the current user does not have permission to make the request. @@ -46,7 +46,7 @@ namespace Octokit /// /// /// This method requires authentication. - /// See the API documentation for more information. + /// See the API documentation for more information. /// /// The ID of the to get /// @@ -65,7 +65,7 @@ namespace Octokit /// /// /// This method requires authentication. - /// See the API documentation for more information. + /// See the API documentation for more information. /// /// Client ID of the OAuth application for the token /// The client secret @@ -78,7 +78,7 @@ namespace Octokit /// /// Thrown when a general API error occurs. /// The created . - public Task GetOrCreateApplicationAuthentication( + public Task GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization) @@ -99,13 +99,13 @@ namespace Octokit { // use classic API var endpoint = ApiUrls.AuthorizationsForClient(clientId); - return ApiConnection.Put(endpoint, requestData); + return ApiConnection.Put(endpoint, requestData); } else { // use new API var endpoint = ApiUrls.AuthorizationsForClient(clientId, newAuthorization.Fingerprint); - return ApiConnection.Put(endpoint, requestData, null, previewAcceptsHeader); + return ApiConnection.Put(endpoint, requestData, null, previewAcceptsHeader); } } @@ -115,7 +115,7 @@ namespace Octokit /// /// /// This method requires authentication. - /// See the API documentation for more information. + /// See the API documentation for more information. /// /// Client ID of the OAuth application for the token /// The client secret @@ -129,7 +129,7 @@ namespace Octokit /// /// Thrown when a general API error occurs. /// The created . - public async Task GetOrCreateApplicationAuthentication( + public async Task GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization, @@ -154,7 +154,7 @@ namespace Octokit { // use classic API var endpoint = ApiUrls.AuthorizationsForClient(clientId); - return await ApiConnection.Put( + return await ApiConnection.Put( endpoint, requestData, twoFactorAuthenticationCode); @@ -163,7 +163,7 @@ namespace Octokit { // use new API var endpoint = ApiUrls.AuthorizationsForClient(clientId, newAuthorization.Fingerprint); - return await ApiConnection.Put( + return await ApiConnection.Put( endpoint, requestData, twoFactorAuthenticationCode, @@ -181,7 +181,7 @@ namespace Octokit /// /// /// This method requires authentication. - /// See the API + /// See the API /// documentation for more details. /// /// ID of the to update @@ -215,7 +215,7 @@ namespace Octokit /// /// /// This method requires authentication. - /// See the API documentation for more information. + /// See the API documentation for more information. /// /// Describes the new authorization to create /// @@ -223,11 +223,11 @@ namespace Octokit /// /// Thrown when a general API error occurs. /// The created . - public Task Create(NewAuthorization newAuthorization) + public Task Create(NewAuthorization newAuthorization) { Ensure.ArgumentNotNull(newAuthorization, "newAuthorization"); - return ApiConnection.Post(ApiUrls.Authorizations(), newAuthorization); + return ApiConnection.Post(ApiUrls.Authorizations(), newAuthorization); } /// @@ -235,7 +235,7 @@ namespace Octokit /// /// /// This method requires authentication. - /// See the API + /// See the API /// documentation for more details. /// /// The system-wide ID of the authorization to delete diff --git a/Octokit/Clients/IAuthorizationsClient.cs b/Octokit/Clients/IAuthorizationsClient.cs index 9195aa44..edb2dbc3 100644 --- a/Octokit/Clients/IAuthorizationsClient.cs +++ b/Octokit/Clients/IAuthorizationsClient.cs @@ -66,7 +66,7 @@ namespace Octokit /// /// Thrown when a general API error occurs. /// The created . - Task GetOrCreateApplicationAuthentication( + Task GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization); @@ -91,7 +91,7 @@ namespace Octokit /// /// Thrown when a general API error occurs. /// The created . - Task GetOrCreateApplicationAuthentication( + Task GetOrCreateApplicationAuthentication( string clientId, string clientSecret, NewAuthorization newAuthorization, @@ -110,7 +110,7 @@ namespace Octokit /// /// Thrown when a general API error occurs. /// The created . - Task Create(NewAuthorization newAuthorization); + Task Create(NewAuthorization newAuthorization); /// /// Updates the specified . diff --git a/Octokit/Helpers/AuthorizationExtensions.cs b/Octokit/Helpers/AuthorizationExtensions.cs index c474a6a3..0d115ab9 100644 --- a/Octokit/Helpers/AuthorizationExtensions.cs +++ b/Octokit/Helpers/AuthorizationExtensions.cs @@ -30,7 +30,7 @@ namespace Octokit /// Callback used to retrieve the two-factor authentication code /// from the user /// - public static async Task GetOrCreateApplicationAuthentication( + public static async Task GetOrCreateApplicationAuthentication( this IAuthorizationsClient authorizationsClient, string clientId, string clientSecret, diff --git a/Octokit/Models/Response/ApplicationAuthorization.cs b/Octokit/Models/Response/ApplicationAuthorization.cs new file mode 100644 index 00000000..07db030a --- /dev/null +++ b/Octokit/Models/Response/ApplicationAuthorization.cs @@ -0,0 +1,23 @@ +using System; +using System.Diagnostics; +using System.Globalization; + +namespace Octokit +{ + /// + /// Represents an oauth access given to a particular application. + /// + [DebuggerDisplay("{DebuggerDisplay,nq}")] + public class ApplicationAuthorization : Authorization + { + /// + /// The oauth token (be careful with these, they are like passwords!). + /// + /// + /// This will return only return a value the first time + /// the authorization is created. All subsequent API calls + /// (for example, 'get' for an authorization) will return `null` + /// + public string Token { get; set; } + } +} \ No newline at end of file diff --git a/Octokit/Models/Response/Authorization.cs b/Octokit/Models/Response/Authorization.cs index 267bf15e..b1aadb12 100644 --- a/Octokit/Models/Response/Authorization.cs +++ b/Octokit/Models/Response/Authorization.cs @@ -26,12 +26,6 @@ namespace Octokit /// public Application Application { get; set; } - /// - /// The oauth token (be careful with these, they are like passwords!). - /// - [Obsolete("Now returns empty string, to be deprecated for security reasons")] - public string Token { get; set; } - /// /// The last eight characters of the user's token /// diff --git a/Octokit/Octokit-Mono.csproj b/Octokit/Octokit-Mono.csproj index 71737447..2dd2bed0 100644 --- a/Octokit/Octokit-Mono.csproj +++ b/Octokit/Octokit-Mono.csproj @@ -358,6 +358,7 @@ + \ No newline at end of file diff --git a/Octokit/Octokit-MonoAndroid.csproj b/Octokit/Octokit-MonoAndroid.csproj index 58191d95..de7c9b79 100644 --- a/Octokit/Octokit-MonoAndroid.csproj +++ b/Octokit/Octokit-MonoAndroid.csproj @@ -370,6 +370,7 @@ + \ No newline at end of file diff --git a/Octokit/Octokit-Monotouch.csproj b/Octokit/Octokit-Monotouch.csproj index f6971503..f99c4511 100644 --- a/Octokit/Octokit-Monotouch.csproj +++ b/Octokit/Octokit-Monotouch.csproj @@ -365,6 +365,7 @@ + diff --git a/Octokit/Octokit-Portable.csproj b/Octokit/Octokit-Portable.csproj index ad418f8c..61c1a6f0 100644 --- a/Octokit/Octokit-Portable.csproj +++ b/Octokit/Octokit-Portable.csproj @@ -356,6 +356,7 @@ + diff --git a/Octokit/Octokit-netcore45.csproj b/Octokit/Octokit-netcore45.csproj index a4a0e111..dc3e519b 100644 --- a/Octokit/Octokit-netcore45.csproj +++ b/Octokit/Octokit-netcore45.csproj @@ -360,6 +360,7 @@ + diff --git a/Octokit/Octokit.csproj b/Octokit/Octokit.csproj index 12142dbf..bf0b0b30 100644 --- a/Octokit/Octokit.csproj +++ b/Octokit/Octokit.csproj @@ -89,6 +89,7 @@ +