/websrc/xmldap_oc/consumer_redirect.jsp

http://openinfocard.googlecode.com/ · JavaServer Pages · 106 lines · 59 code · 16 blank · 31 comment · 4 complexity · 2b9a90c1bbb7301fe49e0fbd89f49cc2 MD5 · raw file

  1. <%@ page session="true" %>
  2. <%@ page import="java.util.Map,java.util.Iterator,org.openid4java.discovery.Identifier, org.openid4java.discovery.DiscoveryInformation, org.openid4java.message.ax.FetchRequest, org.openid4java.message.ax.FetchResponse, org.openid4java.message.ax.AxMessage, org.openid4java.message.*, org.openid4java.OpenIDException, java.util.List, java.io.IOException, javax.servlet.http.HttpSession, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.openid4java.consumer.ConsumerManager, org.openid4java.consumer.InMemoryConsumerAssociationStore, org.openid4java.consumer.InMemoryNonceVerifier" %>
  3. <%
  4. // README:
  5. // Set the returnToUrl string to the appropriate value for this JSP
  6. // Since you may be deployed behind apache, etc, the jsp has no real idea what the
  7. // absolute URI is to get back here.
  8. Object o = pageContext.getAttribute("consumermanager", PageContext.APPLICATION_SCOPE);
  9. if (o == null)
  10. {
  11. ConsumerManager newmgr=new ConsumerManager();
  12. newmgr.setAssociations(new InMemoryConsumerAssociationStore());
  13. newmgr.setNonceVerifier(new InMemoryNonceVerifier(5000));
  14. pageContext.setAttribute("consumermanager", newmgr, PageContext.APPLICATION_SCOPE);
  15. }
  16. ConsumerManager manager=(ConsumerManager) pageContext.getAttribute("consumermanager", PageContext.APPLICATION_SCOPE);
  17. String openid=request.getParameter("openid");
  18. try
  19. {
  20. // determine a return_to URL where your application will receive
  21. // the authentication responses from the OpenID provider
  22. // YOU SHOULD CHANGE THIS TO GO TO THE
  23. String returnToUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/xmldap_oc/consumer_returnurl.jsp";
  24. // perform discovery on the user-supplied identifier
  25. List discoveries = manager.discover(openid);
  26. // attempt to associate with an OpenID provider
  27. // and retrieve one service endpoint for authentication
  28. DiscoveryInformation discovered = manager.associate(discoveries);
  29. // store the discovery information in the user's session
  30. session.setAttribute("openid-disco", discovered);
  31. // obtain a AuthRequest message to be sent to the OpenID provider
  32. AuthRequest authReq = manager.authenticate(discovered, returnToUrl);
  33. // Attribute Exchange example: fetching the 'email' attribute
  34. //FetchRequest fetch = FetchRequest.createFetchRequest();
  35. //fetch.addAttribute("email",
  36. // attribute alias
  37. // "http://schema.openid.net/contact/email", // type URI
  38. // true); // required
  39. // attach the extension to the authentication request
  40. //authReq.addExtension(fetch);
  41. if (! discovered.isVersion2() )
  42. {
  43. // Option 1: GET HTTP-redirect to the OpenID Provider endpoint
  44. // The only method supported in OpenID 1.x
  45. // redirect-URL usually limited ~2048 bytes
  46. response.sendRedirect(authReq.getDestinationUrl(true));
  47. }
  48. else
  49. {
  50. // Option 2: HTML FORM Redirection
  51. // Allows payloads > 2048 bytes
  52. // <FORM action="OpenID Provider's service endpoint">
  53. // see samples/formredirection.jsp for a JSP example
  54. //authReq.getOPEndpoint();
  55. // build a HTML FORM with the message parameters
  56. //authReq.getParameterMap();
  57. %>
  58. <html xmlns="http://www.w3.org/1999/xhtml">
  59. <head>
  60. <title>OpenID HTML FORM Redirection</title>
  61. </head>
  62. <!--<body onload="document.forms['openid-form-redirection'].submit();">-->
  63. <body>
  64. <form name="openid-form-redirection" action="<%= authReq.getOPEndpoint() %>" method="post" accept-charset="utf-8">
  65. <%
  66. Map pm=authReq.getParameterMap();
  67. Iterator keyit=pm.keySet().iterator();
  68. Object key;
  69. Object value;
  70. while (keyit.hasNext())
  71. {
  72. key=keyit.next();
  73. value=pm.get(key);
  74. %>
  75. <input type="hidden" name="<%= key%>" value="<%= value%>"/>
  76. <%
  77. }
  78. %>
  79. <button type="submit">Continue...</button>
  80. </form>
  81. </body>
  82. </html>
  83. <%
  84. }
  85. }
  86. catch (OpenIDException e) {
  87. // present error to the user
  88. response.sendError(500);
  89. }
  90. %>