All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.server.security.internal.InternalAuthenticationConverter Maven / Gradle / Ivy

There is a newer version: 1.17.6
Show newest version
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());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy