Package org.openid4java.message

Examples of org.openid4java.message.Message


      requestParams = new ParameterList(httpReq.getParameterMap());
    }

    String mode = requestParams.getParameterValue("openid.mode");
    StringBuffer responseText = new StringBuffer();
    Message responseMessage;

    if ("associate".equals(mode)) {
      // --- process an association request ---
      responseMessage = manager.associationResponse(requestParams);
      responseText.append(responseMessage.keyValueFormEncoding());
    } else if ("checkid_setup".equals(mode) ||
        "checkid_immediate".equals(mode)) {

      Boolean authenticatedAndApproved = Boolean.FALSE;
      if (session.getAttribute("authenticatedAndApproved") != null) {
        authenticatedAndApproved =
          (Boolean) session.getAttribute("authenticatedAndApproved");
      }

      if (!authenticatedAndApproved) {
        // Interact with the user and obtain data needed to continue
        session.setAttribute("parameterlist", requestParams);
        httpResp.sendRedirect("authorize");
        return;
      }

      String userId = (String) session.getAttribute("openid.claimed_id");
      String userClaimedId = (String) session.getAttribute("openid.identity");
      // Remove the parameterlist so this provider can accept any request
      session.removeAttribute("parameterlist");
      // Makes you authorize each and every time
      session.setAttribute("authenticatedAndApproved", Boolean.FALSE);

      // Process an authorization event
      responseMessage = manager.authResponse(requestParams, userId,
          userClaimedId, authenticatedAndApproved.booleanValue());

      if (responseMessage instanceof AuthSuccess) {
        // Try adding an AX Fetch Response
        String email = (String) session.getAttribute("email");
        FetchResponse2 fetchResponse = new FetchResponse2();
        try {
          if (email != null) {
            fetchResponse.addAttribute(Step2.AxSchema.EMAIL.getShortName(),
                Step2.AxSchema.EMAIL.getUri(), email);
          }
          String country = (String) session.getAttribute("country");
          if (country != null) {
            fetchResponse.addAttribute(Step2.AxSchema.COUNTRY.getShortName(),
                Step2.AxSchema.COUNTRY.getUri(), country);
          }
          responseMessage.addExtension(fetchResponse);
        } catch (MessageException e) {
          throw new ServletException(e);
        }

        // Handle any OAuth Request tokens
        String oauthRequestToken =
          (String) session.getAttribute("oauth_request_token");
        if (oauthRequestToken != null) {
          // This is a request token response
          HybridOauthResponse hybridResponse =
              new HybridOauthResponse(oauthRequestToken, "");
          try {
            responseMessage.addExtension(hybridResponse);
          } catch (MessageException e) {
            throw new ServletException(e);
          }
        }

        httpResp.sendRedirect(
            ((AuthSuccess) responseMessage).getDestinationUrl(true));
        return;
      }
      responseText.append("<pre>");
      responseText.append(responseMessage.keyValueFormEncoding());
      responseText.append("</pre>");
    }
    OutputStream os = httpResp.getOutputStream();
    os.write(responseText.toString().getBytes());
    os.close();
View Full Code Here


        log.info("OP endpoint = " + manager.getOPEndpointUrl());

        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,
                    manager.getOPEndpointUrl());

            String userSelectedId = (String) userData.get(0);
            String userSelectedClaimedId = (String) userData.get(1);
            Boolean authenticatedAndApproved = (Boolean) userData.get(2);

            // --- process an authentication request ---
            response = manager.authResponse(request, userSelectedId,
                    userSelectedClaimedId,
                    authenticatedAndApproved.booleanValue());

            if (response instanceof DirectError) {
                Form f = new Form();
                @SuppressWarnings("unchecked")
                Map<String, String> m = (Map<String, String>) response
                        .getParameterMap();
                for (String key : m.keySet()) {
                    f.add(key, m.get(key));
                }
                return f.getWebRepresentation();
            } else {
                // caller will need to decide which of the following to use:

                // option1: GET HTTP-redirect to the return_to URL
                // return new
                // StringRepresentation(response.getDestinationUrl(true));
                redirectSeeOther(response.getDestinationUrl(true));
                return new EmptyRepresentation();

                // 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);
            log.info("OpenID : " + response.keyValueFormEncoding());
            responseText = response.keyValueFormEncoding();
        } else if (Method.GET.equals(getMethod())) {
            // Could be a discovery request
            sendXRDSLocation();
            return new StringRepresentation("XRDS Discovery Information");
        } else {
            // --- error response ---
            response = DirectError.createDirectError("Unknown request");
            responseText = response.keyValueFormEncoding();
        }

        // return the result to the user
        return new StringRepresentation(responseText);
    }
