Skip to content

Commit

Permalink
refactor(auth): Remove base64 decoding of token service signing key (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
jjoyce0510 authored Dec 15, 2021
1 parent 70d0688 commit 1f53e0b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion metadata-service/auth-impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {

implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2',
'io.jsonwebtoken:jjwt-jackson:0.11.2' // or 'io.jsonwebtoken:jjwt-gson:0.11.2' for gson
'io.jsonwebtoken:jjwt-jackson:0.11.2'

compile externalDependency.lombok

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.ArrayList;
import java.util.Base64;
Expand Down Expand Up @@ -104,7 +105,7 @@ public String generateAccessToken(@Nonnull final String sub, @Nonnull final Map<
if (this.iss != null) {
builder.setIssuer(this.iss);
}
byte[] apiKeySecretBytes = Base64.getDecoder().decode(this.signingKey); // Key must be base64'd.
byte [] apiKeySecretBytes = this.signingKey.getBytes(StandardCharsets.UTF_8);
final Key signingKey = new SecretKeySpec(apiKeySecretBytes, this.signingAlgorithm.getJcaName());
return builder.signWith(signingKey, this.signingAlgorithm).compact();
}
Expand All @@ -118,8 +119,10 @@ public String generateAccessToken(@Nonnull final String sub, @Nonnull final Map<
public TokenClaims validateAccessToken(@Nonnull final String accessToken) throws TokenException {
Objects.requireNonNull(accessToken);
try {
byte [] apiKeySecretBytes = this.signingKey.getBytes(StandardCharsets.UTF_8);
final String base64Key = Base64.getEncoder().encodeToString(apiKeySecretBytes);
final Claims claims = (Claims) Jwts.parserBuilder()
.setSigningKey(this.signingKey)
.setSigningKey(base64Key)
.build()
.parse(accessToken)
.getBody();
Expand Down

0 comments on commit 1f53e0b

Please sign in to comment.