2
This commit is contained in:
@@ -32,6 +32,34 @@ public class JwtService {
|
||||
return jwt.sign(alg);
|
||||
}
|
||||
|
||||
public String signToken(Long userId, Long shopId, String phone, String provider, String email) {
|
||||
Instant now = Instant.now();
|
||||
Algorithm alg = Algorithm.HMAC256(props.getSecret() == null ? "dev-secret" : props.getSecret());
|
||||
var jwt = JWT.create()
|
||||
.withIssuer(props.getIssuer())
|
||||
.withIssuedAt(java.util.Date.from(now))
|
||||
.withExpiresAt(java.util.Date.from(now.plusSeconds(props.getTtlSeconds())))
|
||||
.withClaim("userId", userId)
|
||||
.withClaim("shopId", shopId)
|
||||
.withClaim("provider", provider);
|
||||
if (phone != null && !phone.isBlank()) jwt.withClaim("phone", phone);
|
||||
if (email != null && !email.isBlank()) jwt.withClaim("email", email);
|
||||
return jwt.sign(alg);
|
||||
}
|
||||
|
||||
public String signAdminToken(Long adminId, String username) {
|
||||
Instant now = Instant.now();
|
||||
Algorithm alg = Algorithm.HMAC256(props.getSecret() == null ? "dev-secret" : props.getSecret());
|
||||
var jwt = JWT.create()
|
||||
.withIssuer(props.getIssuer())
|
||||
.withIssuedAt(java.util.Date.from(now))
|
||||
.withExpiresAt(java.util.Date.from(now.plusSeconds(props.getTtlSeconds())))
|
||||
.withClaim("adminId", adminId)
|
||||
.withClaim("role", "admin");
|
||||
if (username != null && !username.isBlank()) jwt.withClaim("username", username);
|
||||
return jwt.sign(alg);
|
||||
}
|
||||
|
||||
public Map<String,Object> parseClaims(String authorizationHeader) {
|
||||
Map<String,Object> out = new HashMap<>();
|
||||
if (authorizationHeader == null || authorizationHeader.isBlank()) return out;
|
||||
@@ -48,9 +76,15 @@ public class JwtService {
|
||||
Long userId = jwt.getClaim("userId").asLong();
|
||||
Long shopId = jwt.getClaim("shopId").asLong();
|
||||
String phone = jwt.getClaim("phone").asString();
|
||||
String email = jwt.getClaim("email").asString();
|
||||
Long adminId = jwt.getClaim("adminId").asLong();
|
||||
String role = jwt.getClaim("role").asString();
|
||||
if (userId != null) out.put("userId", userId);
|
||||
if (shopId != null) out.put("shopId", shopId);
|
||||
if (phone != null && !phone.isBlank()) out.put("phone", phone);
|
||||
if (email != null && !email.isBlank()) out.put("email", email);
|
||||
if (adminId != null) out.put("adminId", adminId);
|
||||
if (role != null && !role.isBlank()) out.put("role", role);
|
||||
} catch (Exception ignore) { }
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user