Skip to content

Commit

Permalink
Netatmo Authentication change / sry, this will result breaking change…
Browse files Browse the repository at this point in the history
… on the public interface
  • Loading branch information
Lost Carrier committed Jul 18, 2023
1 parent aad9aa3 commit 8d0c499
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 72 deletions.
17 changes: 2 additions & 15 deletions src/main/java/losty/netatmo/NetatmoHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,12 @@ public class NetatmoHttpClient {

private final OAuthTokenHandler oAuthTokenHandler;

public NetatmoHttpClient(final String clientId, final String clientSecret) {
this(clientId, clientSecret, new TransientOAuthTokenStore());
}

public NetatmoHttpClient(final String clientId, final String clientSecret, final OAuthTokenStore oauthTokenStore) {
this.oAuthTokenHandler = new OAuthTokenHandler(URL_REQUEST_TOKEN, SCOPE, clientId, clientSecret, oauthTokenStore);
}

/**
* This is the first request you have to do before being able to use the
* API. It allows you to retrieve an access token in one step, using your
* application's credentials and the user's credentials.
*
* @param email E-Mail
* @param password Password
* @throws NetatmoOAuthException When something goes wrong with OAuth.
*/
public void login(final String email, final String password) throws NetatmoOAuthException {
oAuthTokenHandler.login(email, password);
public NetatmoHttpClient(final String clientId, final String clientSecret, final String scope, final OAuthTokenStore oauthTokenStore) {
this.oAuthTokenHandler = new OAuthTokenHandler(URL_REQUEST_TOKEN, scope, clientId, clientSecret, oauthTokenStore);
}

/**
Expand Down
46 changes: 4 additions & 42 deletions src/main/java/losty/netatmo/oauthtoken/OAuthTokenHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,6 @@ public OAuthTokenHandler(String tokenUrl, String scope, String clientId, String
this.oauthTokenStore = oauthTokenStore;
}

/**
* This is the first request you have to do before being able to use the
* API. It allows you to retrieve an access token in one step, using your
* application's credentials and the user's credentials.
*
* @param email E-Mail
* @param password Password
* @throws NetatmoOAuthException When something goes wrong with OAuth.
*/
public void login(final String email, final String password) throws NetatmoOAuthException {

try {
OAuthClientRequest request = OAuthClientRequest.tokenLocation(tokenUrl)
.setGrantType(GrantType.PASSWORD)
.setClientId(clientId)
.setClientSecret(clientSecret)
.setUsername(email)
.setPassword(password)
.setScope(scope)
.buildBodyMessage();

OAuthJSONAccessTokenResponse token = oAuthClient.accessToken(request);
long expiresAt = System.currentTimeMillis() + token.getExpiresIn() * 1000;
oauthTokenStore.setTokens(token.getRefreshToken(), token.getAccessToken(), expiresAt);
} catch (OAuthSystemException | OAuthProblemException e) {
throw new NetatmoOAuthException(e);
}
}

/**
* Executes a certain GET request to a OAuth protected URL.
*
Expand All @@ -80,7 +51,6 @@ public void login(final String email, final String password) throws NetatmoOAuth
*/
public String executeRequest(String request) throws NetatmoOAuthException {

verifyLoggedIn();
verifyAccessToken();

try {
Expand All @@ -98,21 +68,17 @@ private OAuthClientRequest createBearerClientRequest(String request) throws OAut
.buildQueryMessage();
}

private void verifyLoggedIn() throws NetatmoNotLoggedInException {
if (oauthTokenStore.getAccessToken() == null) {
throw new NetatmoNotLoggedInException("Please use login() first!");
}
}

private void verifyAccessToken() {
if(isAccessTokenExpired()) {
refreshToken();
}
}

private void refreshToken() throws NetatmoNotLoggedInException, NetatmoOAuthException {
private boolean isAccessTokenExpired() {
return oauthTokenStore.getExpiresAt() < System.currentTimeMillis();
}

verifyLoggedIn();
private void refreshToken() throws NetatmoNotLoggedInException, NetatmoOAuthException {

try {
OAuthClientRequest request = OAuthClientRequest.tokenLocation(tokenUrl)
Expand All @@ -130,8 +96,4 @@ private void refreshToken() throws NetatmoNotLoggedInException, NetatmoOAuthExce
throw new NetatmoOAuthException(e);
}
}

private boolean isAccessTokenExpired() {
return oauthTokenStore.getExpiresAt() < System.currentTimeMillis();
}
}
15 changes: 0 additions & 15 deletions src/test/java/losty/netatmo/oauthtoken/OAuthTokenHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,6 @@ public void testTokenRefresh() throws OAuthProblemException, OAuthSystemExceptio
verify(oAuthClient, times(1)).accessToken(any(OAuthClientRequest.class));
}

@Test
public void testLogin() throws OAuthProblemException, OAuthSystemException {
OAuthJSONAccessTokenResponse tokenResponse = mock(OAuthJSONAccessTokenResponse.class);
when(tokenResponse.getRefreshToken()).thenReturn("refreshToken");
when(tokenResponse.getAccessToken()).thenReturn("accessToken");
when(tokenResponse.getExpiresIn()).thenReturn(42L);
when(oAuthClient.accessToken(any(OAuthClientRequest.class))).thenReturn(tokenResponse);

oAuthTokenHandler.login("email", "password");

assertEquals("refreshToken", oAuthTokenStore.getRefreshToken());
assertEquals("accessToken", oAuthTokenStore.getAccessToken());
assertTrue(oAuthTokenStore.getExpiresAt() > 42L);
}

@Test
public void testNotLoggedIn() {
assertThrows(NetatmoNotLoggedInException.class,
Expand Down

0 comments on commit 8d0c499

Please sign in to comment.