Skip to content

Commit

Permalink
merge TokenFile auth with refresh auth
Browse files Browse the repository at this point in the history
Signed-off-by: Min Jin <[email protected]>
  • Loading branch information
yue9944882 committed Dec 9, 2024
1 parent 22f8810 commit f1e0eaf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
// TODO: prefer OpenAPI backed Auentication once it is available. see details in
// https://github.com/OpenAPITools/openapi-generator/pull/6036. currently, the
// workaround is to hijack the http request.
// TODO: Merge this with TokenFileAuthentication.
public class RefreshAuthentication implements Authentication, Interceptor {
private Instant expiry;
private Duration refreshPeriod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
Expand All @@ -26,49 +27,23 @@
// TODO: prefer OpenAPI backed Auentication once it is available. see details in
// https://github.com/OpenAPITools/openapi-generator/pull/6036. currently, the
// workaround is to hijack the http request.
public class TokenFileAuthentication implements Authentication, Interceptor {
private String file;
private String token;
private Instant expiry;

public class TokenFileAuthentication extends RefreshAuthentication {
public TokenFileAuthentication(String file) {
this.expiry = Instant.MIN;
this.file = file;
}

private String getToken() {
if (Instant.now().isAfter(this.expiry)) {
try {
this.token =
new String(Files.readAllBytes(Paths.get(this.file)), Charset.defaultCharset()).trim();
expiry = Instant.now().plusSeconds(60);
} catch (IOException ie) {
throw new RuntimeException("Cannot read file: " + this.file);
}
}

return this.token;
this(file, Duration.ofMinutes(1));
}

public void setExpiry(Instant expiry) {
this.expiry = expiry;
public TokenFileAuthentication(String file, Duration refreshPeriod) {
super(() -> {
return getToken(file);
}, refreshPeriod);
}

public void setFile(String file) {
this.file = file;
}

@Override
public void provide(ApiClient client) {
OkHttpClient withInterceptor = client.getHttpClient().newBuilder().addInterceptor(this).build();
client.setHttpClient(withInterceptor);
private static String getToken(String file) {
try {
return new String(Files.readAllBytes(Paths.get(file)), Charset.defaultCharset()).trim();
} catch (IOException e) {
throw new RuntimeException("Cannot read file: " + file);
}
}

@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
Request newRequest;
newRequest = request.newBuilder().header("Authorization", "Bearer " + getToken()).build();
return chain.proceed(newRequest);
}
}

0 comments on commit f1e0eaf

Please sign in to comment.