Skip to content

Commit

Permalink
refactor : Change tokenDecoder return class Map to TokenClaims
Browse files Browse the repository at this point in the history
  • Loading branch information
wwan13 committed Jun 7, 2024
1 parent 6fe0bca commit 995797d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
import io.wwan13.wintersecurity.auth.RequestStorage;
import io.wwan13.wintersecurity.auth.TokenExtractor;
import io.wwan13.wintersecurity.constant.Constants;
import io.wwan13.wintersecurity.jwt.TokenClaims;
import io.wwan13.wintersecurity.jwt.TokenDecoder;
import io.wwan13.wintersecurity.jwt.payload.util.RoleSerializer;
import org.springframework.http.HttpMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public class InterceptorAuthProcessor extends AbstractInterceptorAuthProcessor {

Expand Down Expand Up @@ -57,16 +56,15 @@ private void actionIfTokenPresent(
HttpServletRequest request,
RequestStorage storage
) {
Map<String, Object> claims = tokenDecoder.decode(token);
String rawRoles = (String) claims.get(Constants.PAYLOAD_KEY_USER_ROLE);
TokenClaims claims = tokenDecoder.decode(token);

accessManager.manageWithAuthentication(
HttpMethod.resolve(request.getMethod()),
request.getRequestURI(),
RoleSerializer.deserialize(rawRoles)
claims.getRoles()
);

storage.saveAll(claims);
storage.save(Constants.ATTRIBUTE_CLAIMS_KEY, claims);
}

private void actionIfTokenAbsent(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class Constants {
public static final String PAYLOAD_KEY_USER_ROLE = "roles";
public static final String DEFAULT_SUBJECT_KEY = "sub";

// Attribute
public static final String ATTRIBUTE_CLAIMS_KEY = "claims";

// Token
public static final String TOKEN_TYPE_ACCESS = "access_token";
public static final String TOKEN_TYPE_REFRESH = "refresh_token";
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/io/wwan13/wintersecurity/jwt/TokenDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package io.wwan13.wintersecurity.jwt;

import java.util.Map;

public interface TokenDecoder {
Map<String, Object> decode(String token);
TokenClaims decode(String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.jsonwebtoken.*;
import io.wwan13.wintersecurity.exception.unauthirized.ExpiredJwtTokenException;
import io.wwan13.wintersecurity.exception.unauthirized.InvalidJwtTokenException;
import io.wwan13.wintersecurity.jwt.TokenClaims;
import io.wwan13.wintersecurity.jwt.TokenDecoder;
import io.wwan13.wintersecurity.secretkey.SecretKey;

Expand All @@ -34,8 +35,9 @@ public JwtTokenDecoder(SecretKey secretKey) {
}

@Override
public Map<String, Object> decode(String token) {
return parseClaimsWithExceptionHandling(token);
public TokenClaims decode(String token) {
Map<String, Object> claims = parseClaimsWithExceptionHandling(token);
return new TokenClaims(claims);
}

public Claims parseClaimsWithExceptionHandling(String token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.wwan13.wintersecurity.exception.unauthirized.ExpiredJwtTokenException;
import io.wwan13.wintersecurity.exception.unauthirized.InvalidJwtTokenException;
import io.wwan13.wintersecurity.jwt.JwtProperties;
import io.wwan13.wintersecurity.jwt.TokenClaims;
import io.wwan13.wintersecurity.jwt.TokenDecoder;
import io.wwan13.wintersecurity.jwt.TokenGenerator;
import io.wwan13.wintersecurity.jwt.payload.util.RoleSerializer;
Expand All @@ -27,7 +28,6 @@
import io.wwan13.wintersecurity.secretkey.SecretKey;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

Expand All @@ -49,7 +49,7 @@ void should_DecodeToken() {
String accessToken = tokenGenerator.accessToken(payload);

// when
Map<String, Object> decodedClaims = tokenDecoder.decode(accessToken);
TokenClaims decodedClaims = tokenDecoder.decode(accessToken);

// then
assertThat(decodedClaims.get("sub")).isEqualTo(Objects.toString(id));
Expand Down

0 comments on commit 995797d

Please sign in to comment.