use of org.maxkey.authn.online.OnlineTicket in project MaxKey by dromara.
the class OAuth2UserDetailsService method loadUserByUsername.
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
UserInfo userInfo;
try {
userInfo = loginRepository.find(username, "");
} catch (NoSuchClientException e) {
throw new UsernameNotFoundException(e.getMessage(), e);
}
String onlineTickitId = WebConstants.ONLINE_TICKET_PREFIX + "-" + java.util.UUID.randomUUID().toString().toLowerCase();
SigninPrincipal signinPrincipal = new SigninPrincipal(userInfo);
OnlineTicket onlineTicket = new OnlineTicket(onlineTickitId);
// set OnlineTicket
signinPrincipal.setOnlineTicket(onlineTicket);
ArrayList<GrantedAuthority> grantedAuthoritys = loginRepository.grantAuthority(userInfo);
signinPrincipal.setAuthenticated(true);
for (GrantedAuthority administratorsAuthority : AbstractAuthenticationProvider.grantedAdministratorsAuthoritys) {
if (grantedAuthoritys.contains(administratorsAuthority)) {
signinPrincipal.setRoleAdministrators(true);
_logger.trace("ROLE ADMINISTRATORS Authentication .");
}
}
_logger.debug("Granted Authority " + grantedAuthoritys);
signinPrincipal.setGrantedAuthorityApps(grantedAuthoritys);
return signinPrincipal;
}
use of org.maxkey.authn.online.OnlineTicket in project MaxKey by dromara.
the class CasAuthorizeEndpoint method grantingTicket.
@RequestMapping(CasConstants.ENDPOINT.ENDPOINT_SERVICE_TICKET_GRANTING)
public ModelAndView grantingTicket(Principal principal, @AuthenticationPrincipal Object user, HttpServletRequest request, HttpServletResponse response) {
AppsCasDetails casDetails = (AppsCasDetails) WebContext.getAttribute(CasConstants.PARAMETER.ENDPOINT_CAS_DETAILS);
ServiceTicketImpl serviceTicket = new ServiceTicketImpl(WebContext.getAuthentication(), casDetails);
String ticket = ticketServices.createTicket(serviceTicket, casDetails.getExpires());
StringBuffer callbackUrl = new StringBuffer(casDetails.getCallbackUrl());
if (casDetails.getCallbackUrl().indexOf("?") == -1) {
callbackUrl.append("?");
}
if (callbackUrl.indexOf("&") != -1 || callbackUrl.indexOf("=") != -1) {
callbackUrl.append("&");
}
// append ticket
callbackUrl.append(CasConstants.PARAMETER.TICKET).append("=").append(ticket);
callbackUrl.append("&");
// append service
callbackUrl.append(CasConstants.PARAMETER.SERVICE).append("=").append(casDetails.getService());
// 增加可自定义的参数
if (WebContext.getAttribute(CasConstants.PARAMETER.PARAMETER_MAP) != null) {
@SuppressWarnings("unchecked") Map<String, String> parameterMap = (Map<String, String>) WebContext.getAttribute(CasConstants.PARAMETER.PARAMETER_MAP);
parameterMap.remove(CasConstants.PARAMETER.TICKET);
parameterMap.remove(CasConstants.PARAMETER.SERVICE);
for (String key : parameterMap.keySet()) {
callbackUrl.append("&").append(key).append("=").append(parameterMap.get(key));
}
}
if (casDetails.getLogoutType() == LogoutType.BACK_CHANNEL) {
String onlineTicketId = ((SigninPrincipal) WebContext.getAuthentication().getPrincipal()).getOnlineTicket().getTicketId();
OnlineTicket onlineTicket = onlineTicketServices.get(onlineTicketId);
// set cas ticket as OnlineTicketId
casDetails.setOnlineTicket(ticket);
onlineTicket.setAuthorizedApp(casDetails);
onlineTicketServices.store(onlineTicketId, onlineTicket);
}
_logger.debug("redirect to CAS Client URL {}", callbackUrl);
ModelAndView modelAndView = new ModelAndView("authorize/cas_sso_submint");
modelAndView.addObject("callbackUrl", callbackUrl.toString());
return modelAndView;
}
use of org.maxkey.authn.online.OnlineTicket in project MaxKey by dromara.
the class RealmAuthenticationProvider method createOnlineSession.
public UsernamePasswordAuthenticationToken createOnlineSession(LoginCredential credential, UserInfo userInfo) {
String currentUserSessionId = WebContext.genId();
// Online Tickit Id
String onlineTickitId = WebConstants.ONLINE_TICKET_PREFIX + "-" + currentUserSessionId;
_logger.debug("set online Tickit Cookie {} on domain {}", onlineTickitId, this.applicationConfig.getBaseDomainName());
OnlineTicket onlineTicket = new OnlineTicket(onlineTickitId);
// set ONLINE_TICKET cookie
WebContext.setCookie(WebContext.getResponse(), this.applicationConfig.getBaseDomainName(), WebConstants.ONLINE_TICKET_NAME, onlineTickitId);
SigninPrincipal signinPrincipal = new SigninPrincipal(userInfo);
// set OnlineTicket
signinPrincipal.setOnlineTicket(onlineTicket);
ArrayList<GrantedAuthority> grantedAuthoritys = authenticationRealm.grantAuthority(userInfo);
signinPrincipal.setAuthenticated(true);
for (GrantedAuthority administratorsAuthority : grantedAdministratorsAuthoritys) {
if (grantedAuthoritys.contains(administratorsAuthority)) {
signinPrincipal.setRoleAdministrators(true);
_logger.trace("ROLE ADMINISTRATORS Authentication .");
}
}
_logger.debug("Granted Authority {}", grantedAuthoritys);
signinPrincipal.setGrantedAuthorityApps(authenticationRealm.queryAuthorizedApps(grantedAuthoritys));
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(signinPrincipal, "PASSWORD", grantedAuthoritys);
authenticationToken.setDetails(new WebAuthenticationDetails(WebContext.getRequest()));
onlineTicket.setAuthentication(authenticationToken);
this.onlineTicketServices.store(onlineTickitId, onlineTicket);
/*
* put userInfo to current session context
*/
WebContext.setUserInfo(userInfo);
WebContext.setAuthentication(authenticationToken);
WebContext.setAttribute(WebConstants.CURRENT_USER_SESSION_ID, currentUserSessionId);
if (!WebContext.getInst(WebContext.getRequest()).equalsIgnoreCase(userInfo.getInstId())) {
// TODO :
}
return authenticationToken;
}
use of org.maxkey.authn.online.OnlineTicket in project MaxKey by dromara.
the class LogoutEndpoint method logoutModelAndView.
private ModelAndView logoutModelAndView(HttpServletRequest request, HttpServletResponse response, String viewName, String reLoginUrl) {
ModelAndView modelAndView = new ModelAndView();
authenticationRealm.logout(response);
if (reLoginUrl == null || reLoginUrl.equals("")) {
SavedRequest firstSavedRequest = (SavedRequest) WebContext.getAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
reLoginUrl = "/login";
if (firstSavedRequest != null) {
reLoginUrl = firstSavedRequest.getRedirectUrl();
WebContext.removeAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
}
}
// not start with http or https
if (reLoginUrl != null && !reLoginUrl.toLowerCase().startsWith("http")) {
if (reLoginUrl.startsWith("/")) {
reLoginUrl = request.getContextPath() + reLoginUrl;
} else {
reLoginUrl = request.getContextPath() + "/" + reLoginUrl;
}
}
_logger.debug("re Login URL : " + reLoginUrl);
modelAndView.addObject("reloginUrl", reLoginUrl);
// if logined in have onlineTicket ,need remove or logout back
if (WebContext.getAuthentication() != null) {
String onlineTicketId = ((SigninPrincipal) WebContext.getAuthentication().getPrincipal()).getOnlineTicket().getTicketId();
OnlineTicket onlineTicket = onlineTicketServices.get(onlineTicketId);
if (onlineTicket != null) {
Set<Entry<String, Apps>> entrySet = onlineTicket.getAuthorizedApps().entrySet();
Iterator<Entry<String, Apps>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<String, Apps> mapEntry = iterator.next();
_logger.debug("App Id : " + mapEntry.getKey() + " , " + mapEntry.getValue());
if (mapEntry.getValue().getLogoutType() == LogoutType.BACK_CHANNEL) {
SingleLogout singleLogout;
if (mapEntry.getValue().getProtocol().equalsIgnoreCase(ConstsProtocols.CAS)) {
singleLogout = new SamlSingleLogout();
} else {
singleLogout = new DefaultSingleLogout();
}
singleLogout.sendRequest(onlineTicket.getAuthentication(), mapEntry.getValue());
}
}
onlineTicketServices.remove(onlineTicketId);
}
}
// remove ONLINE_TICKET cookie
WebContext.expiryCookie(WebContext.getResponse(), this.applicationConfig.getBaseDomainName(), WebConstants.ONLINE_TICKET_NAME, UUID.randomUUID().toString());
request.getSession().invalidate();
// for(String removeAttribute : WebContext.logoutAttributeNameList) {
// request.getSession().removeAttribute(removeAttribute);
// }
SecurityContextHolder.clearContext();
modelAndView.setViewName(viewName);
return modelAndView;
}
Aggregations