Saving access token and refresh token without relying on library class

I'd pretty confidently say this now definitely fixes #17, therefore this will be merged into master. If necessary issue will have to reopened.
This commit is contained in:
2023-01-28 21:42:43 +01:00
parent e52dfd4621
commit 502295b34a
2 changed files with 23 additions and 12 deletions

View File

@@ -83,14 +83,12 @@ public class LastFMToSpotify {
if (configuration.containsKey("cache.crypto") && TokenHelper.existsTokens()) { if (configuration.containsKey("cache.crypto") && TokenHelper.existsTokens()) {
logLn("Cached credentials have been found.", 2); logLn("Cached credentials have been found.", 2);
logLn("Fetching credentials from cache.", 2); logLn("Fetching credentials from cache.", 2);
SpotifyCredentials oldcred = TokenHelper.fetchTokens(); SpotifyCredentials cred = TokenHelper.fetchTokens();
api.setRefreshToken(oldcred.getRefreshToken()); api.setRefreshToken(cred.getRefreshToken());
SpotifyCredentials cred;
if(oldcred.isValid()){ if(!cred.isValid()){
cred=oldcred;
} else {
logLn("Cached credentials are invalid due to age. Refreshing and saving to cache", 2); logLn("Cached credentials are invalid due to age. Refreshing and saving to cache", 2);
cred = new SpotifyCredentials(api.authorizationCodeRefresh().build().execute()); cred.refreshCredentials(api.authorizationCodeRefresh().build().execute());
TokenHelper.saveTokens(cred); TokenHelper.saveTokens(cred);
} }
configuration.put("spotify.access", cred.getAccessToken()); configuration.put("spotify.access", cred.getAccessToken());

View File

@@ -4,21 +4,24 @@ import se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCrede
import java.io.Serializable; import java.io.Serializable;
import java.time.Clock; import java.time.Clock;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Objects;
/** /**
* A wrapper class for <code>se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials</code>. Implements checking validity of access token. * A wrapper class for <code>se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials</code>. Implements checking validity of access token.
*/ */
public class SpotifyCredentials implements Serializable { public class SpotifyCredentials implements Serializable {
private final AuthorizationCodeCredentials cred; private String accessToken;
private final LocalDateTime validUntil; private String refreshToken;
private LocalDateTime validUntil;
/** /**
* Initializes the class * Initializes the class
* @param cred The <code>AuthorizationCodeCredentials</code> to be saved. Recommended for use with recently (last few seconds) generated Credentials * @param cred The <code>AuthorizationCodeCredentials</code> to be saved. Recommended for use with recently (last few seconds) generated Credentials
*/ */
public SpotifyCredentials(AuthorizationCodeCredentials cred){ public SpotifyCredentials(AuthorizationCodeCredentials cred){
this.cred = cred; this.accessToken = cred.getAccessToken();
this.refreshToken = cred.getRefreshToken();
this.validUntil = LocalDateTime.now(Clock.systemDefaultZone()).plusSeconds(cred.getExpiresIn()); this.validUntil = LocalDateTime.now(Clock.systemDefaultZone()).plusSeconds(cred.getExpiresIn());
} }
@@ -27,7 +30,7 @@ public class SpotifyCredentials implements Serializable {
* @return An access token that can be provided in subsequent calls, for example to Spotify Web API services. * @return An access token that can be provided in subsequent calls, for example to Spotify Web API services.
*/ */
public String getAccessToken(){ public String getAccessToken(){
return cred.getAccessToken(); return accessToken;
} }
/** /**
@@ -35,7 +38,7 @@ public class SpotifyCredentials implements Serializable {
* @return A token that can be sent to the Spotify Accounts service in place of an access token. * @return A token that can be sent to the Spotify Accounts service in place of an access token.
*/ */
public String getRefreshToken(){ public String getRefreshToken(){
return cred.getRefreshToken(); return refreshToken;
} }
/** /**
@@ -54,4 +57,14 @@ public class SpotifyCredentials implements Serializable {
return LocalDateTime.now(Clock.systemDefaultZone()).isBefore(getValidUntil()); return LocalDateTime.now(Clock.systemDefaultZone()).isBefore(getValidUntil());
} }
/**
* Refreshes the access token. If a new refresh token is provided it will be saved as well.
* @param cred The <code>AuthorizationCodeCredentials</code> to be saved. Recommended for use with recently (last few seconds) generated Credentials
*/
public void refreshCredentials(AuthorizationCodeCredentials cred){
this.accessToken = cred.getAccessToken();
if(Objects.nonNull(cred.getRefreshToken())) this.refreshToken = cred.getRefreshToken();
this.validUntil = LocalDateTime.now(Clock.systemDefaultZone()).plusSeconds(cred.getExpiresIn());
}
} }