-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check jwt token expiration and refresh token if necessary
- Loading branch information
Showing
6 changed files
with
63 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/java/io/camunda/operate/auth/JwtAuthentication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.camunda.operate.auth; | ||
|
||
import java.util.Base64; | ||
|
||
import org.apache.hc.core5.http.message.BasicHeader; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.camunda.operate.CamundaOperateClient; | ||
import io.camunda.operate.exception.OperateException; | ||
|
||
public abstract class JwtAuthentication implements AuthInterface { | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
private static final Base64.Decoder DECODER = Base64.getUrlDecoder(); | ||
|
||
public int getExpiration(String token) throws OperateException { | ||
try { | ||
String[] chunks = token.split("\\."); | ||
|
||
|
||
String payload = new String(DECODER.decode(chunks[1])); | ||
JsonNode jsonPayload = MAPPER.readValue(payload, JsonNode.class); | ||
JsonNode exp = jsonPayload.get("exp"); | ||
if (exp==null) { | ||
return 0; | ||
} else { | ||
return exp.asInt(); | ||
} | ||
} catch (JsonProcessingException e) { | ||
throw new OperateException("Token is not readable", e); | ||
} | ||
} | ||
|
||
public void setToken(CamundaOperateClient client, String token) throws OperateException { | ||
client.setAuthHeader(new BasicHeader("Authorization", "Bearer " + token)); | ||
client.setTokenExpiration(getExpiration(token)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters