diff options
author | Anton Tananaev <anton@traccar.org> | 2023-11-26 08:07:52 -0800 |
---|---|---|
committer | Anton Tananaev <anton@traccar.org> | 2023-11-26 08:07:52 -0800 |
commit | acdc451e3e2ab94f548f078c4bd49985b3c2f01d (patch) | |
tree | 0f396d7b809e7a52adb32e1ee268cbff31235c18 /src/main/java/org/traccar/api/signature | |
parent | 08cb38a2126e91ccde44e9f91d2ad1f149c7e6b5 (diff) | |
download | trackermap-server-acdc451e3e2ab94f548f078c4bd49985b3c2f01d.tar.gz trackermap-server-acdc451e3e2ab94f548f078c4bd49985b3c2f01d.tar.bz2 trackermap-server-acdc451e3e2ab94f548f078c4bd49985b3c2f01d.zip |
Set expiration from token
Diffstat (limited to 'src/main/java/org/traccar/api/signature')
-rw-r--r-- | src/main/java/org/traccar/api/signature/TokenManager.java | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/src/main/java/org/traccar/api/signature/TokenManager.java b/src/main/java/org/traccar/api/signature/TokenManager.java index 3019e12b9..824433b08 100644 --- a/src/main/java/org/traccar/api/signature/TokenManager.java +++ b/src/main/java/org/traccar/api/signature/TokenManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2022 - 2023 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,11 +35,19 @@ public class TokenManager { private final ObjectMapper objectMapper; private final CryptoManager cryptoManager; - public static class Data { + public static class TokenData { @JsonProperty("u") private long userId; @JsonProperty("e") private Date expiration; + + public long getUserId() { + return userId; + } + + public Date getExpiration() { + return expiration; + } } @Inject @@ -54,7 +62,7 @@ public class TokenManager { public String generateToken( long userId, Date expiration) throws IOException, GeneralSecurityException, StorageException { - Data data = new Data(); + TokenData data = new TokenData(); data.userId = userId; if (expiration != null) { data.expiration = expiration; @@ -65,13 +73,13 @@ public class TokenManager { return Base64.encodeBase64URLSafeString(cryptoManager.sign(encoded)); } - public long verifyToken(String token) throws IOException, GeneralSecurityException, StorageException { + public TokenData verifyToken(String token) throws IOException, GeneralSecurityException, StorageException { byte[] encoded = cryptoManager.verify(Base64.decodeBase64(token)); - Data data = objectMapper.readValue(encoded, Data.class); + TokenData data = objectMapper.readValue(encoded, TokenData.class); if (data.expiration.before(new Date())) { throw new SecurityException("Token has expired"); } - return data.userId; + return data; } } |