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

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

Introduction

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

Prototype

public String getIdentityUrl() 

Source Link

Usage

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

/**
 * {@inheritDoc}/*from www .  j av 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:org.apache.rave.portal.service.impl.DefaultUserService.java

@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!");
    }/*from  www . ja  va  2 s.  c o m*/
    return user;
}

From source file:org.apache.rave.portal.web.controller.handler.OpenIDAuthenticationFailureHandler.java

@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;//from  w w  w  .  ja  va2  s.c om
    } else {
        super.onAuthenticationFailure(request, response, exception);
    }
}

From source file:org.bibsonomy.webapp.util.spring.security.rememberMeServices.OpenIDRememberMeServices.java

@Override
protected void onLoginSuccess(final HttpServletRequest request, final HttpServletResponse response,
        final Authentication successfulAuthentication) {
    if (successfulAuthentication instanceof OpenIDAuthenticationToken) {
        final OpenIDAuthenticationToken token = (OpenIDAuthenticationToken) successfulAuthentication;

        final Object principal = token.getPrincipal();

        if (principal instanceof UserDetails) {
            final UserDetails userDetails = (UserDetails) principal;
            final String username = userDetails.getUsername();
            final String openID = token.getIdentityUrl();

            final int tokenLifetime = this.getTokenValiditySeconds();
            final long expiryTime = this.calculateExpiryTime(tokenLifetime);

            final String signatureValue = this
                    .makeTokenSignature(new String[] { Long.toString(expiryTime), username, openID });

            this.setCookie(new String[] { openID, username, Long.toString(expiryTime), signatureValue },
                    tokenLifetime, request, response);

            if (log.isDebugEnabled()) {
                log.debug("Added remember-me cookie for user '" + username + "', expiry: '"
                        + new Date(expiryTime) + "'");
            }/* w ww . j a  v  a2s  .  co  m*/
        }
    }
}

From source file:org.cbioportal.security.spring.authentication.openID.PortalUserDetailsService.java

/**
 * Implementation of {@code AuthenticationUserDetailsService}
 * which allows full access to the submitted {@code Authentication} object.
 * Used by the OpenIDAuthenticationProvider.
 *///from  w ww. j av a2  s .co  m
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {

    // what we return
    PortalUserDetails toReturn = null;

    // get open id
    String id = token.getIdentityUrl();
    id = id.toLowerCase();

    // grab other open id attributes
    String email = null;
    String firstName = null;
    String lastName = null;
    String fullName = null;

    // myopenid does not return attributes in the token
    if (id.indexOf("myopenid") != -1) {
        email = id;
        fullName = id;
    } else {
        try {
            List<OpenIDAttribute> attributes = token.getAttributes();
            for (OpenIDAttribute attribute : attributes) {
                if (attribute.getName().equals("email")) {
                    email = attribute.getValues().get(0);
                    email = email.toLowerCase();
                }
                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) {
                StringBuilder fullNameBldr = new StringBuilder();
                if (firstName != null) {
                    fullNameBldr.append(firstName);
                }
                if (lastName != null) {
                    fullNameBldr.append(" ").append(lastName);
                }
                fullName = fullNameBldr.toString();
            }
        } catch (NullPointerException ex) {
            log.warn("Attribute exchange failed using OpenID " + token.getIdentityUrl() + " for everything");
            fullName = email = token.getIdentityUrl();
        }
    }

    // check if this user exists in our backend db
    try {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), attempting to fetch portal user, email: " + email);
        }
        User user = securityRepository.getPortalUser(email);
        if (user != null && user.isEnabled()) {
            if (log.isDebugEnabled()) {
                log.debug("loadUserDetails(), attempting to fetch portal user authorities, email: " + email);
            }
            UserAuthorities authorities = securityRepository.getPortalUserAuthorities(email);
            if (authorities != null) {
                List<GrantedAuthority> grantedAuthorities = AuthorityUtils.createAuthorityList(
                        authorities.getAuthorities().toArray(new String[authorities.getAuthorities().size()]));
                toReturn = new PortalUserDetails(id, grantedAuthorities);
                toReturn.setEmail(email);
                toReturn.setName(fullName);
            }
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug(e.getMessage());
        } else {
            e.printStackTrace();
        }
    }

    // outta here
    if (toReturn == null) {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), user and/or user authorities is null, email: " + email);
        }
        throw new UsernameNotFoundException("Error:  Unknown user or account disabled");
    } else {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), successfully authenticated user, email: " + email);
        }
        return toReturn;
    }
}

