Search in sources :

Example 1 with TicketGrantingTicketImpl

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);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) AuthenticationException(org.springframework.security.core.AuthenticationException) UserInfo(org.maxkey.entity.UserInfo) TicketGrantingTicketImpl(org.maxkey.authz.cas.endpoint.ticket.TicketGrantingTicketImpl) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) LoginCredential(org.maxkey.authn.LoginCredential) ServiceResponseBuilder(org.maxkey.authz.cas.endpoint.response.ServiceResponseBuilder) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationException(org.springframework.security.core.AuthenticationException) Operation(io.swagger.v3.oas.annotations.Operation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with TicketGrantingTicketImpl

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);
}
Also used : ServiceTicketImpl(org.maxkey.authz.cas.endpoint.ticket.ServiceTicketImpl) ResponseEntity(org.springframework.http.ResponseEntity) AppsCasDetails(org.maxkey.entity.apps.AppsCasDetails) TicketGrantingTicketImpl(org.maxkey.authz.cas.endpoint.ticket.TicketGrantingTicketImpl) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationException(org.springframework.security.core.AuthenticationException) Operation(io.swagger.v3.oas.annotations.Operation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with TicketGrantingTicketImpl

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);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) AuthenticationException(org.springframework.security.core.AuthenticationException) TicketGrantingTicketImpl(org.maxkey.authz.cas.endpoint.ticket.TicketGrantingTicketImpl) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) LoginCredential(org.maxkey.authn.LoginCredential) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) AuthenticationException(org.springframework.security.core.AuthenticationException) Operation(io.swagger.v3.oas.annotations.Operation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)3 TicketGrantingTicketImpl (org.maxkey.authz.cas.endpoint.ticket.TicketGrantingTicketImpl)3 ResponseEntity (org.springframework.http.ResponseEntity)3 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)3 AuthenticationException (org.springframework.security.core.AuthenticationException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 LoginCredential (org.maxkey.authn.LoginCredential)2 HttpHeaders (org.springframework.http.HttpHeaders)2 ServiceResponseBuilder (org.maxkey.authz.cas.endpoint.response.ServiceResponseBuilder)1 ServiceTicketImpl (org.maxkey.authz.cas.endpoint.ticket.ServiceTicketImpl)1 UserInfo (org.maxkey.entity.UserInfo)1 AppsCasDetails (org.maxkey.entity.apps.AppsCasDetails)1