feat(simple-jwt): Create random secret and apply to TokenResolver

This commit is contained in:
Zihlu Wang
2023-08-04 00:39:48 +08:00
parent bad0fb048c
commit 9405908964
@@ -18,6 +18,7 @@
package cn.org.codecrafters.simplejwt.authzero;
import cn.org.codecrafters.guid.GuidCreator;
import cn.org.codecrafters.simplejwt.SecretCreator;
import cn.org.codecrafters.simplejwt.TokenPayload;
import cn.org.codecrafters.simplejwt.TokenResolver;
import cn.org.codecrafters.simplejwt.annotations.ExcludeFromPayload;
@@ -203,6 +204,25 @@ public class AuthzeroTokenResolver implements TokenResolver<DecodedJWT> {
this.verifier = JWT.require(this.algorithm).build();
}
/**
* Creates a new instance of AuthzeroTokenResolver with the provided
* configurations, HMAC256 algorithm and a simple UUID GuidCreator.
*
* @param issuer the issuer claim value to be included in JWT tokens
*/
public AuthzeroTokenResolver(String issuer) {
var secret = SecretCreator.createSecret(32, true, true, true);
this.jtiCreator = (GuidCreator<UUID>) UUID::randomUUID;
this.algorithm = AuthzeroTokenResolverConfig.getInstance()
.getAlgorithm(TokenAlgorithm.HS256)
.apply(secret);
this.issuer = issuer;
this.verifier = JWT.require(this.algorithm).build();
log.info("The secret has been set to {}.", secret);
}
/**
* Builds the basic information of the JSON Web Token (JWT) using the
* provided parameters and adds it to the JWTCreator.Builder.