Skip to content

Commit

Permalink
refactor : Remove all additional claims logics
Browse files Browse the repository at this point in the history
  • Loading branch information
wwan13 committed Jun 7, 2024
1 parent 5ba75ee commit 363d4b0
Show file tree
Hide file tree
Showing 11 changed files with 4 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
public record PayloadAnalysis(
Class<?> payloadClazz,
Field subject,
Field roles,
Set<Field> additionalClaims
Field roles
) {

public Set<Field> allClaims() {
Set<Field> allClaims = new HashSet<>(additionalClaims);
Set<Field> allClaims = new HashSet<>();
allClaims.add(subject);
allClaims.add(roles);

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/io/wwan13/wintersecurity/jwt/PayloadParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

package io.wwan13.wintersecurity.jwt;

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

public interface PayloadParser {

String asSubject(Object payload);

Set<String> asRoles(Object payload);

Map<String, Object> asAdditionalClaims(Object payload);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Field> additionalClaims = findAdditionalClaimFields(payloadClazz);

return new PayloadAnalysis(payloadClazz, subject, roles, additionalClaims);
return new PayloadAnalysis(payloadClazz, subject, roles);
}

private Field findFieldByDeclaredAnnotation(
Expand All @@ -66,16 +64,4 @@ private void validateExistsOnlyOne(
throw new IllegalStateException(declared.getSimpleName() + " cannot be empty");
}
}

private Set<Field> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,6 @@ public Set<String> asRoles(Object payload) {
return Collections.singleton(Objects.toString(values));
}

@Override
public Map<String, Object> asAdditionalClaims(Object payload) {
Set<Field> fields = payloadAnalysis.additionalClaims();
Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String, Object> 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<String> 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<String, Object> result = payloadParser.asAdditionalClaims(payload);

// then
assertThat(result.keySet().size()).isEqualTo(2);
}

@Test
void should_KeyIsFieldName_when_ValueIsNotEntered() {
// given
final long subject = 1L;
final Set<String> 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<String, Object> result = payloadParser.asAdditionalClaims(payload);

// then
assertThat(result.keySet()).contains("annotated", "notAnnotated");
}

private PayloadAnalysis getPayloadAnalysis(Class<?> payloadClazz) {
PayloadAnalyst payloadAnalyst = new DefaultPayloadAnalyst();
return payloadAnalyst.analyze(payloadClazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -155,48 +154,4 @@ public JwtPayloadWithTwoRoles(long subject, Set<String> roles1, Set<String> role
this.roles2 = roles2;
}
}

static class JwtPayloadWithDataTypeAndWrapperClassClaims {
@Subject
long subject;
@Roles
Set<String> roles;
@Claim
long dataTypeClaim;
@Claim
Long wrapperClassClaim;

public JwtPayloadWithDataTypeAndWrapperClassClaims(
long subject, Set<String> 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<String> roles;
@Claim
long annotated;
long notAnnotated;

public JwtPayloadWithAnnotatedClaimAndNotAnnotatedClaim(
long subject,
Set<String> roles,
long annotated,
long notAnnotated
) {
this.subject = subject;
this.roles = roles;
this.annotated = annotated;
this.notAnnotated = notAnnotated;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,7 +37,6 @@ public static class TestPayload {
long id;
@Roles
String role;
@Claim
String claim;

public TestPayload(long id, String role, String claim) {
Expand Down

0 comments on commit 363d4b0

Please sign in to comment.