Skip to content

Commit

Permalink
get token expires at by token file modify time
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-024 committed Dec 9, 2024
1 parent b754f0b commit d234bb1
Showing 1 changed file with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

package org.apache.paimon.rest.auth;

import org.apache.paimon.utils.Pair;
import org.apache.paimon.utils.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand All @@ -39,13 +41,16 @@ public class BearTokenFileCredentialsProvider extends BaseBearTokenCredentialsPr

public BearTokenFileCredentialsProvider(String tokenFilePath) {
this.tokenFilePath = tokenFilePath;
this.token = getTokenFromFile();
Pair<String, Long> token2GenerateTime = getTokenAndGenerateTimeFromFile();
this.token = token2GenerateTime.getLeft();
}

public BearTokenFileCredentialsProvider(String tokenFilePath, Long expiresInMills) {
this(tokenFilePath);
this.tokenFilePath = tokenFilePath;
Pair<String, Long> token2GenerateTime = getTokenAndGenerateTimeFromFile();
this.token = token2GenerateTime.getLeft();
this.expiresAtMillis = token2GenerateTime.getRight() + expiresInMills;
this.keepRefreshed = true;
this.expiresAtMillis = -1L;
this.expiresInMills = expiresInMills;
}

Expand All @@ -57,8 +62,9 @@ String token() {
@Override
public boolean refresh() {
long start = System.currentTimeMillis();
this.token = getTokenFromFile();
this.expiresAtMillis = start + this.expiresInMills;
Pair<String, Long> token2GenerateTime = getTokenAndGenerateTimeFromFile();
this.token = token2GenerateTime.getLeft();
this.expiresAtMillis = Math.min(token2GenerateTime.getRight(), start) + this.expiresInMills;
if (StringUtils.isNullOrWhitespaceOnly(this.token)) {
return false;
}
Expand Down Expand Up @@ -95,10 +101,15 @@ public Optional<Long> expiresInMills() {
return Optional.ofNullable(this.expiresInMills);
}

private String getTokenFromFile() {
private Pair<String, Long> getTokenAndGenerateTimeFromFile() {
try {
// todo: handle exception
return new String(Files.readAllBytes(Paths.get(tokenFilePath)), StandardCharsets.UTF_8);
File file = new File(tokenFilePath);
long modified = file.lastModified();
String token =
new String(
Files.readAllBytes(Paths.get(tokenFilePath)), StandardCharsets.UTF_8);
return Pair.of(token, modified);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit d234bb1

Please sign in to comment.