Example usage for org.springframework.security.openid OpenIDAttribute getValues

List of usage examples for org.springframework.security.openid OpenIDAttribute getValues

Introduction

In this page you can find the example usage for org.springframework.security.openid OpenIDAttribute getValues.

Prototype

public List<String> getValues() 

Source Link

Document

The values obtained from an attribute exchange.

Usage

From source file:grails.plugin.springsecurity.openid.OpenIdAuthenticationFailureHandler.java

protected List<OpenIDAttribute> extractAttrsWithValues(final OpenIDAuthenticationToken authentication) {
    List<OpenIDAttribute> attributes = new ArrayList<OpenIDAttribute>();
    for (OpenIDAttribute attr : authentication.getAttributes()) {
        if (attr.getValues() == null || attr.getValues().isEmpty()) {
            continue;
        }//from  w w w .j  a va 2s  .  c o m
        if (attr.getValues().size() == 1 && attr.getValues().get(0) == null) {
            continue;
        }
        attributes.add(attr);
    }
    return attributes;
}

From source file:info.gewton.openid.servlet.OpenIDLoginServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    SecurityContext sc = (SecurityContext) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
    OpenIDAuthenticationToken auth = (OpenIDAuthenticationToken) sc.getAuthentication();
    List<OpenIDAttribute> attributes = auth.getAttributes();
    for (OpenIDAttribute a : attributes) {
        if (a.getName().equals("namePerson")) {
            request.getSession().setAttribute("namePerson", a.getValues().get(0));
            break;
        }/*w  w w  .ja  v a 2 s .com*/
    }
    response.sendRedirect(request.getContextPath() + "/index.jsp");
}

From source file:org.bisen.blog.service.OpenIDUserDetailsService.java

@Override
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {
    List<OpenIDAttribute> attributes = token.getAttributes();
    String email = null;/*w ww . j  a  va2s.  c om*/
    for (OpenIDAttribute attribute : attributes) {
        if (attribute.getName().equals("email")) {
            email = attribute.getValues().get(0);
        }
    }
    if (email == null) {
        throw new UsernameNotFoundException("User not registered");
    } else {
        BlogUser user = blogService.findUser(email);
        if (user == null) {
            throw new UsernameNotFoundException("User not registered");
        } else {
            return new BlogUserDetails(email);
        }
    }
}

From source file:de.uni_koeln.spinfo.maalr.login.PostLoginHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    String dictContext = Configuration.getInstance().getDictContext();

    // Set Session Timeout to one hour
    request.getSession().setMaxInactiveInterval(60 * 60);
    if (authentication != null && authentication instanceof JaasAuthenticationToken) {
        // TODO: Implement something similar for ldap...
        request.getSession().setAttribute("uname", authentication.getName());
    }//  www . j a  va  2s. c  om
    if (authentication != null && authentication instanceof OpenIDAuthenticationToken) {
        // TODO: Optimize this - inefficient to query for each request...
        MaalrUserInfo userInfo = backend.getByLogin(authentication.getName());
        if (userInfo == null) {
            OpenIDAuthenticationToken token = (OpenIDAuthenticationToken) SecurityContextHolder.getContext()
                    .getAuthentication();
            List<OpenIDAttribute> attributes = token.getAttributes();
            userInfo = new MaalrUserInfo(authentication.getName(), Role.OPENID_2);
            for (OpenIDAttribute openIDAttribute : attributes) {
                if (openIDAttribute.getValues() != null && openIDAttribute.getValues().size() > 0) {
                    if ("axContactEmail".equals(openIDAttribute.getName()) && userInfo.getEmail() == null) {
                        userInfo.setEmail(openIDAttribute.getValues().get(0));
                    }
                    if ("oiContactEmail".equals(openIDAttribute.getName()) && userInfo.getEmail() == null) {
                        userInfo.setEmail(openIDAttribute.getValues().get(0));
                    }
                    if ("axNamePersonFirstName".equals(openIDAttribute.getName())
                            && userInfo.getFirstname() == null) {
                        userInfo.setFirstname(openIDAttribute.getValues().get(0));
                    }
                    if ("axNamePersonLastName".equals(openIDAttribute.getName())
                            && userInfo.getLastname() == null) {
                        userInfo.setLastname(openIDAttribute.getValues().get(0));
                    }
                }
            }
            try {
                backend.insert(userInfo);
            } catch (InvalidUserException e) {
                e.printStackTrace();
            }
        }
        request.getSession().setAttribute("uname", userInfo.getFirstname());
    }
    if (authentication != null) {
        Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities());
        if (roles.contains(Constants.Roles.ADMIN_5)) {
            response.sendRedirect(dictContext + "/admin/admin.html");
            return;
        } else if (roles.contains(Constants.Roles.TRUSTED_IN_4)) {
            response.sendRedirect(dictContext + "/editor/editor.html");
            return;
        }
    }
    response.sendRedirect(dictContext + "/index.html");
}

