Search in sources :

Example 1 with TipoUsuario

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");
    }
}
Also used : TipoUsuario(br.com.propague.api.model.TipoUsuario) Usuario(br.com.propague.api.model.Usuario) HashMap(java.util.HashMap) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date) IOException(java.io.IOException) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with TipoUsuario

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);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) TipoUsuario(br.com.propague.api.model.TipoUsuario) Usuario(br.com.propague.api.model.Usuario) ArrayList(java.util.ArrayList)

Example 3 with TipoUsuario

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);
}
Also used : TipoUsuario(br.com.propague.api.model.TipoUsuario) TipoUsuario(br.com.propague.api.model.TipoUsuario) Usuario(br.com.propague.api.model.Usuario)

Aggregations

TipoUsuario (br.com.propague.api.model.TipoUsuario)3 Usuario (br.com.propague.api.model.Usuario)3 JWTVerifier (com.auth0.jwt.JWTVerifier)1 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1