From 363d4b079eaf08a0ac8407f688ba37655a04a55c Mon Sep 17 00:00:00 2001 From: wwan13 Date: Fri, 7 Jun 2024 16:45:21 +0900 Subject: [PATCH] refactor : Remove all additional claims logics --- .../wintersecurity/jwt/PayloadAnalysis.java | 5 +- .../wintersecurity/jwt/PayloadParser.java | 3 - .../jwt/payload/annotation/Claim.java | 25 ------- .../support/DefaultPayloadAnalyst.java | 16 +---- .../jwt/payload/support/JwtPayloadParser.java | 11 ---- .../jwt/provider/JwtTokenGenerator.java | 2 - .../support/DefaultPayloadAnalystTest.java | 4 +- .../payload/support/PayloadParserTest.java | 66 ------------------- .../jwt/payload/support/TestJwtPayloads.java | 45 ------------- .../jwt/provider/JwtTokenDecoderTest.java | 1 - .../jwt/provider/ProviderTestContainer.java | 2 - 11 files changed, 4 insertions(+), 176 deletions(-) delete mode 100644 src/main/java/io/wwan13/wintersecurity/jwt/payload/annotation/Claim.java diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalysis.java b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalysis.java index e6c4c4b..2194a23 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalysis.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalysis.java @@ -23,12 +23,11 @@ public record PayloadAnalysis( Class payloadClazz, Field subject, - Field roles, - Set additionalClaims + Field roles ) { public Set allClaims() { - Set allClaims = new HashSet<>(additionalClaims); + Set allClaims = new HashSet<>(); allClaims.add(subject); allClaims.add(roles); diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java index 7f99cd3..f5ad897 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java @@ -16,7 +16,6 @@ package io.wwan13.wintersecurity.jwt; -import java.util.Map; import java.util.Set; public interface PayloadParser { @@ -24,6 +23,4 @@ public interface PayloadParser { String asSubject(Object payload); Set asRoles(Object payload); - - Map asAdditionalClaims(Object payload); } diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/payload/annotation/Claim.java b/src/main/java/io/wwan13/wintersecurity/jwt/payload/annotation/Claim.java deleted file mode 100644 index be7fe2d..0000000 --- a/src/main/java/io/wwan13/wintersecurity/jwt/payload/annotation/Claim.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.wwan13.wintersecurity.jwt.payload.annotation; - -import java.lang.annotation.*; - -@Documented -@Target(ElementType.FIELD) -@Retention(RetentionPolicy.RUNTIME) -public @interface Claim { -} diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalyst.java b/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalyst.java index 633c87b..4d34d5f 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalyst.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalyst.java @@ -18,7 +18,6 @@ import io.wwan13.wintersecurity.jwt.PayloadAnalysis; import io.wwan13.wintersecurity.jwt.PayloadAnalyst; -import io.wwan13.wintersecurity.jwt.payload.annotation.Claim; import io.wwan13.wintersecurity.jwt.payload.annotation.Roles; import io.wwan13.wintersecurity.jwt.payload.annotation.Subject; @@ -37,9 +36,8 @@ public class DefaultPayloadAnalyst implements PayloadAnalyst { public PayloadAnalysis analyze(Class payloadClazz) { Field subject = findFieldByDeclaredAnnotation(payloadClazz, Subject.class); Field roles = findFieldByDeclaredAnnotation(payloadClazz, Roles.class); - Set additionalClaims = findAdditionalClaimFields(payloadClazz); - return new PayloadAnalysis(payloadClazz, subject, roles, additionalClaims); + return new PayloadAnalysis(payloadClazz, subject, roles); } private Field findFieldByDeclaredAnnotation( @@ -66,16 +64,4 @@ private void validateExistsOnlyOne( throw new IllegalStateException(declared.getSimpleName() + " cannot be empty"); } } - - private Set findAdditionalClaimFields( - Class payloadClazz - ) { - return Arrays.stream(payloadClazz.getDeclaredFields()) - .filter(this::isAdditionalClaim) - .collect(Collectors.toUnmodifiableSet()); - } - - private boolean isAdditionalClaim(Field field) { - return field.isAnnotationPresent(Claim.class) || field.getDeclaredAnnotations().length == 0; - } } diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/JwtPayloadParser.java b/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/JwtPayloadParser.java index bb28d1f..69e77b0 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/JwtPayloadParser.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/JwtPayloadParser.java @@ -56,17 +56,6 @@ public Set asRoles(Object payload) { return Collections.singleton(Objects.toString(values)); } - @Override - public Map asAdditionalClaims(Object payload) { - Set fields = payloadAnalysis.additionalClaims(); - Map additionalClaims = new HashMap<>(); - - fields.forEach(field -> - additionalClaims.put(field.getName(), getFieldValue(payload, field))); - - return additionalClaims; - } - private Object getFieldValue(Object payload, Field field) { try { field.setAccessible(true); diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGenerator.java b/src/main/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGenerator.java index c73b515..ff097f6 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGenerator.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGenerator.java @@ -53,7 +53,6 @@ public String accessToken(Object payload) { .setExpiration(DateUtil.addFromNow(properties.accessTokenValidity())) .claim(PAYLOAD_KEY_TOKEN_TYPE, TOKEN_TYPE_ACCESS) .claim(PAYLOAD_KEY_USER_ROLE, RoleSerializer.serialize(payloadParser.asRoles(payload))) - .addClaims(payloadParser.asAdditionalClaims(payload)) .signWith(secretKey.value()) .compact(); } @@ -66,7 +65,6 @@ public String refreshToken(Object payload) { .setExpiration(DateUtil.addFromNow(properties.refreshTokenValidity())) .claim(PAYLOAD_KEY_TOKEN_TYPE, TOKEN_TYPE_REFRESH) .claim(PAYLOAD_KEY_USER_ROLE, RoleSerializer.serialize(payloadParser.asRoles(payload))) - .addClaims(payloadParser.asAdditionalClaims(payload)) .signWith(secretKey.value()) .compact(); } diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalystTest.java b/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalystTest.java index 151366a..edd2f28 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalystTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalystTest.java @@ -34,7 +34,7 @@ class DefaultPayloadAnalystTest extends UnitTest { void should_AnalyzePayloadFields() { // given final Class payloadClazz = - TestJwtPayloads.JwtPayloadWithDataTypeAndWrapperClassClaims.class; + TestJwtPayloads.JwtPayloadWithWrapperClassSubject.class; // when PayloadAnalysis payloadAnalysis = payloadAnalyst.analyze(payloadClazz); @@ -43,8 +43,6 @@ void should_AnalyzePayloadFields() { assertThat(payloadAnalysis.payloadClazz()).isInstanceOf(Class.class); assertThat(payloadAnalysis.subject().getName()).isEqualTo("subject"); assertThat(payloadAnalysis.roles().getName()).isEqualTo("roles"); - assertThat(payloadAnalysis.additionalClaims().stream().map(Field::getName)) - .contains("wrapperClassClaim", "dataTypeClaim"); } @Test diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/PayloadParserTest.java b/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/PayloadParserTest.java index 2b3d80d..ce897b5 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/PayloadParserTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/PayloadParserTest.java @@ -156,72 +156,6 @@ void should_FindRoles_when_RolesFieldNameIsNotRoles() { assertThat(result).isEqualTo(authorities); } - @Test - void should_ConvertToObjectMap_when_BothDataTypeAndWrapperClassClaims() { - // given - final long subject = 1L; - final Set roles = Set.of("role"); - final long dataTypeClaim = 1L; - final Long wrapperClassClaim = 1L; - Object payload = new TestJwtPayloads - .JwtPayloadWithDataTypeAndWrapperClassClaims(subject, roles, dataTypeClaim, wrapperClassClaim); - - PayloadAnalysis payloadAnalysis = - getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithDataTypeAndWrapperClassClaims.class); - PayloadParser payloadParser = new JwtPayloadParser(payloadAnalysis); - - // when - Map result = payloadParser.asAdditionalClaims(payload); - - // then - result.keySet().forEach(key -> - assertThat(result.get(key)) - .isEqualTo(1L) - .isInstanceOf(Object.class) - - ); - } - - @Test - void should_ConvertToObjectMap_when_BothAnnotationIsExistAndNotExist() { - // given - final long subject = 1L; - final Set roles = Set.of("role"); - final long claim = 1L; - Object payload = new TestJwtPayloads - .JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim(subject, roles, claim, claim); - - PayloadAnalysis payloadAnalysis = - getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim.class); - PayloadParser payloadParser = new JwtPayloadParser(payloadAnalysis); - - // when - Map result = payloadParser.asAdditionalClaims(payload); - - // then - assertThat(result.keySet().size()).isEqualTo(2); - } - - @Test - void should_KeyIsFieldName_when_ValueIsNotEntered() { - // given - final long subject = 1L; - final Set roles = Set.of("role"); - final long claim = 1L; - Object payload = new TestJwtPayloads - .JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim(subject, roles, claim, claim); - - PayloadAnalysis payloadAnalysis = - getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim.class); - PayloadParser payloadParser = new JwtPayloadParser(payloadAnalysis); - - // when - Map result = payloadParser.asAdditionalClaims(payload); - - // then - assertThat(result.keySet()).contains("annotated", "notAnnotated"); - } - private PayloadAnalysis getPayloadAnalysis(Class payloadClazz) { PayloadAnalyst payloadAnalyst = new DefaultPayloadAnalyst(); return payloadAnalyst.analyze(payloadClazz); diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/TestJwtPayloads.java b/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/TestJwtPayloads.java index c1b609a..24d4ed3 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/TestJwtPayloads.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/TestJwtPayloads.java @@ -16,7 +16,6 @@ package io.wwan13.wintersecurity.jwt.payload.support; -import io.wwan13.wintersecurity.jwt.payload.annotation.Claim; import io.wwan13.wintersecurity.jwt.payload.annotation.Roles; import io.wwan13.wintersecurity.jwt.payload.annotation.Subject; @@ -155,48 +154,4 @@ public JwtPayloadWithTwoRoles(long subject, Set roles1, Set role this.roles2 = roles2; } } - - static class JwtPayloadWithDataTypeAndWrapperClassClaims { - @Subject - long subject; - @Roles - Set roles; - @Claim - long dataTypeClaim; - @Claim - Long wrapperClassClaim; - - public JwtPayloadWithDataTypeAndWrapperClassClaims( - long subject, Set roles, - long dataTypeClaim, - Long wrapperClassClaim - ) { - this.subject = subject; - this.roles = roles; - this.dataTypeClaim = dataTypeClaim; - this.wrapperClassClaim = wrapperClassClaim; - } - } - - static class JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim { - @Subject - long subject; - @Roles - Set roles; - @Claim - long annotated; - long notAnnotated; - - public JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim( - long subject, - Set roles, - long annotated, - long notAnnotated - ) { - this.subject = subject; - this.roles = roles; - this.annotated = annotated; - this.notAnnotated = notAnnotated; - } - } } diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenDecoderTest.java b/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenDecoderTest.java index 6d1021c..4957db4 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenDecoderTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenDecoderTest.java @@ -54,7 +54,6 @@ void should_DecodeToken() { // then assertThat(decodedClaims.get("sub")).isEqualTo(Objects.toString(id)); assertThat(decodedClaims.get("roles")).isEqualTo(RoleSerializer.serialize(Set.of(role))); - assertThat(decodedClaims.get("claim")).isEqualTo(claim); } @Test diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/provider/ProviderTestContainer.java b/src/test/java/io/wwan13/wintersecurity/jwt/provider/ProviderTestContainer.java index c444f6f..2b9ad22 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/provider/ProviderTestContainer.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/provider/ProviderTestContainer.java @@ -22,7 +22,6 @@ import io.wwan13.wintersecurity.jwt.PayloadParser; import io.wwan13.wintersecurity.jwt.TokenDecoder; import io.wwan13.wintersecurity.jwt.TokenGenerator; -import io.wwan13.wintersecurity.jwt.payload.annotation.Claim; import io.wwan13.wintersecurity.jwt.payload.annotation.Roles; import io.wwan13.wintersecurity.jwt.payload.annotation.Subject; import io.wwan13.wintersecurity.jwt.payload.support.JwtPayloadParser; @@ -38,7 +37,6 @@ public static class TestPayload { long id; @Roles String role; - @Claim String claim; public TestPayload(long id, String role, String claim) {