|
| 1 | +package com.somemore.auth.idpw.provider; |
| 2 | + |
| 3 | +import com.somemore.auth.authentication.JwtAuthenticationToken; |
| 4 | +import com.somemore.auth.jwt.domain.EncodedToken; |
| 5 | +import com.somemore.auth.jwt.domain.TokenType; |
| 6 | +import com.somemore.auth.jwt.domain.UserRole; |
| 7 | +import com.somemore.auth.jwt.usecase.JwtUseCase; |
| 8 | +import com.somemore.center.usecase.query.CenterSignUseCase; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import org.springframework.security.authentication.AuthenticationProvider; |
| 11 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 12 | +import org.springframework.security.core.Authentication; |
| 13 | +import org.springframework.security.core.AuthenticationException; |
| 14 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 15 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 16 | +import org.springframework.stereotype.Component; |
| 17 | +import org.springframework.transaction.annotation.Transactional; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +@Component |
| 22 | +@RequiredArgsConstructor |
| 23 | +@Transactional(readOnly = true) |
| 24 | +public class CustomAuthenticationProvider implements AuthenticationProvider { |
| 25 | + |
| 26 | + private final CenterSignUseCase centerSignUseCase; |
| 27 | + private final JwtUseCase jwtUseCase; |
| 28 | + private final PasswordEncoder passwordEncoder; |
| 29 | + |
| 30 | + @Override |
| 31 | + public Authentication authenticate(Authentication authentication) throws AuthenticationException { |
| 32 | + String accountId = authentication.getName(); |
| 33 | + String rawAccountPassword = authentication.getCredentials().toString(); |
| 34 | + |
| 35 | + String centerId = centerSignUseCase.getIdByAccountId(accountId).toString(); |
| 36 | + String encodedPassword = centerSignUseCase.getPasswordByAccountId(accountId); |
| 37 | + |
| 38 | + if (passwordEncoder.matches(rawAccountPassword, encodedPassword)) { |
| 39 | + EncodedToken accessToken = jwtUseCase.generateToken( |
| 40 | + centerId, |
| 41 | + UserRole.CENTER.getAuthority(), |
| 42 | + TokenType.ACCESS |
| 43 | + ); |
| 44 | + |
| 45 | + return new JwtAuthenticationToken( |
| 46 | + centerId, |
| 47 | + accessToken, |
| 48 | + List.of(new SimpleGrantedAuthority(UserRole.CENTER.getAuthority())) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean supports(Class<?> authentication) { |
| 57 | + return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication); |
| 58 | + } |
| 59 | +} |
0 commit comments