View Full Code Here

   
    return false;
  }

  private String unknownError(HttpServletRequest request, HttpServletResponse response, ParameterList parameterList) throws IOException
    Message messageResponse;
    String responseText;
    // --- error response ---
    // When openid.mode = null or does not match any of the standard modes.
    messageResponse = DirectError.createDirectError("Unknown request");
    responseText = messageResponse.keyValueFormEncoding();
    // return the result to the user
    return directResponse(response,messageResponse.keyValueFormEncoding());
  }
View Full Code Here

  }

  private String checkAuthentication(HttpServletRequest request, HttpServletResponse response, ParameterList parameterList) throws IOException {
    ServerManager manager = ((SimpleServiceProxy)getServiceProxy()).getServerManager();
    HttpSession session = request.getSession();
    Message messageResponse;
    String responseText;
   
    // --- processing a verification request ---
    messageResponse = manager.verify(parameterList);
    responseText = messageResponse.keyValueFormEncoding();
    return directResponse(response, messageResponse.keyValueFormEncoding());
  }
View Full Code Here

  }

  private String checkId(HttpServletRequest request, HttpServletResponse response, ParameterList parameterList) throws ServletException, IOException {
    ServerManager manager = ((SimpleServiceProxy)getServiceProxy()).getServerManager();
    HttpSession session = request.getSession();
    Message messageResponse;
    String responseText;
   
    // interact with the user and obtain data needed to continue
    String userSelectedId = null;
    String userSelectedClaimedId = null;
    User authenticatedAndApproved = null;
    String email = "";

    if (session.getAttribute("authenticatedAndApproved") == null) {
      session.setAttribute("parameterlist", parameterList);
      String wwwParams = wwwFormEncoding(parameterList);
      String url = "login.jsp?" + wwwParams;
      response.sendRedirect(url);
      return null;
    } else {
      userSelectedId = (String) session.getAttribute("openid.claimed_id");
      userSelectedClaimedId = (String) session.getAttribute("openid.identity");
      authenticatedAndApproved = (User) session.getAttribute("authenticatedAndApproved");
      // Remove the parameterlist so this provider can accept requests from elsewhere
      session.removeAttribute("parameterlist");
      session.setAttribute("authenticatedAndApproved", null);
    }

    // --- process an authentication request ---
    AuthRequest authReq = null;
    String opLocalId = null;

    try {
      authReq = AuthRequest.createAuthRequest(parameterList, manager.getRealmVerifier());
      // if the user chose a different claimed_id than the one in request
      if (userSelectedClaimedId != null && userSelectedClaimedId.equals(authReq.getClaimed())) {
        //opLocalId = "http://localhost:8081/idp4java/idp";//lookupLocalId(userSelectedClaimedId);
      }
    } catch (MessageException e) {
      e.printStackTrace();
      throw new ServletException(e);
    }

    // --- process an authentication request ---
    //messageResponse = manager.authResponse(parameterList, userSelectedId, userSelectedClaimedId, true);
    messageResponse = manager.authResponse(parameterList, opLocalId, opLocalId, true);
   

    if (messageResponse instanceof DirectError)
      return directResponse(response, messageResponse.keyValueFormEncoding());
    else {

            try {
        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 userDataExt = new HashMap();
                    userDataExt.put("email", "someone@someplace.com");
                    FetchResponse fetchResp = FetchResponse.createFetchResponse(fetchReq, userDataExt);
                    // (alternatively) manually add attribute values
                    //fetchResp.addAttribute("email", "http://schema.openid.net/contact/email", email);
                    messageResponse.addExtension(fetchResp);
                }
            }
            else //if (ext instanceof StoreRequest)
            {
                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 userDataSReg = new HashMap();
                    userDataSReg.put("email", "user@example.com");

                    SRegResponse sregResp = SRegResponse.createSRegResponse(sregReq, userDataSReg);
                    // (alternatively) manually add attribute values
                    //sregResp.addAttribute("email", email);
                    messageResponse.addExtension(sregResp);
                }
            }
            else
            {
                throw new UnsupportedOperationException("TODO");
            }
        }

        // Sign the auth success message.
        // This is required as AuthSuccess.buildSignedList has a `todo' tag now.
        manager.sign((AuthSuccess) messageResponse);
      } catch (MessageException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (ServerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (AssociationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

            // caller will need to decide which of the following to use:

            // option1: GET HTTP-redirect to the return_to URL
           
        session.setAttribute("SUCCESS", Boolean.TRUE);
        response.sendRedirect(messageResponse.getDestinationUrl(true));

            // option2: HTML FORM Redirection
            //RequestDispatcher dispatcher =
            //        getServletContext().getRequestDispatcher("formredirection.jsp");
            //httpReq.setAttribute("prameterMap", response.getParameterMap());
View Full Code Here

  }

  private String associate(HttpServletRequest request, HttpServletResponse response, ParameterList parameterList) throws IOException {
    ServerManager manager = ((SimpleServiceProxy)getServiceProxy()).getServerManager();
    HttpSession session = request.getSession();
    Message messageResponse;
    String responseText;
    // --- process an association parameterList ---
    messageResponse = manager.associationResponse(parameterList);
    responseText = messageResponse.keyValueFormEncoding();
    return directResponse(response, messageResponse.keyValueFormEncoding());
   
  }
View Full Code Here

        cancel(req, rsp);
      }
      return;
    }

    final Message authRsp = result.getAuthResponse();
    SRegResponse sregRsp = null;
    FetchResponse fetchRsp = null;

    if (0 <= papeMaxAuthAge) {
      PapeResponse ext;
      boolean unsupported = false;

      try {
        ext = (PapeResponse) authRsp.getExtension(PapeMessage.OPENID_NS_PAPE);
      } catch (MessageException err) {
        // Far too many providers are unable to provide PAPE extensions
        // right now. Instead of blocking all of them log the error and
        // let the authentication complete anyway.
        //
        log.error("Invalid PAPE response " + openidIdentifier + ": " + err);
        unsupported = true;
        ext = null;
      }
      if (!unsupported && ext == null) {
        log.error("No PAPE extension response from " + openidIdentifier);
        cancelWithError(req, rsp, "OpenID provider does not support PAPE.");
        return;
      }
    }

    if (authRsp.hasExtension(SRegMessage.OPENID_NS_SREG)) {
      final MessageExtension ext =
          authRsp.getExtension(SRegMessage.OPENID_NS_SREG);
      if (ext instanceof SRegResponse) {
        sregRsp = (SRegResponse) ext;
      }
    }

    if (authRsp.hasExtension(AxMessage.OPENID_NS_AX)) {
      final MessageExtension ext = authRsp.getExtension(AxMessage.OPENID_NS_AX);
      if (ext instanceof FetchResponse) {
        fetchRsp = (FetchResponse) ext;
      }
    }
