Add first stab at OpenID verification.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Apr 2012 14:44:04 +0000 (16:44 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 27 Apr 2012 14:44:04 +0000 (16:44 +0200)
src/main/java/net/pterodactylus/demoscenemusic/page/OpenIdPage.java [new file with mode: 0644]
src/main/java/net/pterodactylus/demoscenemusic/page/RegisterPage.java [new file with mode: 0644]
src/main/resources/templates/openid [new file with mode: 0644]
src/main/resources/templates/register [new file with mode: 0644]
src/main/webapp/WEB-INF/web.xml.template

diff --git a/src/main/java/net/pterodactylus/demoscenemusic/page/OpenIdPage.java b/src/main/java/net/pterodactylus/demoscenemusic/page/OpenIdPage.java
new file mode 100644 (file)
index 0000000..cb164e7
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * DemosceneMusic - OpenIdPage.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.demoscenemusic.page;
+
+import net.pterodactylus.demoscenemusic.core.Core;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.web.RedirectException;
+
+import org.openid4java.association.AssociationException;
+import org.openid4java.consumer.VerificationResult;
+import org.openid4java.discovery.DiscoveryException;
+import org.openid4java.discovery.DiscoveryInformation;
+import org.openid4java.discovery.Identifier;
+import org.openid4java.message.AuthSuccess;
+import org.openid4java.message.MessageException;
+import org.openid4java.message.ParameterList;
+import org.openid4java.message.ax.AxMessage;
+import org.openid4java.message.ax.FetchResponse;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class OpenIdPage extends BasePage {
+
+       /**
+        * @param core
+        * @param templateContextFactory
+        * @param template
+        * @param pageName
+        */
+       public OpenIdPage(Core core, TemplateContextFactory templateContextFactory, Template template) {
+               super(core, templateContextFactory, template, "openid");
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException {
+               super.processTemplate(templateContext, request);
+               ParameterList parameterList = new ParameterList(request.getServletRequest().getParameterMap());
+               DiscoveryInformation discoveryInformation = (DiscoveryInformation) request.getServletRequest().getAttribute("openid-discovery");
+               templateContext.set("openid", discoveryInformation.getClaimedIdentifier());
+               StringBuffer receivingURL = request.getServletRequest().getRequestURL();
+               String queryString = request.getServletRequest().getQueryString();
+               if (queryString != null && queryString.length() > 0) {
+                       receivingURL.append("?").append(queryString);
+               }
+               try {
+                       VerificationResult verification = getCore().getConsumerManager().verify(receivingURL.toString(), parameterList, discoveryInformation);
+                       Identifier verified = verification.getVerifiedId();
+                       if (verified != null) {
+                               System.out.println("verified.");
+                               templateContext.set("verified", true);
+                               templateContext.set("openid", verified.getIdentifier());
+                               AuthSuccess authSuccess = (AuthSuccess) verification.getAuthResponse();
+                               if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
+                                       FetchResponse fetchResponse = (FetchResponse) authSuccess.getExtension(AxMessage.OPENID_NS_AX);
+                                       System.out.println(fetchResponse.getAttributes());
+                               }
+                       }
+               } catch (MessageException me1) {
+                       me1.printStackTrace();
+               } catch (DiscoveryException de1) {
+                       de1.printStackTrace();
+               } catch (AssociationException ae1) {
+                       ae1.printStackTrace();
+               }
+       }
+
+}
diff --git a/src/main/java/net/pterodactylus/demoscenemusic/page/RegisterPage.java b/src/main/java/net/pterodactylus/demoscenemusic/page/RegisterPage.java
new file mode 100644 (file)
index 0000000..45d3534
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * DemosceneMusic - RegisterPage.java - Copyright © 2012 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.demoscenemusic.page;
+
+import java.util.List;
+
+import net.pterodactylus.demoscenemusic.core.Core;
+import net.pterodactylus.util.object.Default;
+import net.pterodactylus.util.template.Template;
+import net.pterodactylus.util.template.TemplateContext;
+import net.pterodactylus.util.template.TemplateContextFactory;
+import net.pterodactylus.util.web.Method;
+import net.pterodactylus.util.web.RedirectException;
+
+import org.openid4java.consumer.ConsumerException;
+import org.openid4java.discovery.DiscoveryException;
+import org.openid4java.discovery.DiscoveryInformation;
+import org.openid4java.message.AuthRequest;
+import org.openid4java.message.MessageException;
+
+/**
+ * TODO
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class RegisterPage extends BasePage {
+
+       private static final String RETURN_URL = "http://www.demoscenemusic.org/openid";
+
+       /**
+        * @param core
+        * @param templateContextFactory
+        * @param template
+        * @param pageName
+        */
+       public RegisterPage(Core core, TemplateContextFactory templateContextFactory, Template template) {
+               super(core, templateContextFactory, template, "register");
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       @Override
+       @SuppressWarnings("unchecked")
+       protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException {
+               super.processTemplate(templateContext, request);
+               if (request.getMethod() == Method.POST) {
+                       String openId = Default.forNull(request.getServletRequest().getParameter("openid_url"), "").trim();
+                       templateContext.set("openid_url", openId);
+                       if ("".equals(openId)) {
+                               templateContext.set("error", "openid-empty");
+                               return;
+                       }
+                       try {
+                               List<DiscoveryInformation> discoveries = getCore().getConsumerManager().discover(openId);
+                               DiscoveryInformation discoveryInformation = getCore().getConsumerManager().associate(discoveries);
+                               request.getServletRequest().setAttribute("openid-discovery", discoveryInformation);
+                               AuthRequest authRequest = getCore().getConsumerManager().authenticate(discoveryInformation, RETURN_URL);
+//                             if (discoveryInformation.isVersion2()) {
+//                             } else {
+                               throw new RedirectException(authRequest.getDestinationUrl(true));
+//                             }
+                       } catch (DiscoveryException de1) {
+                               templateContext.set("error", "openid");
+                       } catch (MessageException me1) {
+                               me1.printStackTrace();
+                       } catch (ConsumerException ce1) {
+                               ce1.printStackTrace();
+                       }
+               }
+       }
+
+}
diff --git a/src/main/resources/templates/openid b/src/main/resources/templates/openid
new file mode 100644 (file)
index 0000000..08c2ec7
--- /dev/null
@@ -0,0 +1,13 @@
+<%include include/header title=="Result of Registration">
+
+<h1>Result of Registration</h1>
+
+<p>
+       <%if verified>
+               You have verified successfully as <% openid|html>.
+       <%else>
+               You could not verify that you are <% openid|html>. Please <a href="register">register again</a>.
+       <%/if>
+</p>
+
+<%include include/footer>
diff --git a/src/main/resources/templates/register b/src/main/resources/templates/register
new file mode 100644 (file)
index 0000000..0713e6e
--- /dev/null
@@ -0,0 +1,23 @@
+<%include include/header title=="Register">
+
+<h1>Register</h1>
+
+<p>
+       In order to register with demoscenemusic.org you need an
+       <a href="http://www.openid.net/">OpenID</a>. You can get it from
+       <a href="http://www.scene.org/">scene.org</a> (using
+       <a href="http://www.sceneid.net/">sceneid.net</a>) but the one from e.g.
+       <a href="http://www.google.com/">Google</a> will do fine, too.
+</p>
+
+<form method="post">
+       <p>
+               OpenID:
+               <input type="text" name="openid_url" value="<% openid_url|html>" size="50" length="200"/>
+       </p>
+       <p>
+               <button type="submit" name="register" value="true">Register</button>
+       </p>
+</form>
+
+<%include include/footer>
index b9d19fb..aecdf26 100644 (file)
                        <param-value>net.pterodactylus.demoscenemusic.page.ArtistsPage</param-value>
                </init-param>
                <init-param>
+                       <param-name>openid</param-name>
+                       <param-value>net.pterodactylus.demoscenemusic.page.OpenIdPage</param-value>
+               </init-param>
+               <init-param>
+                       <param-name>register</param-name>
+                       <param-value>net.pterodactylus.demoscenemusic.page.RegisterPage</param-value>
+               </init-param>
+               <init-param>
                        <param-name>track</param-name>
                        <param-value>net.pterodactylus.demoscenemusic.page.TrackPage</param-value>
                </init-param>