style(simple-jwt): Upgraded method to get Algorithm declared in third-party implementation.

This commit is contained in:
Zihlu Wang
2023-08-03 18:59:11 +08:00
parent c53124bb61
commit 14450b6489
3 changed files with 7 additions and 8 deletions
@@ -140,7 +140,7 @@ public class AuthzeroTokenResolver implements TokenResolver<DecodedJWT> {
throw new IllegalArgumentException("A secret is required to build a JSON Web Token."); throw new IllegalArgumentException("A secret is required to build a JSON Web Token.");
} }
this.jtiCreator = jtiCreator; this.jtiCreator = jtiCreator;
this.algorithm = AuthzeroTokenResolverConfig.getInstance().getFunction(algorithm).apply(secret); this.algorithm = AuthzeroTokenResolverConfig.getInstance().getAlgorithm(algorithm).apply(secret);
this.issuer = issuer; this.issuer = issuer;
this.verifier = JWT.require(this.algorithm).build(); this.verifier = JWT.require(this.algorithm).build();
} }
@@ -140,7 +140,7 @@ public final class AuthzeroTokenResolverConfig implements TokenResolverConfig<Fu
* implementation * implementation
*/ */
@Override @Override
public Function<String, Algorithm> getFunction(TokenAlgorithm algorithm) { public Function<String, Algorithm> getAlgorithm(TokenAlgorithm algorithm) {
return Optional.of(SUPPORTED_ALGORITHMS).map((entry) -> entry.get(algorithm)) return Optional.of(SUPPORTED_ALGORITHMS).map((entry) -> entry.get(algorithm))
.orElseThrow(() -> new UnsupportedAlgorithmException("The specified algorithm is not supported yet.")); .orElseThrow(() -> new UnsupportedAlgorithmException("The specified algorithm is not supported yet."));
} }
@@ -30,14 +30,13 @@ import cn.org.codecrafters.simplejwt.constants.TokenAlgorithm;
* specifying algorithm functions corresponding to different TokenAlgorithm * specifying algorithm functions corresponding to different TokenAlgorithm
* instances supported by the TokenResolver implementation. * instances supported by the TokenResolver implementation.
* *
* * @param <Algo> the type representing algorithm functions used by the
* @param <AlgorithmFunction> the type representing algorithm functions used by * {@code TokenResolver}
* the TokenResolver
* @author Zihlu Wang * @author Zihlu Wang
* @version 1.0.0 * @version 1.0.0
* @since 1.0.0 * @since 1.0.0
*/ */
public interface TokenResolverConfig<AlgorithmFunction> { public interface TokenResolverConfig<Algo> {
/** /**
* Gets the algorithm function corresponding to the specified * Gets the algorithm function corresponding to the specified
@@ -53,6 +52,6 @@ public interface TokenResolverConfig<AlgorithmFunction> {
* required * required
* @return the algorithm function associated with the given TokenAlgorithm * @return the algorithm function associated with the given TokenAlgorithm
*/ */
AlgorithmFunction getFunction(TokenAlgorithm algorithm); Algo getAlgorithm(TokenAlgorithm algorithm);
} }