Skip to content

Commit

Permalink
Add existing oauth2Token support (#32)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jobam authored and Riges committed Feb 5, 2019
1 parent 4cc808b commit 435e1e9
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
25 changes: 24 additions & 1 deletion src/Netatmo.Tests/CredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ await sut.GenerateToken("[email protected]", "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<CredentialToken>();
token.AccessToken.Should().Be(expectedToken.access_token);
token.RefreshToken.Should().BeNull();
token.ExpiresAt.Should().Be(token.ReceivedAt.Plus(Duration.FromSeconds(expectedToken.expires_in)));
}

[Fact]
Expand Down Expand Up @@ -105,4 +128,4 @@ public async Task RefreshToken_Should_Refresh_Token()
refreshedToken.Should().NotBe(oldToken);
}
}
}
}
5 changes: 5 additions & 0 deletions src/Netatmo/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
16 changes: 15 additions & 1 deletion src/Netatmo/CredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Netatmo
{
using System;

public class CredentialManager : ICredentialManager
{
private readonly string baseUrl;
Expand Down Expand Up @@ -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)
Expand All @@ -58,4 +72,4 @@ public async Task RefreshToken()
CredentialToken = new CredentialToken(token, clock);
}
}
}
}
1 change: 1 addition & 0 deletions src/Netatmo/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
3 changes: 2 additions & 1 deletion src/Netatmo/ICredentialManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
2 changes: 1 addition & 1 deletion src/Netatmo/Netatmo.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.3.1</Version>
<Version>1.3.2</Version>
<TargetFrameworks>netcoreapp2.2;netcoreapp2.1;netstandard2.0</TargetFrameworks>
<PackageId>Netatmo</PackageId>
<Authors>Luc FASQUELLE</Authors>
Expand Down

0 comments on commit 435e1e9

Please sign in to comment.