Skip to content

Commit

Permalink
[SERVER] update SaigonParkingAuthentication using UUID as jti
Browse files Browse the repository at this point in the history
  • Loading branch information
Huynh Thanh Binh committed Aug 23, 2020
1 parent 9f03d6a commit 27db117
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
5 changes: 5 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@
<artifactId>jjwt-jackson</artifactId>
<version>${java-jwt.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>${java-uuid-generator.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.springframework.data.util.Pair;

import com.bht.saigonparking.common.util.LoggingUtil;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
import com.google.common.io.ByteStreams;

import io.jsonwebtoken.Claims;
Expand All @@ -34,6 +36,7 @@ public final class SaigonParkingAuthenticationImpl implements SaigonParkingAuthe

private static final String SAIGON_PARKING_ISSUER = "www.saigonparking.wtf";
private static final short MAX_RANDOM_EXCLUSIVE = 1000;
private static final TimeBasedGenerator UUID_GENERATOR = Generators.timeBasedGenerator();

private static final String USER_ROLE_KEY_NAME = "role";
private static final String FACTOR_KEY_NAME = "fac";
Expand Down Expand Up @@ -88,16 +91,15 @@ private Pair<String, String> generateJwtToken(@NotNull SaigonParkingTokenType ty
@NotNull ChronoUnit timeUnit) {
Instant now = Instant.now();
Integer factor = new Random().nextInt(MAX_RANDOM_EXCLUSIVE);
Long encryptedUserId = encryptUserId(userId, factor);
String tokenId = String.format("%d@%d", encryptedUserId, now.toEpochMilli());
String tokenId = UUID_GENERATOR.generate().toString();

return Pair.of(tokenId, Jwts.builder()
.setId(tokenId)
.setIssuer(SAIGON_PARKING_ISSUER)
.claim(USER_ROLE_KEY_NAME, userRole)
.claim(FACTOR_KEY_NAME, factor)
.claim(TOKEN_TYPE_KEY_NAME, type)
.setSubject(encryptedUserId.toString())
.setSubject(encryptUserId(userId, factor).toString())
.setIssuedAt(Date.from(now))
.setExpiration(Date.from(now.plus(timeAmount, timeUnit)))
.signWith(secretKey)
Expand Down
5 changes: 0 additions & 5 deletions service/booking-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
<artifactId>parkinglot-api</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.uuid</groupId>
<artifactId>java-uuid-generator</artifactId>
<version>${java-uuid-generator.version}</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
Expand Down

0 comments on commit 27db117

Please sign in to comment.