protected Map<String, List<Map<String, Object>>> getConnectionsAttribute(Profile profile) { return profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); }
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 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 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); } }
/** * Adds the info from the provider profile to the specified profile. * * @param profile the target profile * @param providerProfile the provider profile where to get the info */ public static void addProviderProfileInfo(Profile profile, UserProfile providerProfile) { String email = providerProfile.getEmail(); if (StringUtils.isEmpty(email)) { throw new IllegalStateException("No email included in provider profile"); } String username = providerProfile.getUsername(); if (StringUtils.isEmpty(username)) { username = email; } String firstName = providerProfile.getFirstName(); String lastName = providerProfile.getLastName(); profile.setUsername(username); profile.setEmail(email); profile.setAttribute(FIRST_NAME_ATTRIBUTE_NAME, firstName); profile.setAttribute(LAST_NAME_ATTRIBUTE_NAME, lastName); }
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 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); }
/** * Creates a profile from the specified connection. * * @param connection the connection where to retrieve the profile info from * * @return */ public static Profile createProfileFromConnection(Connection<?> connection) { Profile profile = new Profile(); addProviderProfileInfo(profile, connection.fetchUserProfile()); return profile; }
protected String serializeLogin(PersistentLogin login) throws RememberMeException { StringBuilder serializedLogin = new StringBuilder(); serializedLogin.append(login.getId()).append(SERIALIZED_LOGIN_SEPARATOR); serializedLogin.append(login.getProfileId()).append(SERIALIZED_LOGIN_SEPARATOR); serializedLogin.append(login.getToken()); try { return encryptor.encrypt(serializedLogin.toString()); } catch (CryptoException e) { throw new RememberMeException("Unable to encrypt remember me cookie", e); } }
/** * Returns trues if the profile has any of the specified roles. */ public boolean hasAnyRole(Collection<String> roles) { return isAuthenticated() && profile.hasAnyRole(roles); }
/** * Returns trues if the profile has the specified role. */ public boolean hasRole(String role) { return isAuthenticated() && profile.hasRole(role); }
/** * Returns the current tenant name, which is the tenant of the currently authenticated profile. * * @return the current tenant name. */ public static String getCurrentTenantName() { Profile profile = SecurityUtils.getCurrentProfile(); if (profile != null) { return profile.getTenant(); } else { return null; } }
@Override public void disableRememberMe(RequestContext context) throws RememberMeException { PersistentLogin login = getPersistentLoginFromCookie(context.getRequest()); if (login != null) { disableRememberMe(login.getId(), context); } }
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); } }
/** * Adds the info from the provider profile to the specified profile. * * @param profile the target profile * @param providerProfile the provider profile where to get the info */ public static void addProviderProfileInfo(Profile profile, UserProfile providerProfile) { String email = providerProfile.getEmail(); if (StringUtils.isEmpty(email)) { throw new IllegalStateException("No email included in provider profile"); } String username = providerProfile.getUsername(); if (StringUtils.isEmpty(username)) { username = email; } String firstName = providerProfile.getFirstName(); String lastName = providerProfile.getLastName(); profile.setUsername(username); profile.setEmail(email); profile.setAttribute(FIRST_NAME_ATTRIBUTE_NAME, firstName); profile.setAttribute(LAST_NAME_ATTRIBUTE_NAME, lastName); }
protected Map<String, List<Map<String, Object>>> getConnectionsAttribute(Profile profile) { return profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); }
/** * Creates a profile from the specified connection. * * @param connection the connection where to retrieve the profile info from * * @return */ public static Profile createProfileFromConnection(Connection<?> connection) { Profile profile = new Profile(); addProviderProfileInfo(profile, connection.fetchUserProfile()); return profile; }
/** * Remove all {@link org.springframework.social.connect.ConnectionData} associated to the specified provider ID. * * @param profile the profile where to remove the data from * @param providerId the provider ID of the connection */ public static void removeConnectionData(Profile profile, String providerId) { Map<String, List<Map<String, Object>>> allConnections = profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); if (MapUtils.isNotEmpty(allConnections)) { allConnections.remove(providerId); } }
/** * Remove all {@link ConnectionData} associated to the specified provider ID. * * @param profile the profile where to remove the data from * @param providerId the provider ID of the connection */ public static void removeConnectionData(Profile profile, String providerId) { Map<String, List<Map<String, Object>>> allConnections = profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); if (MapUtils.isNotEmpty(allConnections)) { allConnections.remove(providerId); } }
/** * Remove all {@link org.springframework.social.connect.ConnectionData} associated to the specified provider ID. * * @param profile the profile where to remove the data from * @param providerId the provider ID of the connection */ public static void removeConnectionData(Profile profile, String providerId) { Map<String, List<Map<String, Object>>> allConnections = profile.getAttribute(CONNECTIONS_ATTRIBUTE_NAME); if (MapUtils.isNotEmpty(allConnections)) { allConnections.remove(providerId); } }