use of com.infiniteautomation.mango.rest.v2.model.jwt.TokenModel in project ma-modules-public by infiniteautomation.
the class AuthenticationTokenRestController method createToken.
@ApiOperation(value = "Create auth token", notes = "Creates an authentication token for the current user or for the username specified (admin only)")
@RequestMapping(path = "/create", method = RequestMethod.POST)
@PreAuthorize("isAuthenticated() and isPasswordAuthenticated()")
public ResponseEntity<TokenModel> createToken(@RequestBody CreateTokenRequest requestBody, @AuthenticationPrincipal User currentUser) {
Date expiry = requestBody.getExpiry();
String username = requestBody.getUsername();
User user = currentUser;
if (username != null && !username.equals(currentUser.getUsername())) {
if (!currentUser.isAdmin()) {
throw new AccessDeniedException(new TranslatableMessage("rest.error.onlyAdminsCanCreateTokens"));
}
user = UserDao.instance.getUser(username);
if (user == null) {
throw new BadRequestException(new TranslatableMessage("rest.error.unknownUser", username));
}
}
String token = tokenAuthService.generateToken(user, expiry);
return new ResponseEntity<>(new TokenModel(token), HttpStatus.CREATED);
}
Aggregations