Search in sources :

Example 1 with AuthUser

use of elbar.company.learn_center_rest.entity.auth.user.AuthUser in project learn-center-rest by elbar-org.

the class AuthUserServiceImpl method create.

@Override
public ResponseEntity<Data<Void>> create(AuthUserCreateDTO DTO) {
    PasswordEncoderConfigurer encoderConfigurer = new PasswordEncoderConfigurer();
    AuthUser user = mapper.toCreateDTO(DTO);
    user.setPassword(encoderConfigurer.encoder().encode(user.getPassword()));
    repository.save(user);
    return new ResponseEntity<>(new Data<>(true), HttpStatus.CREATED);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) AuthUser(elbar.company.learn_center_rest.entity.auth.user.AuthUser) PasswordEncoderConfigurer(elbar.company.learn_center_rest.configs.encryption.PasswordEncoderConfigurer)

Example 2 with AuthUser

use of elbar.company.learn_center_rest.entity.auth.user.AuthUser in project learn-center-rest by elbar-org.

the class AuthTokenServiceImpl method refreshToken.

@Override
public void refreshToken(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
    if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
        try {
            String refreshToken = authorizationHeader.substring("Bearer ".length());
            DecodedJWT jwt = JWTUtils.getVerifier().verify(refreshToken);
            String userCode = jwt.getSubject();
            AuthUser user = authUserRepository.findByCode(UUID.fromString(userCode));
            Date accessDate = JWTUtils.getExpiry();
            String accessToken = JWT.create().withSubject(user.getCode().toString()).withExpiresAt(accessDate).withIssuer(request.getRequestURL().toString()).withClaim("roles", authUserRepository.getRolesByCode(UUID.fromString(userCode))).sign(JWTUtils.getAlgorithm());
            AuthTokenCreateDTO tokenCreateDTO = new AuthTokenCreateDTO(UUID.fromString(userCode), accessToken, accessDate, AuthTokenTypeEnum.ACCESS_TOKEN.name());
            create(tokenCreateDTO);
            AuthTokenGetDTO tokenGetDTO1 = new AuthTokenGetDTO(AuthTokenTypeEnum.ACCESS_TOKEN.name(), accessToken);
            AuthTokenGetDTO tokenGetDTO2 = new AuthTokenGetDTO(AuthTokenTypeEnum.REFRESH_TOKEN.name(), refreshToken);
            List<AuthTokenGetDTO> tokenGetDTOList = new ArrayList<>();
            tokenGetDTOList.add(tokenGetDTO1);
            tokenGetDTOList.add(tokenGetDTO2);
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            new ObjectMapper().writeValue(response.getOutputStream(), tokenGetDTOList);
        } catch (Exception e) {
            response.setHeader("error", e.getMessage());
            response.setStatus(HttpStatus.FORBIDDEN.value());
            Map<String, String> error = new HashMap<>();
            error.put("error_message", e.getMessage());
            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
            new ObjectMapper().writeValue(response.getOutputStream(), error);
        }
    } else {
        throw new RuntimeException("Refresh token is missing");
    }
}
Also used : AuthUser(elbar.company.learn_center_rest.entity.auth.user.AuthUser) IOException(java.io.IOException) AuthTokenCreateDTO(elbar.company.learn_center_rest.dto.auth.token.AuthTokenCreateDTO) AuthTokenGetDTO(elbar.company.learn_center_rest.dto.auth.token.AuthTokenGetDTO) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AuthUser (elbar.company.learn_center_rest.entity.auth.user.AuthUser)2 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 PasswordEncoderConfigurer (elbar.company.learn_center_rest.configs.encryption.PasswordEncoderConfigurer)1 AuthTokenCreateDTO (elbar.company.learn_center_rest.dto.auth.token.AuthTokenCreateDTO)1 AuthTokenGetDTO (elbar.company.learn_center_rest.dto.auth.token.AuthTokenGetDTO)1 IOException (java.io.IOException)1 ResponseEntity (org.springframework.http.ResponseEntity)1