diff --git a/build.gradle b/build.gradle index 8ac371c..c92894b 100644 --- a/build.gradle +++ b/build.gradle @@ -1,40 +1,43 @@ plugins { - id 'java' - id 'org.springframework.boot' version '2.7.13' - id 'io.spring.dependency-management' version '1.1.4' + id 'java' + id 'org.springframework.boot' version '2.7.13' + id 'io.spring.dependency-management' version '1.1.4' } group = 'io.wwan13' version = '0.0.1' java { - sourceCompatibility = '17' + sourceCompatibility = '17' } repositories { - mavenCentral() + mavenCentral() } dependencies { - // spring - implementation 'org.springframework.boot:spring-boot-starter' + // spring + implementation 'org.springframework.boot:spring-boot-starter' - // web - implementation 'org.springframework.boot:spring-boot-starter-web' + // web + implementation 'org.springframework.boot:spring-boot-starter-web' - // jwt - implementation 'io.jsonwebtoken:jjwt-api:0.11.5' - runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5' - runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5' + // jwt + implementation 'io.jsonwebtoken:jjwt-api:0.11.5' + runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5' + runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5' - // password encoder - implementation 'org.springframework.security:spring-security-crypto:6.1.0' + // password encoder + implementation 'org.springframework.security:spring-security-crypto:6.1.0' - // test - testImplementation 'org.springframework.boot:spring-boot-starter-test' - testImplementation 'org.junit.jupiter:junit-jupiter' + // reflections api + implementation 'org.reflections:reflections:0.9.10' + + // test + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'org.junit.jupiter:junit-jupiter' } tasks.named('test') { - useJUnitPlatform() + useJUnitPlatform() } diff --git a/src/main/java/io/wwan13/wintersecurity/config/JwtConfiguration.java b/src/main/java/io/wwan13/wintersecurity/config/JwtConfiguration.java index d25117f..045a239 100644 --- a/src/main/java/io/wwan13/wintersecurity/config/JwtConfiguration.java +++ b/src/main/java/io/wwan13/wintersecurity/config/JwtConfiguration.java @@ -20,10 +20,12 @@ import io.wwan13.wintersecurity.jwt.PayloadAnalysis; import io.wwan13.wintersecurity.jwt.PayloadAnalyst; import io.wwan13.wintersecurity.jwt.PayloadParser; +import io.wwan13.wintersecurity.jwt.PayloadScanner; import io.wwan13.wintersecurity.jwt.TokenDecoder; import io.wwan13.wintersecurity.jwt.TokenGenerator; import io.wwan13.wintersecurity.jwt.payload.support.JwtPayloadParser; -import io.wwan13.wintersecurity.jwt.payload.support.ReflectionPayloadAnalyst; +import io.wwan13.wintersecurity.jwt.payload.support.DefaultPayloadAnalyst; +import io.wwan13.wintersecurity.jwt.payload.support.ReflectionPayloadScanner; import io.wwan13.wintersecurity.jwt.provider.JwtTokenDecoder; import io.wwan13.wintersecurity.jwt.provider.JwtTokenGenerator; import io.wwan13.wintersecurity.secretkey.SecretKey; @@ -47,8 +49,11 @@ public TokenDecoder tokenDecoder(SecretKey secretKey) { @Bean public PayloadAnalysis payloadAnalysis(JwtProperties jwtProperties) { - PayloadAnalyst payloadAnalyst = new ReflectionPayloadAnalyst(); - return payloadAnalyst.analyze(jwtProperties); + PayloadScanner payloadScanner = new ReflectionPayloadScanner(); + PayloadAnalyst payloadAnalyst = new DefaultPayloadAnalyst(); + + Class scannedPayload = payloadScanner.scan(); + return payloadAnalyst.analyze(scannedPayload); } @Bean diff --git a/src/main/java/io/wwan13/wintersecurity/constant/Constants.java b/src/main/java/io/wwan13/wintersecurity/constant/Constants.java index d30c87d..9054786 100644 --- a/src/main/java/io/wwan13/wintersecurity/constant/Constants.java +++ b/src/main/java/io/wwan13/wintersecurity/constant/Constants.java @@ -16,7 +16,6 @@ package io.wwan13.wintersecurity.constant; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.payload.DefaultPayload; public class Constants { @@ -24,8 +23,6 @@ public class Constants { // Default Option public static final long DEFAULT_ACCESS_TOKEN_VALIDITY = 10000; public static final long DEFAULT_REFRESH_TOKEN_VALIDITY = 10000; - public static final Class DEFAULT_PAYLOAD_CLAZZ = DefaultPayload.class; - public static final Class DEFAULT_SUBJECT_CLAZZ = Object.class; // Payload public static final String PAYLOAD_KEY_TOKEN_TYPE = "token_type"; diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/JwtProperties.java b/src/main/java/io/wwan13/wintersecurity/jwt/JwtProperties.java index ac4495d..c9b2901 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/JwtProperties.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/JwtProperties.java @@ -18,8 +18,6 @@ public record JwtProperties( long accessTokenValidity, - long refreshTokenValidity, - Class payloadClazz, - Class subjectClazz + long refreshTokenValidity ) { } diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalysis.java b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalysis.java index 0f73b08..e6c4c4b 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalysis.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalysis.java @@ -21,7 +21,7 @@ import java.util.Set; public record PayloadAnalysis( - Class payloadClazz, + Class payloadClazz, Field subject, Field roles, Set additionalClaims diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalyst.java b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalyst.java index 7321139..c32afd8 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalyst.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadAnalyst.java @@ -18,5 +18,5 @@ public interface PayloadAnalyst { - PayloadAnalysis analyze(JwtProperties jwtProperties); + PayloadAnalysis analyze(Class payloadClazz); } diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java index 13f925f..7f99cd3 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java @@ -21,7 +21,9 @@ public interface PayloadParser { - String asSubject(Payload payload); - Set asRoles(Payload payload); - Map asAdditionalClaims(Payload payload); + String asSubject(Object payload); + + Set asRoles(Object payload); + + Map asAdditionalClaims(Object payload); } diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/Payload.java b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadScanner.java similarity index 73% rename from src/main/java/io/wwan13/wintersecurity/jwt/Payload.java rename to src/main/java/io/wwan13/wintersecurity/jwt/PayloadScanner.java index 0416050..4fd9b6d 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/Payload.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/PayloadScanner.java @@ -16,13 +16,7 @@ package io.wwan13.wintersecurity.jwt; -import io.wwan13.wintersecurity.jwt.payload.DefaultPayload; +public interface PayloadScanner { -import java.util.Set; - -public interface Payload { - - static DefaultPayload of(Object subject, Set roles) { - return new DefaultPayload(subject, roles); - } + Class scan(); } diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/TokenGenerator.java b/src/main/java/io/wwan13/wintersecurity/jwt/TokenGenerator.java index b10dcaf..cdd80ff 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/TokenGenerator.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/TokenGenerator.java @@ -17,6 +17,8 @@ package io.wwan13.wintersecurity.jwt; public interface TokenGenerator { - String accessToken(Payload payload); - String refreshToken(Payload payload); + + String accessToken(Object payload); + + String refreshToken(Object payload); } diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/payload/DefaultPayload.java b/src/main/java/io/wwan13/wintersecurity/jwt/payload/DefaultPayload.java index 8bf9a6a..1649bc4 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/payload/DefaultPayload.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/payload/DefaultPayload.java @@ -16,13 +16,12 @@ package io.wwan13.wintersecurity.jwt.payload; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.payload.annotation.Roles; import io.wwan13.wintersecurity.jwt.payload.annotation.Subject; import java.util.Set; -public class DefaultPayload implements Payload { +public class DefaultPayload { @Subject private Object subject; diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/payload/annotation/Payload.java b/src/main/java/io/wwan13/wintersecurity/jwt/payload/annotation/Payload.java new file mode 100644 index 0000000..2fb421a --- /dev/null +++ b/src/main/java/io/wwan13/wintersecurity/jwt/payload/annotation/Payload.java @@ -0,0 +1,29 @@ +/* + * 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; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Documented +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface Payload { +} diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadAnalyst.java b/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalyst.java similarity index 87% rename from src/main/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadAnalyst.java rename to src/main/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalyst.java index 38eec61..633c87b 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadAnalyst.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalyst.java @@ -16,8 +16,6 @@ package io.wwan13.wintersecurity.jwt.payload.support; -import io.wwan13.wintersecurity.jwt.JwtProperties; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.PayloadAnalysis; import io.wwan13.wintersecurity.jwt.PayloadAnalyst; import io.wwan13.wintersecurity.jwt.payload.annotation.Claim; @@ -31,14 +29,12 @@ import java.util.Set; import java.util.stream.Collectors; -public class ReflectionPayloadAnalyst implements PayloadAnalyst { +public class DefaultPayloadAnalyst implements PayloadAnalyst { private static final int FIRST_ELEMENT_INDEX = 0; @Override - public PayloadAnalysis analyze(JwtProperties jwtProperties) { - Class payloadClazz = jwtProperties.payloadClazz(); - + public PayloadAnalysis analyze(Class payloadClazz) { Field subject = findFieldByDeclaredAnnotation(payloadClazz, Subject.class); Field roles = findFieldByDeclaredAnnotation(payloadClazz, Roles.class); Set additionalClaims = findAdditionalClaimFields(payloadClazz); @@ -47,7 +43,7 @@ public PayloadAnalysis analyze(JwtProperties jwtProperties) { } private Field findFieldByDeclaredAnnotation( - Class payloadClazz, + Class payloadClazz, Class declared ) { List fields = Arrays.stream(payloadClazz.getDeclaredFields()) @@ -72,7 +68,7 @@ private void validateExistsOnlyOne( } private Set findAdditionalClaimFields( - Class payloadClazz + Class payloadClazz ) { return Arrays.stream(payloadClazz.getDeclaredFields()) .filter(this::isAdditionalClaim) 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 d8411f0..bb28d1f 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 @@ -16,7 +16,6 @@ package io.wwan13.wintersecurity.jwt.payload.support; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.PayloadAnalysis; import io.wwan13.wintersecurity.jwt.PayloadParser; @@ -38,13 +37,13 @@ public JwtPayloadParser(PayloadAnalysis payloadAnalysis) { } @Override - public String asSubject(Payload payload) { + public String asSubject(Object payload) { Field field = payloadAnalysis.subject(); return Objects.toString(getFieldValue(payload, field)); } @Override - public Set asRoles(Payload payload) { + public Set asRoles(Object payload) { Field field = payloadAnalysis.roles(); Object values = getFieldValue(payload, field); @@ -58,7 +57,7 @@ public Set asRoles(Payload payload) { } @Override - public Map asAdditionalClaims(Payload payload) { + public Map asAdditionalClaims(Object payload) { Set fields = payloadAnalysis.additionalClaims(); Map additionalClaims = new HashMap<>(); @@ -68,7 +67,7 @@ public Map asAdditionalClaims(Payload payload) { return additionalClaims; } - private Object getFieldValue(Payload payload, Field field) { + private Object getFieldValue(Object payload, Field field) { try { field.setAccessible(true); return field.get(payload); diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadScanner.java b/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadScanner.java new file mode 100644 index 0000000..4646ef5 --- /dev/null +++ b/src/main/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadScanner.java @@ -0,0 +1,53 @@ +/* + * 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.support; + +import io.wwan13.wintersecurity.jwt.PayloadScanner; +import io.wwan13.wintersecurity.jwt.payload.DefaultPayload; +import io.wwan13.wintersecurity.jwt.payload.annotation.Payload; +import org.reflections.Reflections; +import org.reflections.scanners.SubTypesScanner; +import org.reflections.scanners.TypeAnnotationsScanner; + +import java.util.Set; + +public class ReflectionPayloadScanner implements PayloadScanner { + + private static final String DEFAULT_SCAN_BASE_PACKAGE = ""; + + @Override + public Class scan() { + Reflections reflections = new Reflections( + DEFAULT_SCAN_BASE_PACKAGE, + new TypeAnnotationsScanner(), + new SubTypesScanner() + ); + + Set> payloads = reflections.getTypesAnnotatedWith(Payload.class); + validateMoreThanTwo(payloads); + + return payloads.stream() + .findFirst() + .orElse(DefaultPayload.class); + } + + public void validateMoreThanTwo(Set> payloads) { + if (payloads.size() > 1) { + throw new IllegalStateException("Must be only one payload."); + } + } +} 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 fa6e366..c73b515 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGenerator.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGenerator.java @@ -18,7 +18,6 @@ import io.jsonwebtoken.Jwts; import io.wwan13.wintersecurity.jwt.JwtProperties; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.PayloadParser; import io.wwan13.wintersecurity.jwt.TokenGenerator; import io.wwan13.wintersecurity.jwt.payload.util.RoleSerializer; @@ -47,8 +46,7 @@ public JwtTokenGenerator( } @Override - public String accessToken(Payload payload) { - validatePayloadClazz(payload.getClass()); + public String accessToken(Object payload) { return Jwts.builder() .setSubject(payloadParser.asSubject(payload)) .setIssuedAt(DateUtil.now()) @@ -61,8 +59,7 @@ public String accessToken(Payload payload) { } @Override - public String refreshToken(Payload payload) { - validatePayloadClazz(payload.getClass()); + public String refreshToken(Object payload) { return Jwts.builder() .setSubject(payloadParser.asSubject(payload)) .setIssuedAt(DateUtil.now()) @@ -73,10 +70,4 @@ public String refreshToken(Payload payload) { .signWith(secretKey.value()) .compact(); } - - private void validatePayloadClazz(Class clazz) { - if (!clazz.equals(properties.payloadClazz())) { - throw new IllegalArgumentException("The type of payload between set and entered do not match."); - } - } } diff --git a/src/main/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesRegistry.java b/src/main/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesRegistry.java index f8e9ecc..9a73153 100644 --- a/src/main/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesRegistry.java +++ b/src/main/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesRegistry.java @@ -17,21 +17,14 @@ package io.wwan13.wintersecurity.jwt.support; import io.wwan13.wintersecurity.jwt.JwtProperties; -import io.wwan13.wintersecurity.jwt.Payload; - -import java.util.Objects; import static io.wwan13.wintersecurity.constant.Constants.DEFAULT_ACCESS_TOKEN_VALIDITY; -import static io.wwan13.wintersecurity.constant.Constants.DEFAULT_PAYLOAD_CLAZZ; import static io.wwan13.wintersecurity.constant.Constants.DEFAULT_REFRESH_TOKEN_VALIDITY; -import static io.wwan13.wintersecurity.constant.Constants.DEFAULT_SUBJECT_CLAZZ; public class JwtPropertiesRegistry { private long accessTokenValidity; private long refreshTokenValidity; - private Class payloadClazz; - private Class subjectClazz; public JwtPropertiesRegistry accessTokenValidity(long accessTokenValidityInSecond) { this.accessTokenValidity = accessTokenValidityInSecond; @@ -43,22 +36,10 @@ public JwtPropertiesRegistry refreshTokenValidity(long refreshTokenValidityInSec return this; } - public JwtPropertiesRegistry payloadClazz(Class payloadClazz) { - this.payloadClazz = payloadClazz; - return this; - } - - public JwtPropertiesRegistry subjectClazz(Class subjectClazz) { - this.subjectClazz = subjectClazz; - return this; - } - protected JwtProperties apply() { return new JwtProperties( existOrDefaultValidity(accessTokenValidity, DEFAULT_ACCESS_TOKEN_VALIDITY), - existOrDefaultValidity(refreshTokenValidity, DEFAULT_REFRESH_TOKEN_VALIDITY), - existOrDefaultPayload(payloadClazz), - existOrDefaultSubject(subjectClazz) + existOrDefaultValidity(refreshTokenValidity, DEFAULT_REFRESH_TOKEN_VALIDITY) ); } @@ -68,18 +49,4 @@ private long existOrDefaultValidity(long validityInSecond, long defaultValidityI } return validityInSecond; } - - private Class existOrDefaultPayload(Class payloadClazz) { - if (Objects.isNull(payloadClazz)) { - return DEFAULT_PAYLOAD_CLAZZ; - } - return payloadClazz; - } - - private Class existOrDefaultSubject(Class subjectClazz) { - if (Objects.isNull(subjectClazz)) { - return DEFAULT_SUBJECT_CLAZZ; - } - return subjectClazz; - } } diff --git a/src/test/java/io/wwan13/wintersecurity/auth/processor/InterceptorAuthProcessorTest.java b/src/test/java/io/wwan13/wintersecurity/auth/processor/InterceptorAuthProcessorTest.java index dbd6a95..bfa4481 100644 --- a/src/test/java/io/wwan13/wintersecurity/auth/processor/InterceptorAuthProcessorTest.java +++ b/src/test/java/io/wwan13/wintersecurity/auth/processor/InterceptorAuthProcessorTest.java @@ -26,7 +26,6 @@ import io.wwan13.wintersecurity.auth.stub.StubHttpServletRequest; import io.wwan13.wintersecurity.exception.forbidden.ForbiddenException; import io.wwan13.wintersecurity.exception.unauthirized.UnauthorizedException; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.provider.ProviderTestContainer; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -77,7 +76,7 @@ void should_PassWithoutException_when_ValidRequestWithValidTokenEntered( request.getMethodWillReturn(method); request.getRequestUriWillReturn(uri); - final Payload payload = new ProviderTestContainer.TestPayload(1, role, "claim"); + final Object payload = new ProviderTestContainer.TestPayload(1, role, "claim"); final String token = ProviderTestContainer.tokenGenerator.accessToken(payload); final String bearerToken = "Bearer " + token; request.getHeaderWillReturn(bearerToken); @@ -102,7 +101,7 @@ void should_ThrowForbiddenException_when_InValidRequestWithValidTokenEntered( request.getMethodWillReturn(method); request.getRequestUriWillReturn(uri); - final Payload payload = new ProviderTestContainer.TestPayload(1, role, "claim"); + final Object payload = new ProviderTestContainer.TestPayload(1, role, "claim"); final String token = ProviderTestContainer.tokenGenerator.accessToken(payload); final String bearerToken = "Bearer " + token; request.getHeaderWillReturn(bearerToken); diff --git a/src/test/java/io/wwan13/wintersecurity/auth/provider/BearerTokenExtractorTest.java b/src/test/java/io/wwan13/wintersecurity/auth/provider/BearerTokenExtractorTest.java index 165d695..c98d1bd 100644 --- a/src/test/java/io/wwan13/wintersecurity/auth/provider/BearerTokenExtractorTest.java +++ b/src/test/java/io/wwan13/wintersecurity/auth/provider/BearerTokenExtractorTest.java @@ -19,7 +19,6 @@ import io.wwan13.wintersecurity.UnitTest; import io.wwan13.wintersecurity.auth.AuthTestContainer; import io.wwan13.wintersecurity.auth.stub.StubHttpServletRequest; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.provider.ProviderTestContainer; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -36,7 +35,7 @@ class BearerTokenExtractorTest extends UnitTest { @Test void should_ExtractTokenInHttpRequestHeader() { // given - final Payload payload = new ProviderTestContainer.TestPayload(1, "role", "claim"); + final Object payload = new ProviderTestContainer.TestPayload(1, "role", "claim"); final String token = ProviderTestContainer.tokenGenerator.accessToken(payload); final StubHttpServletRequest request = new StubHttpServletRequest(); @@ -73,7 +72,7 @@ void should_ReturnEmptyOptional_when_InvalidBearerPrefix( final String bearerPrefix ) { // given - final Payload payload = new ProviderTestContainer.TestPayload(1, "role", "claim"); + final Object payload = new ProviderTestContainer.TestPayload(1, "role", "claim"); final String token = ProviderTestContainer.tokenGenerator.accessToken(payload); final StubHttpServletRequest request = new StubHttpServletRequest(); diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/JwtPropertiesTest.java b/src/test/java/io/wwan13/wintersecurity/jwt/JwtPropertiesTest.java index 73e0ea1..a2906a0 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/JwtPropertiesTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/JwtPropertiesTest.java @@ -29,21 +29,15 @@ void should_CreateJwtPropertiesClass_when_DateEntered() { // given final long accessTokenValidity = 1000L; final long refreshTokenValidity = 1000L; - final Class payloadClass = DefaultPayload.class; - final Class subjectClass = long.class; // when JwtProperties jwtProperties = new JwtProperties( accessTokenValidity, - refreshTokenValidity, - payloadClass, - subjectClass + refreshTokenValidity ); // then assertThat(jwtProperties.accessTokenValidity()).isEqualTo(accessTokenValidity); assertThat(jwtProperties.refreshTokenValidity()).isEqualTo(refreshTokenValidity); - assertThat(jwtProperties.payloadClazz()).isEqualTo(payloadClass); - assertThat(jwtProperties.subjectClazz()).isEqualTo(subjectClass); } } \ No newline at end of file diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadAnalystTest.java b/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalystTest.java similarity index 59% rename from src/test/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadAnalystTest.java rename to src/test/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalystTest.java index 57ab3b8..151366a 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/ReflectionPayloadAnalystTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/payload/support/DefaultPayloadAnalystTest.java @@ -17,12 +17,8 @@ package io.wwan13.wintersecurity.jwt.payload.support; import io.wwan13.wintersecurity.UnitTest; -import io.wwan13.wintersecurity.jwt.JwtProperties; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.PayloadAnalysis; import io.wwan13.wintersecurity.jwt.PayloadAnalyst; -import io.wwan13.wintersecurity.jwt.support.JwtPropertiesApplier; -import io.wwan13.wintersecurity.jwt.support.JwtPropertiesRegistry; import org.junit.jupiter.api.Test; import java.lang.reflect.Field; @@ -30,18 +26,18 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -class ReflectionPayloadAnalystTest extends UnitTest { +class DefaultPayloadAnalystTest extends UnitTest { - static PayloadAnalyst payloadAnalyst = new ReflectionPayloadAnalyst(); + static PayloadAnalyst payloadAnalyst = new DefaultPayloadAnalyst(); @Test void should_AnalyzePayloadFields() { // given - JwtProperties jwtProperties = - getJwtProperties(TestJwtPayloads.JwtPayloadWithDataTypeAndWrapperClassClaims.class); + final Class payloadClazz = + TestJwtPayloads.JwtPayloadWithDataTypeAndWrapperClassClaims.class; // when - PayloadAnalysis payloadAnalysis = payloadAnalyst.analyze(jwtProperties); + PayloadAnalysis payloadAnalysis = payloadAnalyst.analyze(payloadClazz); // then assertThat(payloadAnalysis.payloadClazz()).isInstanceOf(Class.class); @@ -54,52 +50,40 @@ void should_AnalyzePayloadFields() { @Test void should_ThrowException_when_NoSubjectDeclared() { // given - JwtProperties jwtProperties = - getJwtProperties(TestJwtPayloads.JwtPayloadWithNoSubject.class); + final Class payloadClazz = TestJwtPayloads.JwtPayloadWithNoSubject.class; // when, then - assertThatThrownBy(() -> payloadAnalyst.analyze(jwtProperties)) + assertThatThrownBy(() -> payloadAnalyst.analyze(payloadClazz)) .isInstanceOf(IllegalStateException.class); } @Test void should_ThrowException_when_MoreThanTwoSubjectDeclared() { // given - JwtProperties jwtProperties = - getJwtProperties(TestJwtPayloads.JwtPayloadWithTwoSubject.class); + final Class payloadClazz = TestJwtPayloads.JwtPayloadWithTwoSubject.class; // when, then - assertThatThrownBy(() -> payloadAnalyst.analyze(jwtProperties)) + assertThatThrownBy(() -> payloadAnalyst.analyze(payloadClazz)) .isInstanceOf(IllegalStateException.class); } @Test void should_ThrowException_when_NoRolesDeclared() { // given - JwtProperties jwtProperties = - getJwtProperties(TestJwtPayloads.JwtPayloadWithNoRoles.class); + final Class payloadClazz = TestJwtPayloads.JwtPayloadWithNoRoles.class; // when, then - assertThatThrownBy(() -> payloadAnalyst.analyze(jwtProperties)) + assertThatThrownBy(() -> payloadAnalyst.analyze(payloadClazz)) .isInstanceOf(IllegalStateException.class); } @Test void should_ThrowException_when_MoreThanTwoRolesDeclared() { // given - JwtProperties jwtProperties = - getJwtProperties(TestJwtPayloads.JwtPayloadWithTwoRoles.class); + final Class payloadClazz = TestJwtPayloads.JwtPayloadWithTwoRoles.class; // when, then - assertThatThrownBy(() -> payloadAnalyst.analyze(jwtProperties)) + assertThatThrownBy(() -> payloadAnalyst.analyze(payloadClazz)) .isInstanceOf(IllegalStateException.class); } - - private JwtProperties getJwtProperties(Class payloadClazz) { - return JwtPropertiesApplier.apply( - new JwtPropertiesRegistry() - .payloadClazz(payloadClazz) - .subjectClazz(long.class) - ); - } } \ No newline at end of file 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 356a3b8..2b3d80d 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 @@ -17,13 +17,9 @@ package io.wwan13.wintersecurity.jwt.payload.support; import io.wwan13.wintersecurity.UnitTest; -import io.wwan13.wintersecurity.jwt.JwtProperties; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.PayloadAnalysis; import io.wwan13.wintersecurity.jwt.PayloadAnalyst; import io.wwan13.wintersecurity.jwt.PayloadParser; -import io.wwan13.wintersecurity.jwt.support.JwtPropertiesApplier; -import io.wwan13.wintersecurity.jwt.support.JwtPropertiesRegistry; import org.junit.jupiter.api.Test; import java.util.Map; @@ -39,7 +35,7 @@ void should_SubjectConvertToString_when_SubjectIsWrapperClass() { // given final Long subject = 1L; final Set roles = Set.of("role"); - Payload payload = new TestJwtPayloads.JwtPayloadWithWrapperClassSubject(subject, roles); + Object payload = new TestJwtPayloads.JwtPayloadWithWrapperClassSubject(subject, roles); PayloadAnalysis payloadAnalysis = getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithWrapperClassSubject.class); @@ -57,7 +53,7 @@ void should_SubjectConvertToString_when_SubjectIsDataType() { // given final long subject = 1L; final Set roles = Set.of("role"); - Payload payload = new TestJwtPayloads.JwtPayloadWithDataTypeSubject(subject, roles); + Object payload = new TestJwtPayloads.JwtPayloadWithDataTypeSubject(subject, roles); PayloadAnalysis payloadAnalysis = getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithDataTypeSubject.class); @@ -75,7 +71,7 @@ void should_FindSubject_when_SubjectFieldNameIsNotSubject() { // given final long id = 1L; final Set roles = Set.of("role"); - Payload payload = new TestJwtPayloads.JwtPayloadWithSubjectFieldNameId(id, roles); + Object payload = new TestJwtPayloads.JwtPayloadWithSubjectFieldNameId(id, roles); PayloadAnalysis payloadAnalysis = getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithSubjectFieldNameId.class); @@ -93,7 +89,7 @@ void should_RolesConvertToSet_when_RolesIsCollectionType() { // given final long subject = 1L; final Set roles = Set.of("role"); - Payload payload = new TestJwtPayloads.JwtPayloadWithCollectionClassRoles(subject, roles); + Object payload = new TestJwtPayloads.JwtPayloadWithCollectionClassRoles(subject, roles); PayloadAnalysis payloadAnalysis = getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithCollectionClassRoles.class); @@ -111,7 +107,7 @@ void should_RolesConvertToSet_when_RolesIsNoneCollectionType() { // given final long subject = 1L; final String roles = "role"; - Payload payload = new TestJwtPayloads.JwtPayloadWithNoneCollectionClassRoles(subject, roles); + Object payload = new TestJwtPayloads.JwtPayloadWithNoneCollectionClassRoles(subject, roles); PayloadAnalysis payloadAnalysis = getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithNoneCollectionClassRoles.class); @@ -129,7 +125,7 @@ void should_RolesConvertToStringSet_when_RolesIsOtherObjectSet() { // given final long subject = 1L; final Set roles = Set.of(1, 2); - Payload payload = new TestJwtPayloads.JwtPayloadWithOtherObjectSetRoles(subject, roles); + Object payload = new TestJwtPayloads.JwtPayloadWithOtherObjectSetRoles(subject, roles); PayloadAnalysis payloadAnalysis = getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithOtherObjectSetRoles.class); @@ -147,7 +143,7 @@ void should_FindRoles_when_RolesFieldNameIsNotRoles() { // given final long subject = 1L; final Set authorities = Set.of("role"); - Payload payload = new TestJwtPayloads.JwtPayloadWithRolesFieldNameAuthorities(subject, authorities); + Object payload = new TestJwtPayloads.JwtPayloadWithRolesFieldNameAuthorities(subject, authorities); PayloadAnalysis payloadAnalysis = getPayloadAnalysis(TestJwtPayloads.JwtPayloadWithRolesFieldNameAuthorities.class); @@ -167,7 +163,7 @@ void should_ConvertToObjectMap_when_BothDataTypeAndWrapperClassClaims() { final Set roles = Set.of("role"); final long dataTypeClaim = 1L; final Long wrapperClassClaim = 1L; - Payload payload = new TestJwtPayloads + Object payload = new TestJwtPayloads .JwtPayloadWithDataTypeAndWrapperClassClaims(subject, roles, dataTypeClaim, wrapperClassClaim); PayloadAnalysis payloadAnalysis = @@ -192,7 +188,7 @@ void should_ConvertToObjectMap_when_BothAnnotationIsExistAndNotExist() { final long subject = 1L; final Set roles = Set.of("role"); final long claim = 1L; - Payload payload = new TestJwtPayloads + Object payload = new TestJwtPayloads .JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim(subject, roles, claim, claim); PayloadAnalysis payloadAnalysis = @@ -212,7 +208,7 @@ void should_KeyIsFieldName_when_ValueIsNotEntered() { final long subject = 1L; final Set roles = Set.of("role"); final long claim = 1L; - Payload payload = new TestJwtPayloads + Object payload = new TestJwtPayloads .JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim(subject, roles, claim, claim); PayloadAnalysis payloadAnalysis = @@ -226,13 +222,8 @@ void should_KeyIsFieldName_when_ValueIsNotEntered() { assertThat(result.keySet()).contains("annotated", "notAnnotated"); } - private PayloadAnalysis getPayloadAnalysis(Class payloadClazz) { - JwtProperties jwtProperties = JwtPropertiesApplier.apply( - new JwtPropertiesRegistry() - .payloadClazz(payloadClazz) - .subjectClazz(long.class) - ); - PayloadAnalyst payloadAnalyst = new ReflectionPayloadAnalyst(); - return payloadAnalyst.analyze(jwtProperties); + private PayloadAnalysis getPayloadAnalysis(Class payloadClazz) { + PayloadAnalyst payloadAnalyst = new DefaultPayloadAnalyst(); + return payloadAnalyst.analyze(payloadClazz); } } \ No newline at end of file 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 3a2a568..c1b609a 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; import io.wwan13.wintersecurity.jwt.payload.annotation.Claim; import io.wwan13.wintersecurity.jwt.payload.annotation.Roles; import io.wwan13.wintersecurity.jwt.payload.annotation.Subject; @@ -25,9 +24,11 @@ public class TestJwtPayloads { - static class JwtPayloadWithWrapperClassSubject implements Payload { - @Subject Long subject; - @Roles Set roles; + static class JwtPayloadWithWrapperClassSubject { + @Subject + Long subject; + @Roles + Set roles; public JwtPayloadWithWrapperClassSubject(Long subject, Set roles) { this.subject = subject; @@ -35,9 +36,11 @@ public JwtPayloadWithWrapperClassSubject(Long subject, Set roles) { } } - static class JwtPayloadWithDataTypeSubject implements Payload { - @Subject long subject; - @Roles Set roles; + static class JwtPayloadWithDataTypeSubject { + @Subject + long subject; + @Roles + Set roles; public JwtPayloadWithDataTypeSubject(long subject, Set roles) { this.subject = subject; @@ -45,9 +48,11 @@ public JwtPayloadWithDataTypeSubject(long subject, Set roles) { } } - static class JwtPayloadWithCollectionClassRoles implements Payload { - @Subject long subject; - @Roles Set roles; + static class JwtPayloadWithCollectionClassRoles { + @Subject + long subject; + @Roles + Set roles; public JwtPayloadWithCollectionClassRoles(long subject, Set roles) { this.subject = subject; @@ -55,9 +60,11 @@ public JwtPayloadWithCollectionClassRoles(long subject, Set roles) { } } - static class JwtPayloadWithNoneCollectionClassRoles implements Payload { - @Subject long subject; - @Roles String roles; + static class JwtPayloadWithNoneCollectionClassRoles { + @Subject + long subject; + @Roles + String roles; public JwtPayloadWithNoneCollectionClassRoles(long subject, String roles) { this.subject = subject; @@ -65,9 +72,11 @@ public JwtPayloadWithNoneCollectionClassRoles(long subject, String roles) { } } - static class JwtPayloadWithOtherObjectSetRoles implements Payload { - @Subject long subject; - @Roles Set roles; + static class JwtPayloadWithOtherObjectSetRoles { + @Subject + long subject; + @Roles + Set roles; public JwtPayloadWithOtherObjectSetRoles(long subject, Set roles) { this.subject = subject; @@ -75,9 +84,11 @@ public JwtPayloadWithOtherObjectSetRoles(long subject, Set roles) { } } - static class JwtPayloadWithSubjectFieldNameId implements Payload { - @Subject long id; - @Roles Set roles; + static class JwtPayloadWithSubjectFieldNameId { + @Subject + long id; + @Roles + Set roles; public JwtPayloadWithSubjectFieldNameId(long id, Set roles) { this.id = id; @@ -85,9 +96,11 @@ public JwtPayloadWithSubjectFieldNameId(long id, Set roles) { } } - static class JwtPayloadWithRolesFieldNameAuthorities implements Payload { - @Subject long id; - @Roles Set authorities; + static class JwtPayloadWithRolesFieldNameAuthorities { + @Subject + long id; + @Roles + Set authorities; public JwtPayloadWithRolesFieldNameAuthorities(long id, Set authorities) { this.id = id; @@ -95,38 +108,46 @@ public JwtPayloadWithRolesFieldNameAuthorities(long id, Set authorities) } } - static class JwtPayloadWithNoSubject implements Payload { - @Roles Set roles; + static class JwtPayloadWithNoSubject { + @Roles + Set roles; public JwtPayloadWithNoSubject(Set roles) { this.roles = roles; } } - static class JwtPayloadWithTwoSubject implements Payload { - @Subject long subject1; - @Subject long subject2; - @Roles Set roles; + static class JwtPayloadWithTwoSubject { + @Subject + long subject1; + @Subject + long subject2; + @Roles + Set roles; - public JwtPayloadWithTwoSubject(long subject1, long subject2, Set roles) { + public JwtPayloadWithTwoSubject(long subject1, long subject2, Set roles) { this.subject1 = subject1; this.subject2 = subject2; this.roles = roles; } } - static class JwtPayloadWithNoRoles implements Payload { - @Subject long subject; + static class JwtPayloadWithNoRoles { + @Subject + long subject; public JwtPayloadWithNoRoles(long subject) { this.subject = subject; } } - static class JwtPayloadWithTwoRoles implements Payload { - @Subject long subject; - @Roles Set roles1; - @Roles Set roles2; + static class JwtPayloadWithTwoRoles { + @Subject + long subject; + @Roles + Set roles1; + @Roles + Set roles2; public JwtPayloadWithTwoRoles(long subject, Set roles1, Set roles2) { this.subject = subject; @@ -135,11 +156,15 @@ public JwtPayloadWithTwoRoles(long subject, Set roles1, Set role } } - static class JwtPayloadWithDataTypeAndWrapperClassClaims implements Payload { - @Subject long subject; - @Roles Set roles; - @Claim long dataTypeClaim; - @Claim Long wrapperClassClaim; + static class JwtPayloadWithDataTypeAndWrapperClassClaims { + @Subject + long subject; + @Roles + Set roles; + @Claim + long dataTypeClaim; + @Claim + Long wrapperClassClaim; public JwtPayloadWithDataTypeAndWrapperClassClaims( long subject, Set roles, @@ -153,10 +178,13 @@ public JwtPayloadWithDataTypeAndWrapperClassClaims( } } - static class JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim implements Payload { - @Subject long subject; - @Roles Set roles; - @Claim long annotated; + static class JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim { + @Subject + long subject; + @Roles + Set roles; + @Claim + long annotated; long notAnnotated; public JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim( 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 ce57390..6d1021c 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenDecoderTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenDecoderTest.java @@ -19,7 +19,6 @@ 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.Payload; import io.wwan13.wintersecurity.jwt.TokenDecoder; import io.wwan13.wintersecurity.jwt.TokenGenerator; import io.wwan13.wintersecurity.jwt.payload.util.RoleSerializer; @@ -45,7 +44,7 @@ void should_DecodeToken() { final long id = 1L; final String role = "role"; final String claim = "claim"; - Payload payload = new ProviderTestContainer.TestPayload(id, role, claim); + Object payload = new ProviderTestContainer.TestPayload(id, role, claim); String accessToken = tokenGenerator.accessToken(payload); @@ -78,7 +77,6 @@ void should_ThrowException_when_ExpiredTokenEntered() { final JwtProperties properties = JwtPropertiesApplier.apply( new JwtPropertiesRegistry() .accessTokenValidity(-1L) - .payloadClazz(ProviderTestContainer.TestPayload.class) ); final TokenGenerator tokenGenerator = new JwtTokenGenerator(secretKey, properties, ProviderTestContainer.payloadParser); @@ -87,7 +85,7 @@ void should_ThrowException_when_ExpiredTokenEntered() { final long id = 1L; final String role = "role"; final String claim = "claim"; - Payload payload = new ProviderTestContainer.TestPayload(id, role, claim); + Object payload = new ProviderTestContainer.TestPayload(id, role, claim); final String invalidToken = tokenGenerator.accessToken(payload); diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGeneratorTest.java b/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGeneratorTest.java index 6ee9529..a55cda6 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGeneratorTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/provider/JwtTokenGeneratorTest.java @@ -16,25 +16,24 @@ package io.wwan13.wintersecurity.jwt.provider; -import io.wwan13.wintersecurity.jwt.Payload; import org.junit.jupiter.api.Test; import static io.wwan13.wintersecurity.jwt.provider.ProviderTestContainer.tokenGenerator; import static org.assertj.core.api.Assertions.assertThat; class JwtTokenGeneratorTest { - + @Test void should_CreateAccessToken() { // given final long id = 1L; final String role = "role"; final String claim = "claim"; - Payload payload = new ProviderTestContainer.TestPayload(id, role, claim); - + Object payload = new ProviderTestContainer.TestPayload(id, role, claim); + // when String accessToken = tokenGenerator.accessToken(payload); - + // then assertThat(accessToken).isNotEmpty(); } @@ -45,7 +44,7 @@ void should_CreateRefreshToken() { final long id = 1L; final String role = "role"; final String claim = "claim"; - Payload payload = new ProviderTestContainer.TestPayload(id, role, claim); + Object payload = new ProviderTestContainer.TestPayload(id, role, claim); // when String refreshToken = tokenGenerator.refreshToken(payload); 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 319ba40..c444f6f 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/provider/ProviderTestContainer.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/provider/ProviderTestContainer.java @@ -17,7 +17,6 @@ package io.wwan13.wintersecurity.jwt.provider; import io.wwan13.wintersecurity.jwt.JwtProperties; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.PayloadAnalysis; import io.wwan13.wintersecurity.jwt.PayloadAnalyst; import io.wwan13.wintersecurity.jwt.PayloadParser; @@ -27,14 +26,14 @@ import io.wwan13.wintersecurity.jwt.payload.annotation.Roles; import io.wwan13.wintersecurity.jwt.payload.annotation.Subject; import io.wwan13.wintersecurity.jwt.payload.support.JwtPayloadParser; -import io.wwan13.wintersecurity.jwt.payload.support.ReflectionPayloadAnalyst; +import io.wwan13.wintersecurity.jwt.payload.support.DefaultPayloadAnalyst; import io.wwan13.wintersecurity.jwt.support.JwtPropertiesApplier; import io.wwan13.wintersecurity.jwt.support.JwtPropertiesRegistry; import io.wwan13.wintersecurity.secretkey.SecretKey; public class ProviderTestContainer { - public static class TestPayload implements Payload { + public static class TestPayload { @Subject long id; @Roles @@ -72,13 +71,11 @@ public String getClaim() { new JwtPropertiesRegistry() .accessTokenValidity(100000000000L) .refreshTokenValidity(100000000000L) - .payloadClazz(TestPayload.class) - .subjectClazz(long.class) ); public static PayloadAnalysis payloadAnalysis() { - PayloadAnalyst payloadAnalyst = new ReflectionPayloadAnalyst(); - return payloadAnalyst.analyze(jwtProperties); + PayloadAnalyst payloadAnalyst = new DefaultPayloadAnalyst(); + return payloadAnalyst.analyze(TestPayload.class); } public static PayloadParser payloadParser = new JwtPayloadParser(payloadAnalysis()); diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesApplierTest.java b/src/test/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesApplierTest.java index 4c0238f..c310a17 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesApplierTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesApplierTest.java @@ -18,7 +18,6 @@ import io.wwan13.wintersecurity.UnitTest; import io.wwan13.wintersecurity.jwt.JwtProperties; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.payload.DefaultPayload; import org.junit.jupiter.api.Test; @@ -32,14 +31,10 @@ void should_ReturnJwtProperties_when_RegistryEntered() { final String secretKey = "secretkey123123123123123123123123123123123123123123123123"; final long accessTokenValidity = 1000L; final long refreshTokenValidity = 1000L; - final Class payloadClass = DefaultPayload.class; - final Class subjectClass = long.class; JwtPropertiesRegistry registry = new JwtPropertiesRegistry() .accessTokenValidity(accessTokenValidity) - .refreshTokenValidity(refreshTokenValidity) - .payloadClazz(payloadClass) - .subjectClazz(subjectClass); + .refreshTokenValidity(refreshTokenValidity); // when JwtProperties jwtProperties = JwtPropertiesApplier.apply(registry); @@ -48,7 +43,5 @@ void should_ReturnJwtProperties_when_RegistryEntered() { assertThat(jwtProperties).isInstanceOf(JwtProperties.class); assertThat(jwtProperties.accessTokenValidity()).isEqualTo(accessTokenValidity); assertThat(jwtProperties.refreshTokenValidity()).isEqualTo(refreshTokenValidity); - assertThat(jwtProperties.payloadClazz()).isEqualTo(payloadClass); - assertThat(jwtProperties.subjectClazz()).isEqualTo(subjectClass); } } \ No newline at end of file diff --git a/src/test/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesRegistryTest.java b/src/test/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesRegistryTest.java index 8ddedb4..36643ec 100644 --- a/src/test/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesRegistryTest.java +++ b/src/test/java/io/wwan13/wintersecurity/jwt/support/JwtPropertiesRegistryTest.java @@ -18,7 +18,6 @@ import io.wwan13.wintersecurity.constant.Constants; import io.wwan13.wintersecurity.jwt.JwtProperties; -import io.wwan13.wintersecurity.jwt.Payload; import io.wwan13.wintersecurity.jwt.payload.DefaultPayload; import org.junit.jupiter.api.Test; @@ -32,27 +31,21 @@ void should_CreateJwtPropertiesClass_when_UsingJwtPropertiesRegistry() { // given final long accessTokenValidity = 1000L; final long refreshTokenValidity = 1000L; - final Class payloadClass = DefaultPayload.class; - final Class subjectClass = long.class; // when JwtProperties jwtProperties = new JwtPropertiesRegistry() .accessTokenValidity(accessTokenValidity) .refreshTokenValidity(refreshTokenValidity) - .payloadClazz(payloadClass) - .subjectClazz(subjectClass) .apply(); // then assertThat(jwtProperties).isInstanceOf(JwtProperties.class); assertThat(jwtProperties.accessTokenValidity()).isEqualTo(accessTokenValidity); assertThat(jwtProperties.refreshTokenValidity()).isEqualTo(refreshTokenValidity); - assertThat(jwtProperties.payloadClazz()).isEqualTo(payloadClass); - assertThat(jwtProperties.subjectClazz()).isEqualTo(subjectClass); } @Test - void should_ReplaceToDefaultValue_when_ValidityAndClazzValuesNotEntered() { + void should_ReplaceToDefaultValue_when_ValidityNotEntered() { // given, when JwtProperties jwtProperties = new JwtPropertiesRegistry() .apply(); @@ -60,7 +53,5 @@ void should_ReplaceToDefaultValue_when_ValidityAndClazzValuesNotEntered() { // then assertThat(jwtProperties.accessTokenValidity()).isEqualTo(Constants.DEFAULT_ACCESS_TOKEN_VALIDITY); assertThat(jwtProperties.refreshTokenValidity()).isEqualTo(Constants.DEFAULT_REFRESH_TOKEN_VALIDITY); - assertThat(jwtProperties.payloadClazz()).isEqualTo(Constants.DEFAULT_PAYLOAD_CLAZZ); - assertThat(jwtProperties.subjectClazz()).isEqualTo(Constants.DEFAULT_SUBJECT_CLAZZ); } } \ No newline at end of file