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

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

Introduction

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

Prototype

public String getName() 

Source Link

Usage

From source file:com.rd.adchallenge.security.OpenIdUserDetailsService.java

public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {
    return new User(token.getName(), "", AuthorityUtils.createAuthorityList("ROLE_USER"));
}

From source file:org.mitre.provenance.openid.OpenId2AuthenticationUserDetailsService.java

public UserDetails loadUserDetails(OpenIDAuthenticationToken auth) throws UsernameNotFoundException {
    Set<SimpleGrantedAuthority> authorities = new HashSet<SimpleGrantedAuthority>();
    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));

    OpenIDAuthenticationToken oidToken = (OpenIDAuthenticationToken) auth;

    String oid2UniqueId = oidToken.getName();
    return new User(oid2UniqueId, "", authorities);
}

From source file:org.mitre.provenance.openid.OpenIDInterceptorFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpSession session = httpRequest.getSession();

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    //Only proceed if we have a logged-in user AND there is no PlusUser in the request
    //already. //from ww w .j  a  v  a2s .c  o m
    if (auth != null && session.getAttribute(PLUS_USER) == null) {
        System.err.println("FILTER: checking auth type");
        //If OpenID Connect:
        if (auth instanceof OIDCAuthenticationToken) {
            System.err.println("FILTER: OIDC");
            User user = handle((OIDCAuthenticationToken) auth);
            session.setAttribute(PLUS_USER, user);
        } else if (auth instanceof OpenIDAuthenticationToken) {
            OpenIDAuthenticationToken oidToken = (OpenIDAuthenticationToken) auth;
            String oid2UniqueId = oidToken.getName();

            System.err.println("FILTER: OpenID2 Token ID " + oid2UniqueId + " cred " + oidToken.getCredentials()
                    + " details " + oidToken.getDetails() + " principal " + oidToken.getPrincipal()
                    + " message " + oidToken.getMessage());

            User user = handle(oidToken);
            session.setAttribute(PLUS_USER, user);
        } else
            log.warning("Unrecognized token " + auth.getClass().getName());
    }

    //Continue the filter chain
    filterChain.doFilter(httpRequest, response);
}

From source file:org.mitre.provenance.openid.OpenIDInterceptorFilter.java

/**
 * Handle turning an OpenID (2) token into a user.
 */// w ww . java 2 s . c om
protected User handle(OpenIDAuthenticationToken oidToken) {
    String oid2UniqueId = oidToken.getName();

    System.err.println("FILTER: OpenID2 Token ID " + oid2UniqueId + " cred " + oidToken.getCredentials()
            + " details " + oidToken.getDetails() + " principal " + oidToken.getPrincipal() + " message "
            + oidToken.getMessage());

    User existingUser = null;

    try {
        PLUSActor a = Neo4JPLUSObjectFactory.getActor(oid2UniqueId);
        if (a instanceof User)
            existingUser = (User) a;
    } catch (PLUSException exc) {
        log.severe("Could not load actor by ID " + oid2UniqueId);
        exc.printStackTrace();
    }

    if (existingUser != null) {
        // System.err.println("FILTER: OpenID2 existing user " + existingUser);
        return existingUser;
    } else {
        List<OpenIDAttribute> attributes = oidToken.getAttributes();

        System.err.println("FILTER: OpenID2 new user with " + attributes.size() + " attributes.");

        String oid2DisplayName = null;
        String oid2FirstName = null;
        String oid2LastName = null;
        String email = null;

        for (OpenIDAttribute attr : attributes) {
            String attrName = attr.getName();

            StringBuffer vals = new StringBuffer("");
            for (String val : attr.getValues())
                vals.append(val + "/");
            System.err.println("OPEN ID ATTRIBUTE:  " + attrName + " type " + attr.getType() + " vals " + vals);

            if (attrName.equals("name")) {
                //This is the OpenID 2.0 display name.
                //OpenID 2.0 Attribute Exchange (AX) is a little finicky, so this value
                //may not be populated or may be stored uner a different attribute name.
                oid2DisplayName = attr.getValues().get(0);
            } else if (attrName.equals("firstName")) {
                oid2FirstName = attr.getValues().get(0);
            } else if (attrName.equals("lastName")) {
                oid2LastName = attr.getValues().get(0);
            } else if (attrName.equals("email")) {
                email = attr.getValues().get(0);
            }
        }

        if (oid2DisplayName == null) {
            // Google sends first and last rather than "name"
            oid2DisplayName = oid2FirstName + oid2LastName;
        }

        OpenIDUser oid2User = new OpenIDUser(oid2UniqueId,
                (oid2DisplayName != null) ? oid2DisplayName : "Name Not Provided");
        oid2User.setEmail(email);

        // TODO:  Remove
        oid2User.addPrivilege(PrivilegeClass.ADMIN);
        oid2User.addPrivilege(PrivilegeClass.PUBLIC);

        try {
            if (client.actorExists(oid2User.getId()) == null)
                client.report(ProvenanceCollection.collect(oid2User));
        } catch (PLUSException exc) {
            log.severe("Could not save new user entry " + oid2User);
            exc.printStackTrace();
        }

        System.err.println("FILTER: set new OpenID2 user " + oid2User);
        return oid2User;
    }
}