From source file:ar.edu.fesf.security.OpenIDAttributes2UserDetailsImpl.java

/**
 * {@inheritDoc}//w ww  .jav a 2  s  .c  om
 */
@Override
public UserDetails extract(final OpenIDAuthenticationToken token) {
    String email = "";
    String firstName = "";
    String lastName = "";
    String language = "";
    List<OpenIDAttribute> attributes = token.getAttributes();
    for (OpenIDAttribute openIDAttribute : attributes) {
        if (openIDAttribute.getName().equals("firstName")) {
            firstName = StringUtils.join(openIDAttribute.getValues(), "");
        }

        if (openIDAttribute.getName().equals("email")) {
            email = StringUtils.join(openIDAttribute.getValues(), "");
        }

        if (openIDAttribute.getName().equals("lastName")) {
            lastName = StringUtils.join(openIDAttribute.getValues(), "");
        }

        if (openIDAttribute.getName().equals("language")) {
            language = StringUtils.join(openIDAttribute.getValues(), "");
        }
    }
    return new UserDetailsImpl(token.getIdentityUrl(), firstName, lastName, email, language);
}

From source file:org.cloudfoundry.identity.uaa.openid2.OpenIdUserDetailsService.java

/**
 * Implementation of {@code AuthenticationUserDetailsService} which allows full access to the submitted
 * {@code Authentication} object. Used by the OpenIDAuthenticationProvider.
 *///from  www  . j a  v a 2s  .  c o  m
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) {
    // String id = token.getIdentityUrl();

    String email = null;
    String firstName = null;
    String lastName = null;
    String fullName = null;

    List<OpenIDAttribute> attributes = token.getAttributes();

    for (OpenIDAttribute attribute : attributes) {
        if (attribute.getName().equals("email")) {
            email = attribute.getValues().get(0);
        }

        if (attribute.getName().equals("firstname")) {
            firstName = attribute.getValues().get(0);
        }

        if (attribute.getName().equals("lastname")) {
            lastName = attribute.getValues().get(0);
        }

        if (attribute.getName().equals("fullname")) {
            fullName = attribute.getValues().get(0);
        }
    }

    if (firstName == null && StringUtils.hasText(fullName)) {
        String[] names = fullName.split(" ");
        firstName = names[0];
    }

    if (lastName == null && StringUtils.hasText(fullName)) {
        String[] names = fullName.split(" ");
        lastName = names.length > 1 ? names[1] : "User";
    }

    if (firstName == null && StringUtils.hasText(email)) {
        String[] names = email.split("@");
        firstName = names[0];
    }

    if (lastName == null && StringUtils.hasText(email)) {
        String[] names = email.split("@");
        lastName = names.length > 1 ? names[1] : "User";
    }

    UaaUser user = new UaaUser(email, generator.generate(), email, firstName, lastName);
    return new UaaUserDetails(user);
}

From source file:it.av.youeat.web.security.OpenIDAttributes2UserDetailsImpl.java

/**
 * {@inheritDoc}/*from w  w w .  ja  v a2s .  co  m*/
 */