View Full Code Here

        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 userSelectedId = (String) userData.get(0);
            String userSelectedClaimedId = (String) userData.get(1);
            Boolean authenticatedAndApproved = (Boolean) userData.get(2);

            // --- process an authentication request ---
            response = manager.authResponse(request,
                    userSelectedId,
                    userSelectedClaimedId,
                    authenticatedAndApproved.booleanValue());

            if (response instanceof DirectError)
                return directResponse(httpResp, response.keyValueFormEncoding());
            else
            {
                // caller will need to decide which of the following to use:

                // 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;
    }
View Full Code Here

            {
                throw new InfocardException(
                    "Error extracting OpenID message from the xmlToken", e);
            }

            Message message = Message.createMessage(
                ParameterList.createFromKeyValueForm(keyValueForm));

            return new OpenIDToken(message);

        // DOM exceptions :
View Full Code Here

        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());

            String opLocalId = null;
            // if the user chose a different claimed_id than the one in request
            if (userSelectedClaimedId != null &&
                userSelectedClaimedId.equals(authReq.getClaimed()))
            {
                //opLocalId = lookupLocalId(userSelectedClaimedId);
            }

            response = manager.authResponse(request,
                    opLocalId,
                    userSelectedClaimedId,
                    authenticatedAndApproved.booleanValue(),
                    false); // Sign after we added extensions.

            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 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 //if (ext instanceof StoreRequest)
                    {
                        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 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.
                // This is required as AuthSuccess.buildSignedList has a `todo' tag now.
                manager.sign((AuthSuccess) response);

                // caller will need to decide which of the following to use:

                // 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;
    }
View Full Code Here

TOP

Related Classes of org.openid4java.message.Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.