mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 21:31:53 +01:00
[oauth] Migrate usage of LocalDateTime to Instant (#3066)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
parent
8f745b0d3d
commit
9bb88a1b7b
@ -18,7 +18,7 @@ import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@ -331,7 +331,7 @@ public class OAuthClientServiceImpl implements OAuthClientService {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (lastAccessToken.isExpired(LocalDateTime.now(), tokenExpiresInSeconds)
|
||||
if (lastAccessToken.isExpired(Instant.now(), tokenExpiresInSeconds)
|
||||
&& lastAccessToken.getRefreshToken() != null) {
|
||||
return refreshToken();
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedActionException;
|
||||
import java.security.PrivilegedExceptionAction;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
@ -312,7 +312,7 @@ public class OAuthConnector {
|
||||
|
||||
if (statusCode == HttpStatus.OK_200) {
|
||||
AccessTokenResponse jsonResponse = gson.fromJson(content, AccessTokenResponse.class);
|
||||
jsonResponse.setCreatedOn(LocalDateTime.now()); // this is not supplied by the response
|
||||
jsonResponse.setCreatedOn(Instant.now()); // this is not supplied by the response
|
||||
logger.debug("grant type {} to URL {} success", grantType, request.getURI());
|
||||
return jsonResponse;
|
||||
} else if (statusCode == HttpStatus.BAD_REQUEST_400) {
|
||||
|
@ -13,7 +13,7 @@
|
||||
package org.openhab.core.auth.client.oauth2;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@ -98,7 +98,7 @@ public final class AccessTokenResponse implements Serializable, Cloneable {
|
||||
* This should be slightly later than the actual time the access token
|
||||
* is produced at the server.
|
||||
*/
|
||||
private LocalDateTime createdOn;
|
||||
private Instant createdOn;
|
||||
|
||||
/**
|
||||
* Calculate if the token is expired against the given time.
|
||||
@ -110,7 +110,7 @@ public final class AccessTokenResponse implements Serializable, Cloneable {
|
||||
* by the authorization server.
|
||||
* @return true if object is not-initialized, or expired, or expired early due to buffer
|
||||
*/
|
||||
public boolean isExpired(LocalDateTime givenTime, int tokenExpiresInBuffer) {
|
||||
public boolean isExpired(Instant givenTime, int tokenExpiresInBuffer) {
|
||||
return createdOn == null
|
||||
|| createdOn.plusSeconds(expiresIn).minusSeconds(tokenExpiresInBuffer).isBefore(givenTime);
|
||||
}
|
||||
@ -163,11 +163,11 @@ public final class AccessTokenResponse implements Serializable, Cloneable {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public LocalDateTime getCreatedOn() {
|
||||
public Instant getCreatedOn() {
|
||||
return createdOn;
|
||||
}
|
||||
|
||||
public void setCreatedOn(LocalDateTime createdOn) {
|
||||
public void setCreatedOn(Instant createdOn) {
|
||||
this.createdOn = createdOn;
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,7 @@ package org.openhab.core.auth.client.oauth2;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Instant;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -30,13 +30,13 @@ public class AccessTokenResponseTest {
|
||||
AccessTokenResponse token = new AccessTokenResponse();
|
||||
|
||||
// token created 20 seconds ago
|
||||
LocalDateTime someTimeAgo = LocalDateTime.now().minusSeconds(20);
|
||||
Instant someTimeAgo = Instant.now().minusSeconds(20);
|
||||
token.setCreatedOn(someTimeAgo);
|
||||
|
||||
// token only has a life time of 10 seconds
|
||||
token.setExpiresIn(10);
|
||||
|
||||
assertTrue(token.isExpired(LocalDateTime.now(), 0), "Token should have been expired");
|
||||
assertTrue(token.isExpired(Instant.now(), 0), "Token should have been expired");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -44,25 +44,25 @@ public class AccessTokenResponseTest {
|
||||
AccessTokenResponse token = new AccessTokenResponse();
|
||||
|
||||
// token created 20 seconds ago
|
||||
LocalDateTime someTimeAgo = LocalDateTime.now().minusSeconds(20);
|
||||
Instant someTimeAgo = Instant.now().minusSeconds(20);
|
||||
token.setCreatedOn(someTimeAgo);
|
||||
|
||||
// token only has a life time of 30 seconds
|
||||
token.setExpiresIn(30);
|
||||
|
||||
assertTrue(token.isExpired(LocalDateTime.now(), 15), "Token should have been expired due to buffer");
|
||||
assertTrue(token.isExpired(Instant.now(), 15), "Token should have been expired due to buffer");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTokenNotExpired() {
|
||||
AccessTokenResponse token = new AccessTokenResponse();
|
||||
// A token created now
|
||||
token.setCreatedOn(LocalDateTime.now());
|
||||
token.setCreatedOn(Instant.now());
|
||||
|
||||
// token has life time of 1 minute
|
||||
token.setExpiresIn(60);
|
||||
|
||||
assertFalse(token.isExpired(LocalDateTime.now(), 0), "Token should not be expired");
|
||||
assertFalse(token.isExpired(Instant.now(), 0), "Token should not be expired");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -70,12 +70,12 @@ public class AccessTokenResponseTest {
|
||||
AccessTokenResponse token = new AccessTokenResponse();
|
||||
|
||||
// token created 20 seconds ago
|
||||
LocalDateTime someTimeAgo = LocalDateTime.now().minusSeconds(20);
|
||||
Instant someTimeAgo = Instant.now().minusSeconds(20);
|
||||
token.setCreatedOn(someTimeAgo);
|
||||
|
||||
// token has a life time of 60 seconds
|
||||
token.setExpiresIn(60);
|
||||
|
||||
assertTrue(!token.isExpired(LocalDateTime.now(), 10), "Token should have been expired due to buffer");
|
||||
assertTrue(!token.isExpired(Instant.now(), 10), "Token should have been expired due to buffer");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user