From source file:org.mskcc.cbio.portal.authentication.openID.PortalUserDetailsService.java

/**
 * Implementation of {@code AuthenticationUserDetailsService}
* which allows full access to the submitted {@code Authentication} object.
* Used by the OpenIDAuthenticationProvider.
 */// www.  j  a va 2s  .  com
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {

    // what we return
    PortalUserDetails toReturn = null;

    // get open id
    String id = token.getIdentityUrl();
    id = id.toLowerCase();

    // grab other open id attributes
    String email = null;
    String firstName = null;
    String lastName = null;
    String fullName = null;

    // myopenid does not return attributes in the token
    if (id.indexOf("myopenid") != -1) {
        email = id;
        fullName = id;
    } else {
        try {
            List<OpenIDAttribute> attributes = token.getAttributes();
            for (OpenIDAttribute attribute : attributes) {
                if (attribute.getName().equals("email")) {
                    email = attribute.getValues().get(0);
                    email = email.toLowerCase();
                }
                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) {
                StringBuilder fullNameBldr = new StringBuilder();
                if (firstName != null) {
                    fullNameBldr.append(firstName);
                }
                if (lastName != null) {
                    fullNameBldr.append(" ").append(lastName);
                }
                fullName = fullNameBldr.toString();
            }
        } catch (NullPointerException ex) {
            log.warn("Attribute exchange failed using OpenID " + token.getIdentityUrl() + " for everything");
            fullName = email = token.getIdentityUrl();
        }
    }

    // check if this user exists in our backend db
    try {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), attempting to fetch portal user, email: " + email);
        }
        User user = portalUserDAO.getPortalUser(email);
        if (user != null && user.isEnabled()) {
            if (log.isDebugEnabled()) {
                log.debug("loadUserDetails(), attempting to fetch portal user authorities, email: " + email);
            }
            UserAuthorities authorities = portalUserDAO.getPortalUserAuthorities(email);
            if (authorities != null) {
                List<GrantedAuthority> grantedAuthorities = AuthorityUtils.createAuthorityList(
                        authorities.getAuthorities().toArray(new String[authorities.getAuthorities().size()]));
                toReturn = new PortalUserDetails(id, grantedAuthorities);
                toReturn.setEmail(email);
                toReturn.setName(fullName);
            }
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug(e.getMessage());
        } else {
            e.printStackTrace();
        }
    }

    // outta here
    if (toReturn == null) {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), user and/or user authorities is null, email: " + email);
        }
        throw new UsernameNotFoundException("Error:  Unknown user or account disabled");
    } else {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), successfully authenticated user, email: " + email);
        }
        return toReturn;
    }
}

From source file:org.mskcc.cbio.portal.openIDlogin.OpenIDUserDetailsService.java

