Skip to content

Commit

Permalink
Fix cached JWT
Browse files Browse the repository at this point in the history
  • Loading branch information
dlazarenko-macrometa committed Aug 18, 2022
1 parent 98e3436 commit 7c8d571
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>co.macrometa</groupId>
<artifactId>c84j</artifactId>
<version>1.1.25</version>
<version>1.1.25.1</version>
<inceptionYear>2019</inceptionYear>
<packaging>jar</packaging>

Expand Down Expand Up @@ -334,7 +334,7 @@
<url>https://github.com/Macrometacorp/C84j.git</url>
<connection>scm:git:git@github.com:Macrometacorp/C84j.git</connection>
<developerConnection>scm:git:git@github.com:Macrometacorp/C84j.git</developerConnection>
<tag>c84j-1.1.25</tag>
<tag>c84j-1.1.25.1</tag>
</scm>

<organization>
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/c8db/internal/http/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public class HttpConnection implements Connection {
private final Protocol contentType;
private final HostDescription host;
private volatile String jwt;
private volatile String defaultJWT;
private final String apiKey;
private final HostDescription auxHost;

Expand All @@ -110,7 +111,7 @@ private HttpConnection(final HostDescription host, final Integer timeout, final
this.useSsl = useSsl;
this.util = util;
this.contentType = contentType;
this.jwt = jwt;
this.defaultJWT = jwt;
this.apiKey = apiKey;
this.auxHost = auxHost;
final RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
Expand Down Expand Up @@ -226,6 +227,7 @@ public Response execute(final Request request) throws C8DBException, IOException
}
addHeader(request, httpRequest);
if (jwtAuthEnabled) {
updateJWT();
if (StringUtils.isNotEmpty(apiKey) && jwt == null) { //Use API key onlu if API Key is provided
LOGGER.debug("Using API Key for authenication.");
httpRequest.addHeader("Authorization", "apikey " + apiKey);
Expand Down Expand Up @@ -297,6 +299,14 @@ private Response retryRequest(final Request request, HttpRequestBase httpRequest
return response;
}

private void updateJWT() {
if (StringUtils.isNotEmpty(user) && !host.getHost().equals(auxHost.getHost())) {
jwt = null;
} else {
jwt = defaultJWT;
}
}

private synchronized void addJWT(final Request request) throws IOException {
addServiceJWT();
if(StringUtils.isNotEmpty(user) && !host.getHost().equals(auxHost.getHost())) {
Expand All @@ -321,7 +331,8 @@ private synchronized void addServiceJWT() throws IOException {
}
Response authResponse = buildResponse(client.execute(authHttpRequest));
checkError(authResponse);
setJwt(authResponse.getBody().get("jwt").getAsString());
defaultJWT = authResponse.getBody().get("jwt").getAsString();
setJwt(defaultJWT);
}

private synchronized void addUserJWT(String tenant, String user) throws IOException {
Expand Down

0 comments on commit 7c8d571

Please sign in to comment.