Add page that lists all parties.
[demoscenemusic.git] / src / main / java / net / pterodactylus / demoscenemusic / page / OpenIdPage.java
1 /*
2  * DemosceneMusic - OpenIdPage.java - Copyright © 2012 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.demoscenemusic.page;
19
20 import net.pterodactylus.demoscenemusic.core.Core;
21 import net.pterodactylus.util.template.Template;
22 import net.pterodactylus.util.template.TemplateContext;
23 import net.pterodactylus.util.template.TemplateContextFactory;
24 import net.pterodactylus.util.web.RedirectException;
25
26 import org.openid4java.association.AssociationException;
27 import org.openid4java.consumer.VerificationResult;
28 import org.openid4java.discovery.DiscoveryException;
29 import org.openid4java.discovery.DiscoveryInformation;
30 import org.openid4java.discovery.Identifier;
31 import org.openid4java.message.AuthSuccess;
32 import org.openid4java.message.MessageException;
33 import org.openid4java.message.ParameterList;
34 import org.openid4java.message.ax.AxMessage;
35 import org.openid4java.message.ax.FetchResponse;
36
37 /**
38  * TODO
39  *
40  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41  */
42 public class OpenIdPage extends BasePage {
43
44         /**
45          * @param core
46          * @param templateContextFactory
47          * @param template
48          * @param pageName
49          */
50         public OpenIdPage(Core core, TemplateContextFactory templateContextFactory, Template template) {
51                 super(core, templateContextFactory, template, "openid");
52         }
53
54         /**
55          * {@inheritDoc}
56          */
57         @Override
58         protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException {
59                 super.processTemplate(templateContext, request);
60                 ParameterList parameterList = new ParameterList(request.getServletRequest().getParameterMap());
61                 DiscoveryInformation discoveryInformation = (DiscoveryInformation) request.getServletRequest().getAttribute("openid-discovery");
62                 templateContext.set("openid", discoveryInformation.getClaimedIdentifier());
63                 StringBuffer receivingURL = request.getServletRequest().getRequestURL();
64                 String queryString = request.getServletRequest().getQueryString();
65                 if (queryString != null && queryString.length() > 0) {
66                         receivingURL.append("?").append(queryString);
67                 }
68                 try {
69                         VerificationResult verification = getCore().getConsumerManager().verify(receivingURL.toString(), parameterList, discoveryInformation);
70                         Identifier verified = verification.getVerifiedId();
71                         if (verified != null) {
72                                 System.out.println("verified.");
73                                 templateContext.set("verified", true);
74                                 templateContext.set("openid", verified.getIdentifier());
75                                 AuthSuccess authSuccess = (AuthSuccess) verification.getAuthResponse();
76                                 if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
77                                         FetchResponse fetchResponse = (FetchResponse) authSuccess.getExtension(AxMessage.OPENID_NS_AX);
78                                         System.out.println(fetchResponse.getAttributes());
79                                 }
80                         }
81                 } catch (MessageException me1) {
82                         me1.printStackTrace();
83                 } catch (DiscoveryException de1) {
84                         de1.printStackTrace();
85                 } catch (AssociationException ae1) {
86                         ae1.printStackTrace();
87                 }
88         }
89
90 }