@Override
public Eater extract(OpenIDAuthenticationToken token) {
    Eater user = new Eater();
    List<OpenIDAttribute> attributes = token.getAttributes();
    for (OpenIDAttribute openIDAttribute : attributes) {
        if (openIDAttribute.getName().equals("firstName")) {
            user.setFirstname(StringUtils.join(openIDAttribute.getValues(), ""));
        }

        if (openIDAttribute.getName().equals("email")) {
            user.setEmail(StringUtils.join(openIDAttribute.getValues(), ""));
        }

        if (openIDAttribute.getName().equals("lastName")) {
            user.setLastname(StringUtils.join(openIDAttribute.getValues(), ""));
        }

        if (openIDAttribute.getName().equals("language")) {
            String langage = StringUtils.join(openIDAttribute.getValues(), "");
            user.setLanguage(languageService.getSupportedLanguage(new Locale(langage)));
        }
        if (openIDAttribute.getName().equals("country")) {
            String country = StringUtils.join(openIDAttribute.getValues(), "");
            user.setCountry(countryService.getByIso2(country));
        }
    }
    if (user.getCountry() == null) {
        user.setCountry(countryService.getByIso2(user.getLanguage().getCountry()));
        //user.setLanguage(languageService.getSupportedLanguage(new Locale(user.getLocale())));
    }
    user.setEmail(user.getEmail());
    user.setSocialType(SocialType.GOOGLE);
    user.setSocialUID(token.getIdentityUrl());
    return user;
}

From source file:mx.edu.um.mateo.general.service.UserDetailsServiceImpl.java

@Override
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {
    log.debug("loadUserDetails: {}", token);
    String username = token.getIdentityUrl();
    String email = "";
    Usuario usuario = usuarioDao.obtienePorOpenId(username);
    log.debug("Usuario encontrado : {}", usuario);
    if (usuario == null) {
        log.debug("Buscando atributo email");
        List<OpenIDAttribute> attrs = token.getAttributes();
        for (OpenIDAttribute attr : attrs) {
            log.debug("Attr: {}", attr.getName());
            if (attr.getName().equals("email")) {
                email = attr.getValues().get(0);
            }/* ww  w  .  j ava2 s  .  co  m*/
        }
        log.debug("Buscando por email {}", email);
        usuario = usuarioDao.obtienePorCorreo(email);
        if (usuario == null) {
            throw new UsernameNotFoundException("No se encontro al usuario " + username);
        }
        usuario.setOpenId(username);
        usuarioDao.actualiza(usuario);
    }
    log.debug("Regresando usuario: {}", usuario);
    return (UserDetails) usuario;
}

From source file:net.firejack.platform.web.security.spring.openid.OpenIDAuthenticationManager.java

private String findAttributeValueByName(SupportedOpenIDAttribute searchOpenIDAttribute,
        List<OpenIDAttribute> attributes) {
    String value = null;/*from  w w  w.ja v a2 s.com*/
    for (OpenIDAttribute attribute : attributes) {
        if (searchOpenIDAttribute.getAttributeName().equals(attribute.getName())) {
            if (attribute.getValues() != null && !attribute.getValues().isEmpty()) {
                value = attribute.getValues().get(0);
            }
            break;
        }
    }
    return value;
}

From source file:com.erudika.para.security.SimpleUserService.java

/**
 * Loads a user from the data store or creates a new user from an OpenID profile
 * @param token the OpenID authentication token holding the user profile
 * @return a user object or null if user is not found
 *///  w  w  w  .j  a  va 2 s.co  m
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) {
    if (token == null) {
        return null;
    }

    User user = new User();
    user.setIdentifier(token.getIdentityUrl());
    user = loadUser(user);

    if (user == null) {
        // create new OpenID user
        String email = "email@domain.com";
        String firstName = null, lastName = null, fullName = null;
        List<OpenIDAttribute> attributes = token.getAttributes();

        for (OpenIDAttribute attribute : attributes) {
            if (attribute.getName().equals("email")) {
                email = attribute.getValues().get(0);
            }
            if (attribute.getName().equals("firstname")) {
                firstName = attribute.getValues().get(0);
            }
            if (attribute.getName().equals("lastname")) {
                lastName = attribute.getValues().get(0);
            }
            if (attribute.getName().equals("fullname")) {
                fullName = attribute.getValues().get(0);
            }
        }

        if (fullName == null) {
            if (firstName == null) {
                firstName = "No";
            }
            if (lastName == null) {
                lastName = "Name";
            }
            fullName = firstName.concat(" ").concat(lastName);
        }

        user = new User();
        user.setActive(true);
        user.setEmail(email);
        user.setName(fullName);
        user.setPassword(new UUID().toString());
        user.setIdentifier(token.getIdentityUrl());
        String id = user.create();
        if (id == null) {
            throw new BadCredentialsException("Authentication failed: cannot create new user.");
        }
    }

    return user;
}