com.truelayer.java.http.auth.cache.SimpleCredentialsCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truelayer-java Show documentation
Show all versions of truelayer-java Show documentation
TrueLayer Java SDK for https://truelayer.com
package com.truelayer.java.http.auth.cache;
import com.truelayer.java.auth.entities.AccessToken;
import com.truelayer.java.entities.RequestScopes;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.*;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
/**
* Default in-memory cache implementation.
* Class constructor accepts a Clock instance for improved testing purposes. Please note that this is not a monotonic implementation.
* @see this article for more context,
*/
public class SimpleCredentialsCache implements ICredentialsCache {
/**
* internal state
*/
private final Map tokenRecords = new HashMap<>();
private final Clock clock;
/**
* Constructor for this class.
* @param clock clock instance
*/
public SimpleCredentialsCache(Clock clock) {
this.clock = clock;
}
@Override
public Optional getToken(RequestScopes scopes) {
int key = computeCacheKey(scopes);
AccessTokenRecord tokenRecord = tokenRecords.get(key);
if (tokenRecord == null || LocalDateTime.now(clock).compareTo(tokenRecord.expiresAt) >= 0) {
return Optional.empty();
}
return Optional.of(tokenRecord.token);
}
@Override
public void storeToken(RequestScopes scopes, AccessToken token) {
AccessTokenRecord tokenRecord =
new AccessTokenRecord(token, LocalDateTime.now(clock).plusSeconds(token.getExpiresIn()));
tokenRecords.put(computeCacheKey(scopes), tokenRecord);
}
@Override
public void clearToken(RequestScopes scopes) {
tokenRecords.remove(computeCacheKey(scopes));
}
@Getter
@RequiredArgsConstructor
public static class AccessTokenRecord {
private final AccessToken token;
private final LocalDateTime expiresAt;
}
private static int computeCacheKey(RequestScopes scopes) {
List sortedScopes = new ArrayList<>(scopes.getScopes());
Collections.sort(sortedScopes);
return sortedScopes.hashCode();
}
}