use of com.haulmont.cuba.restapi.RestUserSessionInfo in project cuba by cuba-platform.
the class ClientProxyTokenStore method processSession.
/**
* Tries to find the session associated with the given {@code authentication}. If the session id is in the store and
* exists then it is set to the {@link SecurityContext}. If the session id is not in the store or the session with
* the id doesn't exist in the middleware, then the trusted login attempt is performed.
*/
protected void processSession(OAuth2Authentication authentication, String tokenValue) {
RestUserSessionInfo sessionInfo = serverTokenStore.getSessionInfoByTokenValue(tokenValue);
UUID sessionId = sessionInfo != null ? sessionInfo.getId() : null;
if (sessionId == null) {
@SuppressWarnings("unchecked") Map<String, String> userAuthenticationDetails = (Map<String, String>) authentication.getUserAuthentication().getDetails();
// sessionId parameter was put in the CubaUserAuthenticationProvider
String sessionIdStr = userAuthenticationDetails.get("sessionId");
if (!Strings.isNullOrEmpty(sessionIdStr)) {
sessionId = UUID.fromString(sessionIdStr);
}
}
UserSession session = null;
if (sessionId != null) {
try {
session = trustedClientService.findSession(restApiConfig.getTrustedClientPassword(), sessionId);
} catch (LoginException e) {
throw new RuntimeException("Unable to login with trusted client password");
}
}
if (session == null) {
@SuppressWarnings("unchecked") Map<String, String> userAuthenticationDetails = (Map<String, String>) authentication.getUserAuthentication().getDetails();
String username = userAuthenticationDetails.get("username");
if (Strings.isNullOrEmpty(username)) {
throw new IllegalStateException("Empty username extracted from user authentication details");
}
Locale locale = sessionInfo != null ? sessionInfo.getLocale() : null;
TrustedClientCredentials credentials = new TrustedClientCredentials(username, restApiConfig.getTrustedClientPassword(), locale);
credentials.setClientType(ClientType.REST_API);
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
credentials.setIpAddress(request.getRemoteAddr());
credentials.setClientInfo(makeClientInfo(request.getHeader(HttpHeaders.USER_AGENT)));
} else {
credentials.setClientInfo(makeClientInfo(""));
}
// if locale was not determined then use the user locale
if (locale == null) {
credentials.setOverrideLocale(false);
}
try {
session = authenticationService.login(credentials).getSession();
} catch (LoginException e) {
throw new OAuth2Exception("Cannot login to the middleware", e);
}
log.debug("New session created for token '{}' since the original session has been expired", tokenValue);
}
if (session != null) {
serverTokenStore.putSessionInfo(tokenValue, new RestUserSessionInfo(session));
AppContext.setSecurityContext(new SecurityContext(session));
}
}
Aggregations