Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
update :: TokenizerImpl.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Apr 12, 2024
1 parent 3385138 commit 82e6d18
Showing 1 changed file with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import io.jsonwebtoken.*;
import org.daemawiki.config.SecurityProperties;
import org.daemawiki.domain.user.application.FindUserPort;
import org.daemawiki.exception.h400.InvalidTokenException;
import org.daemawiki.exception.h404.UserNotFoundException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
Expand All @@ -15,15 +18,17 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Component
public class TokenizerImpl implements Tokenizer {
private final SecurityProperties securityProperties;
private final FindUserPort findUserPort;

public TokenizerImpl(SecurityProperties securityProperties) {
public TokenizerImpl(SecurityProperties securityProperties, FindUserPort findUserPort) {
this.securityProperties = securityProperties;
this.findUserPort = findUserPort;
}

@Override
Expand Down Expand Up @@ -51,29 +56,33 @@ private Tuple2<String, LocalDateTime> tokenize(String user) {
.compact(),
now);
}

private Mono<Jws<Claims>> parse(String token) {
return Mono.fromCallable(() -> Jwts.parser().setSigningKey(securityProperties.getSecret())
.parseClaimsJws(token));
.parseClaimsJws(token));
}

private Mono<Claims> parseClaims(String token) {
return parse(token)
.map(Jwt::getBody);
}

private UserDetails createAuthenticatedUserBySubject(String subject) {
return new User(subject, "", Collections.emptyList());
}

@Override
public Mono<Authentication> getAuthentication(String token) {
return parseClaims(token)
.map(claims -> createAuthenticatedUserBySubject(claims.getSubject()))
.flatMap(claims -> createAuthenticatedUserBySubject(claims.getSubject()))
.map(details -> new UsernamePasswordAuthenticationToken(
details, null, details.getAuthorities()));
}


private Mono<UserDetails> createAuthenticatedUserBySubject(String subject) {
return findUserPort.findByEmail(subject)
.switchIfEmpty(Mono.error(UserNotFoundException.EXCEPTION))
.flatMap(user -> Mono.justOrEmpty(new User(subject, "", List.of(
new SimpleGrantedAuthority("ROLE_" + user.getRole().name())
))));
}

private boolean validateIssuer(Claims claims) {
return claims.getIssuer()
.equals(securityProperties.getIssuer());
Expand Down

0 comments on commit 82e6d18

Please sign in to comment.