Search in sources :

Example 1 with SRegRequest

use of org.openid4java.message.sreg.SRegRequest in project gerrit by GerritCodeReview.

the class OpenIdServiceImpl method discover.

@SuppressWarnings("unchecked")
DiscoveryResult discover(HttpServletRequest req, String openidIdentifier, SignInMode mode, boolean remember, String returnToken) {
    final State state;
    state = init(req, openidIdentifier, mode, remember, returnToken);
    if (state == null) {
        return new DiscoveryResult(DiscoveryResult.Status.NO_PROVIDER);
    }
    final AuthRequest aReq;
    try {
        aReq = manager.authenticate(state.discovered, state.retTo.toString());
        logger.atFine().log("OpenID: openid-realm=%s", state.contextUrl);
        aReq.setRealm(state.contextUrl);
        if (requestRegistration(aReq)) {
            final SRegRequest sregReq = SRegRequest.createFetchRequest();
            sregReq.addAttribute("fullname", true);
            sregReq.addAttribute("email", true);
            aReq.addExtension(sregReq);
            final FetchRequest fetch = FetchRequest.createFetchRequest();
            fetch.addAttribute("FirstName", SCHEMA_FIRSTNAME, true);
            fetch.addAttribute("LastName", SCHEMA_LASTNAME, true);
            fetch.addAttribute("Email", SCHEMA_EMAIL, true);
            aReq.addExtension(fetch);
        }
        if (0 <= papeMaxAuthAge) {
            final PapeRequest pape = PapeRequest.createPapeRequest();
            pape.setMaxAuthAge(papeMaxAuthAge);
            aReq.addExtension(pape);
        }
    } catch (MessageException | ConsumerException e) {
        logger.atSevere().withCause(e).log("Cannot create OpenID redirect for %s", openidIdentifier);
        return new DiscoveryResult(DiscoveryResult.Status.ERROR);
    }
    return new DiscoveryResult(aReq.getDestinationUrl(false), aReq.getParameterMap());
}
Also used : AuthRequest(org.openid4java.message.AuthRequest) SRegRequest(org.openid4java.message.sreg.SRegRequest) PapeRequest(org.openid4java.message.pape.PapeRequest) MessageException(org.openid4java.message.MessageException) FetchRequest(org.openid4java.message.ax.FetchRequest) ConsumerException(org.openid4java.consumer.ConsumerException)

Example 2 with SRegRequest

use of org.openid4java.message.sreg.SRegRequest in project ratpack by ratpack.

the class PatchedSampleServer method processRequest.

public String processRequest(HttpServletRequest httpReq, HttpServletResponse httpResp) throws Exception {
    // extract the parameters from the request
    ParameterList request = new ParameterList(httpReq.getParameterMap());
    String mode = request.hasParameter("openid.mode") ? request.getParameterValue("openid.mode") : null;
    Message response;
    String responseText;
    if ("associate".equals(mode)) {
        // --- process an association request ---
        response = manager.associationResponse(request);
        responseText = response.keyValueFormEncoding();
    } else if ("checkid_setup".equals(mode) || "checkid_immediate".equals(mode)) {
        // interact with the user and obtain data needed to continue
        List<?> userData = userInteraction(request);
        String userSelectedClaimedId = (String) userData.get(0);
        Boolean authenticatedAndApproved = (Boolean) userData.get(1);
        String email = (String) userData.get(2);
        // --- process an authentication request ---
        AuthRequest authReq = AuthRequest.createAuthRequest(request, manager.getRealmVerifier());
        // Sign after we added extensions.
        response = manager.authResponse(request, null, userSelectedClaimedId, authenticatedAndApproved, false);
        if (response instanceof DirectError) {
            return directResponse(httpResp, response.keyValueFormEncoding());
        } else {
            if (authReq.hasExtension(AxMessage.OPENID_NS_AX)) {
                MessageExtension ext = authReq.getExtension(AxMessage.OPENID_NS_AX);
                if (ext instanceof FetchRequest) {
                    FetchRequest fetchReq = (FetchRequest) ext;
                    Map<?, ?> required = fetchReq.getAttributes(true);
                    // Map optional = fetchReq.getAttributes(false);
                    if (required.containsKey("email")) {
                        Map<Object, Object> userDataExt = new HashMap<>();
                        // userDataExt.put("email", userData.get(3));
                        FetchResponse fetchResp = FetchResponse.createFetchResponse(fetchReq, userDataExt);
                        // (alternatively) manually add attribute values
                        fetchResp.addAttribute("email", "http://schema.openid.net/contact/email", email);
                        response.addExtension(fetchResp);
                    }
                } else {
                    throw new UnsupportedOperationException("TODO");
                }
            }
            if (authReq.hasExtension(SRegMessage.OPENID_NS_SREG)) {
                MessageExtension ext = authReq.getExtension(SRegMessage.OPENID_NS_SREG);
                if (ext instanceof SRegRequest) {
                    SRegRequest sregReq = (SRegRequest) ext;
                    List<?> required = sregReq.getAttributes(true);
                    // List optional = sregReq.getAttributes(false);
                    if (required.contains("email")) {
                        // data released by the user
                        Map<Object, Object> userDataSReg = new HashMap<>();
                        // userData.put("email", "user@example.com");
                        SRegResponse sregResp = SRegResponse.createSRegResponse(sregReq, userDataSReg);
                        // (alternatively) manually add attribute values
                        sregResp.addAttribute("email", email);
                        response.addExtension(sregResp);
                    }
                } else {
                    throw new UnsupportedOperationException("TODO");
                }
            }
            // Sign the auth success message.
            if (response instanceof AuthSuccess) {
                manager.sign((AuthSuccess) response);
            }
            // option1: GET HTTP-redirect to the return_to URL
            return response.getDestinationUrl(true);
        // option2: HTML FORM Redirection
        // RequestDispatcher dispatcher =
        // getServletContext().getRequestDispatcher("formredirection.jsp");
        // httpReq.setAttribute("prameterMap", response.getParameterMap());
        // httpReq.setAttribute("destinationUrl", response.getDestinationUrl(false));
        // dispatcher.forward(request, response);
        // return null;
        }
    } else if ("check_authentication".equals(mode)) {
        // --- processing a verification request ---
        response = manager.verify(request);
        responseText = response.keyValueFormEncoding();
    } else {
        // --- error response ---
        response = DirectError.createDirectError("Unknown request");
        responseText = response.keyValueFormEncoding();
    }
    // return the result to the user
    return responseText;
}
Also used : SRegRequest(org.openid4java.message.sreg.SRegRequest) AxMessage(org.openid4java.message.ax.AxMessage) SRegMessage(org.openid4java.message.sreg.SRegMessage) FetchResponse(org.openid4java.message.ax.FetchResponse) SRegResponse(org.openid4java.message.sreg.SRegResponse) FetchRequest(org.openid4java.message.ax.FetchRequest) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

FetchRequest (org.openid4java.message.ax.FetchRequest)2 SRegRequest (org.openid4java.message.sreg.SRegRequest)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConsumerException (org.openid4java.consumer.ConsumerException)1 AuthRequest (org.openid4java.message.AuthRequest)1 MessageException (org.openid4java.message.MessageException)1 AxMessage (org.openid4java.message.ax.AxMessage)1 FetchResponse (org.openid4java.message.ax.FetchResponse)1 PapeRequest (org.openid4java.message.pape.PapeRequest)1 SRegMessage (org.openid4java.message.sreg.SRegMessage)1 SRegResponse (org.openid4java.message.sreg.SRegResponse)1