use of br.com.propague.api.model.TipoUsuario in project UPE_2021_2_Propague by netrometro.
the class TipoParaUsuarioForm method refreshToken.
@GetMapping("/token/refresh")
public void refreshToken(HttpServletRequest request, HttpServletResponse response) throws IOException {
String authorizationHeader = request.getHeader("Authorization");
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
try {
String refresh_token = authorizationHeader.substring(7);
Algorithm algorithm = Algorithm.HMAC256("secret".getBytes());
JWTVerifier verifier = JWT.require(algorithm).build();
DecodedJWT decodedJWT = verifier.verify(refresh_token);
String username = decodedJWT.getSubject();
Usuario usuario = servico.getUsuario(username);
String acces_token = com.auth0.jwt.JWT.create().withSubject(usuario.getEmail()).withExpiresAt(new Date(System.currentTimeMillis() + 10 * 60 * 1000)).withIssuer(request.getRequestURL().toString()).withClaim("tipo", usuario.getTipos().stream().map(TipoUsuario::getNome).collect(Collectors.joining())).sign(algorithm);
// response.setHeader("acces_token", token);
// response.setHeader("refresh_token", refresh_token);
Map<String, String> map = new HashMap<>();
map.put("token", acces_token);
map.put("refresh_token", refresh_token);
response.setContentType(APPLICATION_JSON_VALUE);
new ObjectMapper().writeValue(response.getOutputStream(), map);
} catch (Exception e) {
response.setHeader("error", e.getMessage());
response.setStatus(403);
Map<String, String> map = new HashMap<>();
map.put("error", e.getMessage());
response.setContentType(MimeTypeUtils.APPLICATION_JSON_VALUE);
new ObjectMapper().writeValue(response.getOutputStream(), map);
}
} else {
throw new RuntimeException("Refresh token is missing");
}
}
use of br.com.propague.api.model.TipoUsuario in project UPE_2021_2_Propague by netrometro.
the class UsuarioService method loadUserByUsername.
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Usuario usuario = repositorioUsuario.findByEmail(username);
if (usuario == null) {
log.error("Usuário não encontrado: " + username);
throw new UsernameNotFoundException("Usuário não encontrado");
} else {
log.info("Usuário encontrado: " + username);
}
Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
usuario.getTipos().forEach(tipoUsuario -> authorities.add(new SimpleGrantedAuthority(tipoUsuario.getNome())));
return new org.springframework.security.core.userdetails.User(usuario.getEmail(), usuario.getSenha(), authorities);
}
use of br.com.propague.api.model.TipoUsuario in project UPE_2021_2_Propague by netrometro.
the class UsuarioService method atribuirTipoAoUsuario.
@Override
public void atribuirTipoAoUsuario(String email, String tipoUsuarioNome) {
Usuario usuario = repositorioUsuario.findByEmail(email);
TipoUsuario tipo = repositorioTipoUsuario.findByNome(tipoUsuarioNome);
usuario.getTipos().add(tipo);
}
Aggregations