8000 Add existing oauth2Token support by jobam · Pull Request #32 · Riges/Netatmo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add existing oauth2Token support #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("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<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