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);
}
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");
}
}
Aggregations