com.xlrit.gears.server.security.internal.InternalAuthenticationConverter Maven / Gradle / Ivy
package com.xlrit.gears.server.security.internal;
import java.util.List;
import java.util.stream.Collectors;
import com.xlrit.gears.base.model.Role;
import com.xlrit.gears.base.repository.RoleRepository;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
@RequiredArgsConstructor
class InternalAuthenticationConverter implements BearerConverter {
private final JWTVerifier verifier;
private final RoleRepository roleRepo;
@Override
public Authentication convert(String token) {
DecodedJWT decoded = verifier.verify(token);
String userId = decoded.getSubject();
List roleNames = getRoleNames(userId);
return new InternalAuthentication(decoded, roleNames);
}
private List getRoleNames(String userId) {
List roles = roleRepo.findByUserId(userId);
return roles.stream().map(Role::getName).collect(Collectors.toList());
}
}