/**
 * Implementation of {@code AuthenticationUserDetailsService}
* which allows full access to the submitted {@code Authentication} object.
* Used by the OpenIDAuthenticationProvider.
 *//*from  ww  w. j a v  a2 s . c o  m*/
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {

    // what we return
    OpenIDUserDetails toReturn = null;

    // get open id
    String id = token.getIdentityUrl();
    id = id.toLowerCase();

    // grab other open id attributes
    String email = null;
    String firstName = null;
    String lastName = null;
    String fullName = null;

    // myopenid does not return attributes in the token
    if (id.indexOf("myopenid") != -1) {
        email = id;
        fullName = id;
    } else {
        List<OpenIDAttribute> attributes = token.getAttributes();
        for (OpenIDAttribute attribute : attributes) {
            if (attribute.getName().equals("email")) {
                email = attribute.getValues().get(0);
                email = email.toLowerCase();
            }
            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) {
            StringBuilder fullNameBldr = new StringBuilder();
            if (firstName != null) {
                fullNameBldr.append(firstName);
            }
            if (lastName != null) {
                fullNameBldr.append(" ").append(lastName);
            }
            fullName = fullNameBldr.toString();
        }
    }

    // check if this user exists in our backend db
    try {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), attempting to fetch portal user, email: " + email);
        }
        User user = portalUserDAO.getPortalUser(email);
        if (user != null && user.isEnabled()) {
            if (log.isDebugEnabled()) {
                log.debug("loadUserDetails(), attempting to fetch portal user authorities, email: " + email);
            }
            UserAuthorities authorities = portalUserDAO.getPortalUserAuthorities(email);
            if (authorities != null) {
                List<GrantedAuthority> grantedAuthorities = AuthorityUtils
                        .createAuthorityList(authorities.getAuthorities().toArray(new String[0]));
                toReturn = new OpenIDUserDetails(id, grantedAuthorities);
                toReturn.setEmail(email);
                toReturn.setName(fullName);
            }
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug(e.getMessage());
        } else {
            e.printStackTrace();
        }
    }

    // outta here
    if (toReturn == null) {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), user and/or user authorities is null, email: " + email);
        }
        throw new UsernameNotFoundException("Error:  Unknown user or account disabled");
    } else {
        if (log.isDebugEnabled()) {
            log.debug("loadUserDetails(), successfully authenticated user, email: " + email);
        }
        return toReturn;
    }
}

From source file:org.opendatakit.common.security.spring.WrappingOpenIDAuthenticationProvider.java

@Override
protected Authentication createSuccessfulAuthentication(UserDetails rawUserDetails,
        OpenIDAuthenticationToken auth) {
    String eMail = null;//from  w  w  w . ja v  a 2  s. c  om
    List<OpenIDAttribute> oAttrList = auth.getAttributes();
    for (OpenIDAttribute oAttr : oAttrList) {
        if ("email".equals(oAttr.getName())) {
            Object o = oAttr.getValues().get(0);
            if (o != null) {
                eMail = (String) o;
            }
        }
    }
    if (eMail == null) {
        logger.warn("OpenId attributes did not include an e-mail address! ");
        throw new UsernameNotFoundException("email address not supplied in OpenID attributes");
    }
    eMail = WrappingOpenIDAuthenticationProvider.normalizeMailtoAddress(eMail);
    String mailtoDomain = WrappingOpenIDAuthenticationProvider.getMailtoDomain(eMail);

    UserDetails userDetails = rawUserDetails;

    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();

    authorities.addAll(userDetails.getAuthorities());
    // add the AUTH_OPENID granted authority,
    authorities.add(new SimpleGrantedAuthority(GrantedAuthorityName.AUTH_OPENID.toString()));

    // attempt to look user up in registered users table...
    String username = null;
    UserDetails partialDetails = null;
    boolean noRights = false;
    try {
        partialDetails = wrappingUserDetailsService.loadUserByUsername(eMail);
        // found the user in the table -- fold in authorizations and get uriUser.
        authorities.addAll(partialDetails.getAuthorities());
        // users are blacklisted by registering them and giving them no rights.
        noRights = partialDetails.getAuthorities().isEmpty();
        username = partialDetails.getUsername();
    } catch (Exception e) {
        e.printStackTrace();
        logger.warn("OpenId attribute e-mail: " + eMail + " did not match any known e-mail addresses! "
                + e.getMessage());
        throw new UsernameNotFoundException("account not recognized");
    }

    AggregateUser trueUser = new AggregateUser(username, partialDetails.getPassword(),
            UUID.randomUUID().toString(), // junk...
            mailtoDomain, partialDetails.isEnabled(), partialDetails.isAccountNonExpired(),
            partialDetails.isCredentialsNonExpired(), partialDetails.isAccountNonLocked(), authorities);
    if (noRights
            || !(trueUser.isEnabled() && trueUser.isAccountNonExpired() && trueUser.isAccountNonLocked())) {
        logger.warn("OpenId attribute e-mail: " + eMail + " account is blocked! ");
        throw new UsernameNotFoundException("account is blocked");
    }

    return new OpenIDAuthenticationToken(trueUser, trueUser.getAuthorities(), auth.getIdentityUrl(),
            auth.getAttributes());
}