/** * Handles the creation of the final <tt>Authentication</tt> object which will be * returned by the provider. * <p> * The default implementation just creates a new OpenIDAuthenticationToken from the * original, but with the UserDetails as the principal and including the authorities * loaded by the UserDetailsService. * * @param userDetails the loaded UserDetails object * @param auth the token passed to the authenticate method, containing * @return the token which will represent the authenticated user. */ protected Authentication createSuccessfulAuthentication(UserDetails userDetails, OpenIDAuthenticationToken auth) { return new OpenIDAuthenticationToken(userDetails, this.authoritiesMapper.mapAuthorities(userDetails.getAuthorities()), auth.getIdentityUrl(), auth.getAttributes()); }
@Override public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException { final String openId = token.getIdentityUrl(); User user = this.getUserByOpenId(openId); if (user == null) { log.info("Open ID User with URL "+openId+" was not found!"); throw new UsernameNotFoundException("Open ID User with URL "+openId+" was not found!"); } return user; } }
public Builder withAuthenticationToken(OpenIDAuthenticationToken token) { this.username = token.getIdentityUrl(); for (OpenIDAttribute attribute : token.getAttributes()) { if (attribute.getName().equals("email")) { this.email = attribute.getValues().get(0); } if (attribute.getName().equals("firstname")) { this.firstname = attribute.getValues().get(0); } if (attribute.getName().equals("lastname")) { this.lastname = attribute.getValues().get(0); } if (attribute.getName().equals("fullname")) { this.fullname = attribute.getValues().get(0); } } return this; }
OpenIDAuthenticationToken newAuthentication = new OpenIDAuthenticationToken(userDetails, userDetails.getAuthorities(), openidAuthenticationToken.getIdentityUrl(), openidAuthenticationToken.getAttributes());
/** * Handles the creation of the final <tt>Authentication</tt> object which will be * returned by the provider. * <p> * The default implementation just creates a new OpenIDAuthenticationToken from the * original, but with the UserDetails as the principal and including the authorities * loaded by the UserDetailsService. * * @param userDetails the loaded UserDetails object * @param auth the token passed to the authenticate method, containing * @return the token which will represent the authenticated user. */ protected Authentication createSuccessfulAuthentication(UserDetails userDetails, OpenIDAuthenticationToken auth) { return new OpenIDAuthenticationToken(userDetails, this.authoritiesMapper.mapAuthorities(userDetails.getAuthorities()), auth.getIdentityUrl(), auth.getAttributes()); }
@Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { if(exception instanceof UsernameNotFoundException && exception.getAuthentication() instanceof OpenIDAuthenticationToken && ((OpenIDAuthenticationToken)exception.getAuthentication()).getStatus().equals(OpenIDAuthenticationStatus.SUCCESS)) { OpenIDAuthenticationToken token = (OpenIDAuthenticationToken)exception.getAuthentication(); String url = token.getIdentityUrl(); User user = createTemporaryUser(token, url); request.getSession(true).setAttribute(ModelKeys.NEW_USER, user); DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); log.info("Redirecting to new user account creation page"); super.setRedirectStrategy(redirectStrategy); redirectStrategy.sendRedirect(request, response, "/"+ViewNames.CREATE_ACCOUNT_PAGE); return; } else { super.onAuthenticationFailure(request, response, exception); } }
@Override public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException { String identity = token.getIdentityUrl(); UserIdentifier identifier = new UserIdentifier(identity); User user = userRepository.findByIdentifier(identifier); if (user != null) { // TODO: update account } else { // TODO: add user } Builder builder = new Builder().withAuthenticationToken(token); if (user != null) { for (RoleIdentifier role : user.getRoles()) { builder.addGrantedAuthority(new SimpleGrantedAuthority(role.getIdentifier())); } } return builder.build(); }
user.setIdentifier(token.getIdentityUrl()); user = loadUser(user); user.setName(fullName); user.setPassword(Utils.generateSecurityToken()); user.setIdentifier(token.getIdentityUrl()); String id = user.create(); if (id == null) {
user.setIdentifier(token.getIdentityUrl()); user = loadUser(user); user.setName(fullName); user.setPassword(Utils.generateSecurityToken()); user.setIdentifier(token.getIdentityUrl()); String id = user.create(); if (id == null) {
user.setIdentifier(token.getIdentityUrl()); user = loadUser(user); user.setName(fullName); user.setPassword(new UUID().toString()); user.setIdentifier(token.getIdentityUrl()); String id = user.create(); if (id == null) {
if (token.getStatus() == OpenIDAuthenticationStatus.SUCCESS) { String openId = token.getIdentityUrl(); if (securityRealm.getUserForOpenId(openId) != null) { validationContext.addError("The OpenID supplied is already mapped to a user.");