use of org.craftercms.profile.api.exceptions.ProfileException in project profile by craftercms.
the class AuthenticationManagerImpl method authenticateUser.
@Override
public Authentication authenticateUser(String tenant, String username, String password) {
try {
Ticket ticket = authenticationService.authenticate(tenant, username, password);
Profile profile = profileService.getProfile(ticket.getProfileId());
if (profile == null) {
throw new AuthenticationSystemException("No profile found for ID '" + ticket.getProfileId() + "'");
}
String ticketId = ticket.getId();
DefaultAuthentication auth = new DefaultAuthentication(ticketId, profile);
authenticationCache.putAuthentication(auth);
logger.debug("Authentication successful for user '{}' (ticket ID = '{}')", ticket.getProfileId(), ticketId);
return auth;
} catch (ProfileRestServiceException e) {
switch(e.getErrorCode()) {
case DISABLED_PROFILE:
throw new DisabledUserException("User is disabled", e);
case BAD_CREDENTIALS:
throw new BadCredentialsException("Invalid username and/or password", e);
default:
throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e);
}
} catch (ProfileException e) {
throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e);
}
}
use of org.craftercms.profile.api.exceptions.ProfileException in project profile by craftercms.
the class RememberMeManagerImpl method autoLogin.
@Override
public Authentication autoLogin(RequestContext context) throws RememberMeException {
PersistentLogin login = getPersistentLoginFromCookie(context.getRequest());
if (login != null) {
PersistentLogin actualLogin;
try {
actualLogin = authenticationService.getPersistentLogin(login.getId());
} catch (ProfileException e) {
throw new RememberMeException("Error retrieving persistent login '" + login.getProfileId() + "'");
}
if (actualLogin != null) {
if (!login.getProfileId().equals(actualLogin.getProfileId())) {
throw new InvalidCookieException("Profile ID mismatch");
} else if (!login.getToken().equals(actualLogin.getToken())) {
throw new CookieTheftException("Token mismatch. Implies a cookie theft");
} else {
String loginId = actualLogin.getId();
String profileId = actualLogin.getProfileId();
logger.debug("Remember me cookie match for {}. Starting auto-login", actualLogin);
Authentication auth;
try {
auth = authenticate(profileId);
} catch (AuthenticationException e) {
// Delete remember me cookie so that we don't retry auto login in next request
disableRememberMe(loginId, context);
throw new RememberMeException("Unable to auto-login user '" + profileId + "'", e);
}
updateRememberMe(loginId, context);
return auth;
}
} else {
logger.debug("No persistent login found for ID '{}' (has possibly expired)", login.getId());
deleteRememberMeCookie(context.getResponse());
return null;
}
} else {
return null;
}
}
use of org.craftercms.profile.api.exceptions.ProfileException in project profile by craftercms.
the class RememberMeManagerImpl method enableRememberMe.
@Override
public void enableRememberMe(Authentication authentication, RequestContext context) throws RememberMeException {
String profileId = authentication.getProfile().getId().toString();
PersistentLogin login;
try {
login = authenticationService.createPersistentLogin(profileId);
} catch (ProfileException e) {
throw new RememberMeException("Error creating persistent login for profile '" + profileId + "'", e);
}
logger.debug("Persistent login created: {}", login);
addRememberMeCookie(serializeLogin(login), context.getResponse());
}
use of org.craftercms.profile.api.exceptions.ProfileException in project engine by craftercms.
the class ProfileHeadersAuthenticationFilter method doGetPreAuthenticatedPrincipal.
@Override
protected Object doGetPreAuthenticatedPrincipal(final HttpServletRequest request) {
String username = request.getHeader(getUsernameHeaderName());
String email = request.getHeader(getEmailHeaderName());
if (isNoneEmpty(username, email)) {
try {
String[] tenantNames = tenantsResolver.getTenants();
Tenant tenant = getSsoEnabledTenant(tenantNames);
if (tenant != null) {
Profile profile = profileService.getProfileByUsername(tenant.getName(), username);
if (profile == null) {
profile = createProfileWithSsoInfo(username, tenant, request);
}
return new ProfileUser(profile);
} else {
logger.warn("A SSO login was attempted, but none of the tenants [{}] is enabled for SSO", (Object) tenantNames);
}
} catch (ProfileException e) {
logger.error("Error processing headers authentication for '{}'", username, e);
}
}
return null;
}
use of org.craftercms.profile.api.exceptions.ProfileException in project engine by craftercms.
the class ProfileRememberMeServices method onLoginSuccess.
@Override
protected void onLoginSuccess(final HttpServletRequest request, final HttpServletResponse response, final Authentication successfulAuthentication) {
ProfileUser profileUser = (ProfileUser) successfulAuthentication.getPrincipal();
try {
PersistentLogin persistentLogin = authenticationService.createPersistentLogin(profileUser.getProfile().getId().toHexString());
setCookie(new String[] { persistentLogin.getId(), persistentLogin.getToken() }, getTokenValiditySeconds(), request, response);
} catch (ProfileException e) {
throw new RememberMeAuthenticationException("Error creating persistent login for " + profileUser.getUsername(), e);
}
}
Aggregations