Example usage for org.springframework.security.openid OpenIDAuthenticationToken getAttributes

List of usage examples for org.springframework.security.openid OpenIDAuthenticationToken getAttributes

Introduction

In this page you can find the example usage for org.springframework.security.openid OpenIDAuthenticationToken getAttributes.

Prototype

public List<OpenIDAttribute> getAttributes() 

Source Link

Usage

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 .  j a  v  a2  s. c  o m
    }
    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;/*from www. ja v a2  s.c  o  m*/
    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:it.av.youeat.web.security.OpenIDAttributes2UserDetailsImpl.java

/**
 * {@inheritDoc}//  www .  j a v a2  s .com
 */
@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: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;
        }/*  w w  w. j  a v a  2 s.com*/
        if (attr.getValues().size() == 1 && attr.getValues().get(0) == null) {
            continue;
        }
        attributes.add(attr);
    }
    return attributes;
}

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

/**
 * {@inheritDoc}/*from   w ww.  java 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: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());
    }/*  w ww .  j a v a  2s .com*/
    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: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 w  w  w. jav  a2s.  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:com.wisemapping.security.UserDetailsService.java

@NotNull
private User buildUserFromToken(@NotNull OpenIDAuthenticationToken token) {
    final User result = new User();

    String lastName = null;/*from  w w  w  .  j a  v  a  2  s  .  c om*/
    String firstName = null;
    String email = null;
    String fullName = null;

    final 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 (lastName == null || firstName == null) {
        result.setFirstname(fullName);
        result.setLastname("");
    } else {
        result.setLastname(lastName);
        result.setFirstname(firstName);
    }
    result.setEmail(email);
    result.setPassword("");

    final Calendar now = Calendar.getInstance();
    result.setActivationDate(now);
    return result;
}

From source file:net.triptech.buildulator.service.OpenIdAuthenticationFailureHandler.java

/**
 * Creates the person object based on the supplied OpenID attributes.
 *
 * @param token the token//from   w  ww . j  a v a2  s.  com
 * @return the person
 */
private Person createPerson(final OpenIDAuthenticationToken token) {

    Person person = new Person();

    // The person does not exist, create
    String email = null;
    String firstName = null;
    String lastName = null;

    String id = token.getIdentityUrl();
    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 (StringUtils.isBlank(email)) {
        Random generator = new Random();
        email = String.valueOf(generator.nextInt()) + "@"
                + String.valueOf(Calendar.getInstance().getTimeInMillis());
    }
    if (StringUtils.isBlank(firstName)) {
        firstName = "New";
    }
    if (StringUtils.isBlank(lastName)) {
        lastName = "User";
    }

    UserRole role = UserRole.ROLE_USER;
    if (Person.countPeople() == 0) {
        role = UserRole.ROLE_ADMIN;
    }

    person = new Person();
    person.setOpenIdIdentifier(id);

    person.setEmailAddress(email);
    person.setFirstName(firstName);
    person.setLastName(lastName);
    person.setUserRole(role);
    person.setUserStatus(UserStatus.ACTIVE);

    person.persist();

    return person;
}

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);
            }//  w  ww. j  ava2  s .  c om
        }
        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;
}