From 8d0c499b2f5ac5f0e0f3f0595f8f448d7e7d2cbb Mon Sep 17 00:00:00 2001 From: Lost Carrier Date: Tue, 18 Jul 2023 23:48:12 +0200 Subject: [PATCH] Netatmo Authentication change / sry, this will result breaking change on the public interface --- .../java/losty/netatmo/NetatmoHttpClient.java | 17 +------ .../netatmo/oauthtoken/OAuthTokenHandler.java | 46 ++----------------- .../oauthtoken/OAuthTokenHandlerTest.java | 15 ------ 3 files changed, 6 insertions(+), 72 deletions(-) diff --git a/src/main/java/losty/netatmo/NetatmoHttpClient.java b/src/main/java/losty/netatmo/NetatmoHttpClient.java index 7fd471f..1d46cb0 100644 --- a/src/main/java/losty/netatmo/NetatmoHttpClient.java +++ b/src/main/java/losty/netatmo/NetatmoHttpClient.java @@ -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); } /** diff --git a/src/main/java/losty/netatmo/oauthtoken/OAuthTokenHandler.java b/src/main/java/losty/netatmo/oauthtoken/OAuthTokenHandler.java index 96964ab..b2c52a1 100644 --- a/src/main/java/losty/netatmo/oauthtoken/OAuthTokenHandler.java +++ b/src/main/java/losty/netatmo/oauthtoken/OAuthTokenHandler.java @@ -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. * @@ -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 { @@ -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) @@ -130,8 +96,4 @@ private void refreshToken() throws NetatmoNotLoggedInException, NetatmoOAuthExce throw new NetatmoOAuthException(e); } } - - private boolean isAccessTokenExpired() { - return oauthTokenStore.getExpiresAt() < System.currentTimeMillis(); - } } diff --git a/src/test/java/losty/netatmo/oauthtoken/OAuthTokenHandlerTest.java b/src/test/java/losty/netatmo/oauthtoken/OAuthTokenHandlerTest.java index db412f2..3057f51 100644 --- a/src/test/java/losty/netatmo/oauthtoken/OAuthTokenHandlerTest.java +++ b/src/test/java/losty/netatmo/oauthtoken/OAuthTokenHandlerTest.java @@ -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,