protected Profile findProfile(String profileId) { try { return profileService.getProfile(profileId); } catch (ProfileException e) { throw new SocialMediaIntegrationException("Unable to find profile '" + profileId + "'", e); } }
protected List<Profile> findProfilesByQuery(String tenant, String query) { try { return profileService.getProfilesByQuery(tenant, query, null, null, null, null, ProfileConstants.NO_ATTRIBUTE); } catch (ProfileException e) { throw new SocialMediaIntegrationException("Unable to find profiles of tenant '" + tenant + "' by " + "query " + query, e); } }
protected Tenant getSsoEnabledTenant(String[] tenantNames) throws ProfileException { for (String tenantName : tenantNames) { Tenant tenant = tenantService.getTenant(tenantName); if (tenant != null && tenant.isSsoEnabled()) { return tenant; } } return null; }
protected void updateProfile() { try { profile = profileService.updateAttributes(profile.getId().toString(), profile.getAttributes()); } catch (ProfileException e) { throw new SocialMediaIntegrationException("Unable to update profile of user '" + profile.getId() + "'", e); } }
@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); } }
protected Profile createProfileWithSsoInfo(String username, Tenant tenant, HttpServletRequest request) throws ProfileException { Map<String, Object> attributes = null; List<AttributeDefinition> attributeDefinitions = tenant.getAttributeDefinitions(); String email = request.getHeader(emailHeaderName); for (AttributeDefinition attributeDefinition : attributeDefinitions) { String attributeName = attributeDefinition.getName(); String attributeValue = request.getHeader(mellonHeaderPrefix + attributeName); if (StringUtils.isNotEmpty(attributeValue)) { if (attributes == null) { attributes = new HashMap<>(); } attributes.put(attributeName, attributeValue); } } logger.info("Creating new profile with SSO info: username={}, email={}, tenant={}, attributes={}", username, email, tenant.getName(), attributes); return profileService.createProfile(tenant.getName(), username, null, email, true, null, attributes, null); }
/** * Returns a list with the names of all tenants. * * @param tenantService the service that retrieves the {@link org.craftercms.profile.api.Tenant}s. * * @return the list of tenant names */ public static List<String> getTenantNames(TenantService tenantService) throws ProfileException { List<Tenant> tenants = tenantService.getAllTenants(); List<String> tenantNames = new ArrayList<>(tenants.size()); if (CollectionUtils.isNotEmpty(tenants)) { for (Tenant tenant : tenants) { tenantNames.add(tenant.getName()); } } return tenantNames; }
protected Profile getProfile(String tenant, Profile userData) { try { return profileService.getProfileByUsername(tenant, userData.getUsername()); } catch (ProfileException e) { throw new AuthenticationException("Unable to retrieve current profile for user '" + userData.getUsername() + "' of tenant '" + tenant + "'", e); } }
protected void disableRememberMe(String loginId, RequestContext context) throws RememberMeException { deleteRememberMeCookie(context.getResponse()); try { authenticationService.deletePersistentLogin(loginId); } catch (ProfileException e) { throw new RememberMeException("Error invalidating persistent login '" + loginId + "'"); } logger.debug("Persistent login '{}' invalidated", loginId); }
protected void updateRememberMe(String loginId, RequestContext context) throws RememberMeException { PersistentLogin login; try { login = authenticationService.refreshPersistentLoginToken(loginId); } catch (ProfileException e) { throw new RememberMeException("Unable to update persistent login '" + loginId + "'", e); } logger.debug("Persistent login updated: {}", login); addRememberMeCookie(serializeLogin(login), context.getResponse()); }
@Override public void invalidateAuthentication(Authentication authentication) { try { authenticationCache.removeAuthentication(authentication.getTicket()); authenticationService.invalidateTicket(authentication.getTicket()); logger.debug("Ticket '{}' successfully invalidated"); } catch (ProfileException e) { throw new AuthenticationSystemException("An unexpected error occurred while attempting to invalidate " + "ticket '" + authentication.getTicket() + "'", e); } }
protected Profile loadProfile(String ticketId) throws AuthenticationException { try { Profile profile = profileService.getProfileByTicket(ticketId); if (profile != null) { logger.debug("Profile '{}' retrieved for ticket '{}'", profile.getId(), ticketId); return profile; } else { throw new AuthenticationSystemException("No profile found for ticket '" + ticketId + "'"); } } catch (ProfileRestServiceException e) { if (e.getErrorCode() == ErrorCode.NO_SUCH_TICKET) { logger.debug("Ticket '{}' is invalid", ticketId); return null; } else { throw new AuthenticationSystemException("An unexpected error occurred while attempting to retrieve " + "profile for ticket '" + ticketId + "'", e); } } catch (ProfileException e) { throw new AuthenticationSystemException("An unexpected error occurred while attempting to retrieve " + "profile for ticket '" + ticketId + "'", e); } }
@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()); }
@Override public Authentication authenticateUser(Profile profile, boolean remembered) throws AuthenticationException { try { Ticket ticket = authenticationService.createTicket(profile.getId().toString()); String ticketId = ticket.getId(); DefaultAuthentication auth = new DefaultAuthentication(ticketId, profile, remembered); authenticationCache.putAuthentication(auth); logger.debug("Authentication successful for user '{}' (ticket ID = '{}')", ticket.getProfileId(), ticketId); return auth; } catch (ProfileRestServiceException e) { if (e.getErrorCode() == ErrorCode.DISABLED_PROFILE) { throw new DisabledUserException("User is disabled", e); } else { throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e); } } catch (ProfileException e) { throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e); } }
protected Profile findProfile(String profileId) { try { return profileService.getProfile(profileId); } catch (ProfileException e) { throw new SocialMediaIntegrationException("Unable to find profile '" + profileId + "'", e); } }
protected void updateProfile() { try { profile = profileService.updateAttributes(profile.getId().toString(), profile.getAttributes()); } catch (ProfileException e) { throw new SocialMediaIntegrationException("Unable to update profile of user '" + profile.getId() + "'", e); } }
protected List<Profile> findProfilesByQuery(String tenant, String query) { try { return profileService.getProfilesByQuery(tenant, query, null, null, null, null, ProfileConstants.NO_ATTRIBUTE); } catch (ProfileException e) { throw new SocialMediaIntegrationException("Unable to find profiles of tenant '" + tenant + "' by " + "query " + query, e); } }
protected Profile createProfile(String tenant, Connection<?> connection, Profile userData) { try { ConnectionUtils.addConnectionData(userData, connection.createData(), textEncryptor); return profileService.createProfile(tenant, userData.getUsername(), null, userData.getEmail(), true, userData.getRoles(), userData.getAttributes(), null); } catch (CryptoException | ProfileException e) { throw new AuthenticationException("Unable to create profile of user '" + userData.getUsername() + "' in tenant '" + tenant + "'", e); } }
protected Authentication authenticate(String profileId) throws AuthenticationException { Profile profile; try { profile = profileService.getProfile(profileId); } catch (ProfileException e) { throw new AuthenticationSystemException("Error retrieving profile '" + profileId + "'", e); } if (profile != null) { return authenticationManager.authenticateUser(profile, true); } else { throw new AuthenticationSystemException("No profile found for ID '" + profileId + "'"); } }
protected Profile updateProfileConnectionData(String tenant, Connection<?> connection, Profile profile) { try { ConnectionUtils.addConnectionData(profile, connection.createData(), textEncryptor); return profileService.updateAttributes(profile.getId().toString(), profile.getAttributes()); } catch (CryptoException | ProfileException e) { throw new AuthenticationException("Unable to update connection data of user '" + profile.getUsername() + "' of tenant '" + tenant + "'", e); } }