use of org.maxkey.authz.cas.endpoint.ticket.TicketGrantingTicketImpl in project MaxKey by dromara.
the class CasRestV1Endpoint method casLoginRestUsers.
@Operation(summary = "CAS REST认证接口", description = "用户名密码登录接口", method = "POST")
@RequestMapping(value = CasConstants.ENDPOINT.ENDPOINT_REST_USERS_V1, method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> casLoginRestUsers(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = CasConstants.PARAMETER.SERVICE, required = false) String casService, @RequestParam(value = CasConstants.PARAMETER.REST_USERNAME, required = true) String username, @RequestParam(value = CasConstants.PARAMETER.REST_PASSWORD, required = true) String password) {
try {
if (password == null || password.isEmpty()) {
throw new BadCredentialsException("No credentials are provided or extracted to authenticate the REST request");
}
LoginCredential loginCredential = new LoginCredential(username, password, "CASREST");
authenticationProvider.authentication(loginCredential, false);
UserInfo userInfo = WebContext.getUserInfo();
TicketGrantingTicketImpl ticketGrantingTicket = new TicketGrantingTicketImpl("Random", WebContext.getAuthentication(), null);
String ticket = casTicketGrantingTicketServices.createTicket(ticketGrantingTicket);
String location = applicationConfig.getServerPrefix() + CasConstants.ENDPOINT.ENDPOINT_REST_TICKET_V1 + ticket;
HttpHeaders headers = new HttpHeaders();
headers.add("location", location);
ServiceResponseBuilder serviceResponseBuilder = new ServiceResponseBuilder();
serviceResponseBuilder.setFormat(HttpResponseConstants.FORMAT_TYPE.JSON);
// for user
serviceResponseBuilder.setAttribute("userId", userInfo.getId());
serviceResponseBuilder.setAttribute("displayName", userInfo.getDisplayName());
serviceResponseBuilder.setAttribute("firstName", userInfo.getGivenName());
serviceResponseBuilder.setAttribute("lastname", userInfo.getFamilyName());
serviceResponseBuilder.setAttribute("mobile", userInfo.getMobile());
serviceResponseBuilder.setAttribute("birthday", userInfo.getBirthDate());
serviceResponseBuilder.setAttribute("gender", userInfo.getGender() + "");
// for work
serviceResponseBuilder.setAttribute("employeeNumber", userInfo.getEmployeeNumber());
serviceResponseBuilder.setAttribute("title", userInfo.getJobTitle());
serviceResponseBuilder.setAttribute("email", userInfo.getWorkEmail());
serviceResponseBuilder.setAttribute("department", userInfo.getDepartment());
serviceResponseBuilder.setAttribute("departmentId", userInfo.getDepartmentId());
serviceResponseBuilder.setAttribute("workRegion", userInfo.getWorkRegion());
serviceResponseBuilder.success().setUser(userInfo.getUsername());
return new ResponseEntity<>(serviceResponseBuilder.serviceResponseBuilder(), headers, HttpStatus.OK);
} catch (final AuthenticationException e) {
_logger.error("BadCredentialsException ", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
} catch (final Exception e) {
_logger.error("Exception ", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of org.maxkey.authz.cas.endpoint.ticket.TicketGrantingTicketImpl in project MaxKey by dromara.
the class CasRestV1Endpoint method requestServiceTicket.
@Operation(summary = "CAS REST认证接口", description = "通过TGT获取ST", method = "POST")
@RequestMapping(value = CasConstants.ENDPOINT.ENDPOINT_REST_TICKET_V1 + "/{ticketGrantingTicket}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> requestServiceTicket(HttpServletRequest request, HttpServletResponse response, @PathVariable("ticketGrantingTicket") String ticketGrantingTicket, @RequestParam(value = CasConstants.PARAMETER.SERVICE, required = false) String casService, @RequestParam(value = CasConstants.PARAMETER.RENEW, required = false) String renew, @RequestParam(value = CasConstants.PARAMETER.REST_USERNAME, required = false) String username, @RequestParam(value = CasConstants.PARAMETER.REST_PASSWORD, required = false) String password) {
try {
TicketGrantingTicketImpl ticketGrantingTicketImpl = (TicketGrantingTicketImpl) casTicketGrantingTicketServices.get(ticketGrantingTicket);
AppsCasDetails casDetails = casDetailsService.getAppDetails(casService, true);
ServiceTicketImpl serviceTicket = new ServiceTicketImpl(ticketGrantingTicketImpl.getAuthentication(), casDetails);
String ticket = ticketServices.createTicket(serviceTicket);
return new ResponseEntity<>(ticket, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
}
return new ResponseEntity<>("", HttpStatus.BAD_REQUEST);
}
use of org.maxkey.authz.cas.endpoint.ticket.TicketGrantingTicketImpl in project MaxKey by dromara.
the class CasRestV1Endpoint method casLoginRestTickets.
@Operation(summary = "CAS REST认证接口", description = "通过用户名密码获取TGT", method = "POST")
@RequestMapping(value = CasConstants.ENDPOINT.ENDPOINT_REST_TICKET_V1, method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<String> casLoginRestTickets(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = CasConstants.PARAMETER.SERVICE, required = false) String casService, @RequestParam(value = CasConstants.PARAMETER.REST_USERNAME, required = true) String username, @RequestParam(value = CasConstants.PARAMETER.REST_PASSWORD, required = true) String password) {
try {
if (StringUtils.isBlank(password)) {
throw new BadCredentialsException("No credentials are provided or extracted to authenticate the REST request");
}
LoginCredential loginCredential = new LoginCredential(username, password, "CASREST");
authenticationProvider.authentication(loginCredential, false);
TicketGrantingTicketImpl ticketGrantingTicket = new TicketGrantingTicketImpl("Random", WebContext.getAuthentication(), null);
String ticket = casTicketGrantingTicketServices.createTicket(ticketGrantingTicket);
String location = applicationConfig.getServerPrefix() + CasConstants.ENDPOINT.ENDPOINT_REST_TICKET_V1 + "/" + ticket;
HttpHeaders headers = new HttpHeaders();
headers.add("location", location);
_logger.trace("ticket {}", ticket);
_logger.trace("location {}", location);
return new ResponseEntity<>("Location: " + location, headers, HttpStatus.CREATED);
} catch (final AuthenticationException e) {
_logger.error("BadCredentialsException ", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
} catch (final Exception e) {
_logger.error("Exception ", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Aggregations