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

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

Introduction

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

Prototype

public Object getDetails() 

Source Link

Usage

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

/**
 * Handle turning an OpenID (2) token into a user.
 *///from  w ww .  j a va2 s . c o  m
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;
    }
}

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. //  w ww. j a  v  a 2s  .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:net.firejack.platform.web.security.spring.openid.OpenIDAuthenticationManager.java

@Override
protected Authentication doAuthentication(Authentication authentication) throws AuthenticationException {
    if (authentication instanceof OpenIDAuthenticationToken) {
        OpenIDAuthenticationToken token = (OpenIDAuthenticationToken) authentication;
        if (!OpenIDAuthenticationStatus.SUCCESS.equals(token.getStatus())) {
            String errorMessage = MessageResolver.messageFormatting("login.wrong.credentials", null);
            throw new BadCredentialsException(errorMessage);
        }/*from  w ww  .  j  a  v a2s  .  c  o m*/
        String email = findAttributeValueByName(SupportedOpenIDAttribute.EMAIL, token.getAttributes());
        if (StringUtils.isBlank(email)) {
            String errorMessage = MessageResolver.messageFormatting("login.wrong.credentials", null);
            throw new BadCredentialsException(errorMessage);
        }

        HttpSession session = ((SessionContainerWebAuthenticationDetails) token.getDetails()).getSession();

        if (authenticator != null) {
            AuthenticatorFactory authenticatorFactory = AuthenticatorFactory.getInstance();
            Map<SupportedOpenIDAttribute, String> attributeValues = findAttributeValues(token.getAttributes());
            OpenIDAuthenticationSource openIDAuthenticationSource = (OpenIDAuthenticationSource) authenticatorFactory
                    .provideOpenIDAuthenticationSource(email, attributeValues);
            IAuthenticationDetails authenticationDetails = authenticator
                    .authenticate(openIDAuthenticationSource);
            if (authenticationDetails != null) {
                return generateDefaultToken(authenticationDetails, session);
            }
        }
    }

    String errorMessage = MessageResolver.messageFormatting("login.authentication.failure", null);
    throw new BadCredentialsException(errorMessage);
}