From 435e1e903a79ceba4e9ea44926a3b8263b7ebec5 Mon Sep 17 00:00:00 2001 From: Abraham Jonathan Date: Tue, 5 Feb 2019 11:15:44 +0100 Subject: [PATCH] Add existing oauth2Token support (#32) Some services (as Alexa skills) manage the authentication and just provide the app oauth2 token. I added the oauth2 existing token support in "generate token" process. --- src/Netatmo.Tests/CredentialManager.cs | 25 ++++++++++++++++++++++++- src/Netatmo/Client.cs | 5 +++++ src/Netatmo/CredentialManager.cs | 16 +++++++++++++++- src/Netatmo/IClient.cs | 1 + src/Netatmo/ICredentialManager.cs | 3 ++- src/Netatmo/Netatmo.csproj | 2 +- 6 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Netatmo.Tests/CredentialManager.cs b/src/Netatmo.Tests/CredentialManager.cs index 0678245..49309c4 100644 --- a/src/Netatmo.Tests/CredentialManager.cs +++ b/src/Netatmo.Tests/CredentialManager.cs @@ -59,6 +59,29 @@ await sut.GenerateToken("username@email.com", "p@$$W0rd", token.AccessToken.Should().Be(expectedToken.access_token); token.RefreshToken.Should().Be(expectedToken.refresh_token); token.ExpiresAt.Should().Be(token.ReceivedAt.Plus(Duration.FromSeconds(expectedToken.expires_in))); + } + + [Fact] + public void ProvideOAuth2Token_Should_Provide_Token_From_Existing() + { + var expectedToken = new + { + access_token = "2YotnFZFEjr1zCsicMWpAA", + expires_in = 20 + }; + + httpTest.RespondWithJson(expectedToken); + + var sut = new Netatmo.CredentialManager("https://api.netatmo.com/", "clientId", "clientSecret", SystemClock.Instance); + + sut.ProvideOAuth2Token(expectedToken.access_token); + + var token = sut.CredentialToken; + + token.Should().BeOfType(); + token.AccessToken.Should().Be(expectedToken.access_token); + token.RefreshToken.Should().BeNull(); + token.ExpiresAt.Should().Be(token.ReceivedAt.Plus(Duration.FromSeconds(expectedToken.expires_in))); } [Fact] @@ -105,4 +128,4 @@ public async Task RefreshToken_Should_Refresh_Token() refreshedToken.Should().NotBe(oldToken); } } -} \ No newline at end of file +} diff --git a/src/Netatmo/Client.cs b/src/Netatmo/Client.cs index a58ad26..99918dc 100644 --- a/src/Netatmo/Client.cs +++ b/src/Netatmo/Client.cs @@ -22,6 +22,11 @@ public Task GenerateToken(string username, string password, Scope[] scopes = nul { return CredentialManager.GenerateToken(username, password, scopes); } + + public void ProvideOAuth2Token(string oauth2Token) + { + CredentialManager.ProvideOAuth2Token(oauth2Token); + } public Task RefreshToken() { diff --git a/src/Netatmo/CredentialManager.cs b/src/Netatmo/CredentialManager.cs index 06ee01d..74c6e1d 100644 --- a/src/Netatmo/CredentialManager.cs +++ b/src/Netatmo/CredentialManager.cs @@ -8,6 +8,8 @@ namespace Netatmo { + using System; + public class CredentialManager : ICredentialManager { private readonly string baseUrl; @@ -44,6 +46,18 @@ public async Task GenerateToken(string username, string password, Scope[] scopes CredentialToken = new CredentialToken(token, clock); } + public void ProvideOAuth2Token(string oauth2Token) + { + var appToken = new Token() + { + AccessToken = oauth2Token, + RefreshToken = null, + ExpiresIn = 20 + }; + + CredentialToken = new CredentialToken(appToken, clock); + } + public async Task RefreshToken() { // TODO : Handle not success status codes (rate limit exceeded, api down, ect) @@ -58,4 +72,4 @@ public async Task RefreshToken() CredentialToken = new CredentialToken(token, clock); } } -} \ No newline at end of file +} diff --git a/src/Netatmo/IClient.cs b/src/Netatmo/IClient.cs index 9b2b109..8c4f27b 100644 --- a/src/Netatmo/IClient.cs +++ b/src/Netatmo/IClient.cs @@ -9,6 +9,7 @@ public interface IClient IAirClient Air { get; } ICredentialManager CredentialManager { get; } Task GenerateToken(string username, string password, Scope[] scopes = null); + void ProvideOAuth2Token(string oauth2Token); Task RefreshToken(); } } \ No newline at end of file diff --git a/src/Netatmo/ICredentialManager.cs b/src/Netatmo/ICredentialManager.cs index a9064ae..6c4b673 100644 --- a/src/Netatmo/ICredentialManager.cs +++ b/src/Netatmo/ICredentialManager.cs @@ -8,6 +8,7 @@ public interface ICredentialManager CredentialToken CredentialToken { get; } string AccessToken { get; } Task GenerateToken(string username, string password, Scope[] scopes = null); + void ProvideOAuth2Token(string oauth2Token); Task RefreshToken(); } -} \ No newline at end of file +} diff --git a/src/Netatmo/Netatmo.csproj b/src/Netatmo/Netatmo.csproj index 377b965..bf01bb9 100644 --- a/src/Netatmo/Netatmo.csproj +++ b/src/Netatmo/Netatmo.csproj @@ -1,6 +1,6 @@  - 1.3.1 + 1.3.2 netcoreapp2.2;netcoreapp2.1;netstandard2.0 Netatmo Luc FASQUELLE