1.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sat Oct 23 22:51:43 2010 +0200
1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Thu Nov 25 23:08:20 2010 +0100
1.3 @@ -32,7 +32,6 @@
1.4 import java.io.StringWriter;
1.5 import java.net.URI;
1.6 import java.text.MessageFormat;
1.7 -import java.text.ParseException;
1.8 import java.util.ArrayList;
1.9 import java.util.Arrays;
1.10 import java.util.Date;
1.11 @@ -44,6 +43,8 @@
1.12 import java.util.Properties;
1.13 import java.util.ResourceBundle;
1.14 import java.util.concurrent.Callable;
1.15 +import java.util.logging.Level;
1.16 +import java.util.logging.Logger;
1.17 import javax.ws.rs.DefaultValue;
1.18 import javax.ws.rs.FormParam;
1.19 import javax.ws.rs.GET;
1.20 @@ -61,6 +62,16 @@
1.21 import javax.ws.rs.core.NewCookie;
1.22 import javax.ws.rs.core.Response;
1.23 import javax.ws.rs.core.Response.ResponseBuilder;
1.24 +import org.openid4java.consumer.ConsumerManager;
1.25 +import org.openid4java.consumer.VerificationResult;
1.26 +import org.openid4java.discovery.DiscoveryInformation;
1.27 +import org.openid4java.message.AuthRequest;
1.28 +import org.openid4java.message.MessageExtension;
1.29 +import org.openid4java.message.Parameter;
1.30 +import org.openid4java.message.ParameterList;
1.31 +import org.openid4java.message.ax.AxMessage;
1.32 +import org.openid4java.message.ax.FetchRequest;
1.33 +import org.openid4java.message.ax.FetchResponse;
1.34 import org.openide.util.Exceptions;
1.35 import org.w3c.dom.Document;
1.36
1.37 @@ -83,10 +94,13 @@
1.38 }
1.39 version = p.getProperty("version", "unknown"); // NOI18N
1.40 }
1.41 + private static final Logger LOG = Logger.getLogger(UI.class.getName());
1.42 private static WebResource base;
1.43 private static WebResource stat;
1.44 private static WebResource web;
1.45 private static Requests requests;
1.46 + private static ConsumerManager manager;
1.47 + private static Map<String,DiscoveryInformation> ids = new HashMap<String, DiscoveryInformation>();
1.48
1.49 @Context
1.50 private HttpHeaders headers;
1.51 @@ -124,6 +138,85 @@
1.52 return viewable("login.fmt", null);
1.53 }
1.54
1.55 + @GET
1.56 + @Path("openid")
1.57 + @Produces(MediaType.TEXT_HTML)
1.58 + public Viewable openidResponse(
1.59 + @QueryParam("openid.assoc_handle") String handle,
1.60 + @QueryParam("openid.claimed_id") String claimedID,
1.61 + @QueryParam("openid.identity") String identity,
1.62 + @QueryParam("openid.mode") String mode /*id_res */,
1.63 + @QueryParam("openid.ns") String ns, /* http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0 */
1.64 + @QueryParam("openid.op_endpoint") String endpoint,
1.65 + @QueryParam("openid.response_nonce") String nonce,
1.66 + @QueryParam("openid.return_to") String returnTo,
1.67 + @QueryParam("openid.sig") String sig,
1.68 + @QueryParam("openid.signed") String signed
1.69 + ) throws Exception {
1.70 + if (signed == null) {
1.71 + return viewable("openid.fmt", null);
1.72 + }
1.73 +
1.74 + if (manager == null) {
1.75 + manager = new ConsumerManager();
1.76 + }
1.77 + ParameterList pl = new ParameterList();
1.78 + pl.set(new Parameter("openid.assoc_handle", handle));
1.79 + pl.set(new Parameter("openid.claimed_id", claimedID));
1.80 + pl.set(new Parameter("openid.identity", identity));
1.81 + pl.set(new Parameter("openid.mode", mode));
1.82 + pl.set(new Parameter("openid.ns", ns));
1.83 + pl.set(new Parameter("openid.op_endpoint", endpoint));
1.84 + pl.set(new Parameter("openid.response_nonce", nonce));
1.85 + pl.set(new Parameter("openid.return_to", returnTo));
1.86 + pl.set(new Parameter("openid.sig", sig));
1.87 + pl.set(new Parameter("openid.signed", signed));
1.88 +
1.89 + DiscoveryInformation info = ids.get(claimedID);
1.90 + VerificationResult res = manager.verify(returnTo, pl, info);
1.91 + String userId = res != null && res.getVerifiedId() != null ?
1.92 + res.getVerifiedId().getIdentifier() : null;
1.93 +
1.94 + if (res.getAuthResponse().hasExtension(AxMessage.OPENID_NS_AX)) {
1.95 + MessageExtension ext = res.getAuthResponse().getExtension(AxMessage.OPENID_NS_AX);
1.96 +
1.97 + if (ext instanceof FetchResponse) {
1.98 + FetchResponse fetchResp = (FetchResponse) ext;
1.99 +
1.100 + String firstName = fetchResp.getAttributeValue("FirstName");
1.101 + String lastName = fetchResp.getAttributeValue("LastName");
1.102 + String email = fetchResp.getAttributeValue("Email");
1.103 +
1.104 + LOG.log(Level.INFO, "First name: {0}", firstName);
1.105 + LOG.log(Level.INFO, "Last name: {0}", lastName);
1.106 + LOG.log(Level.INFO, "Email: {0}", email);
1.107 +
1.108 +
1.109 + }
1.110 + }
1.111 +
1.112 + return viewable("openid.fmt", null, "id", userId);
1.113 + }
1.114 + @POST
1.115 + @Path("openid")
1.116 + @Produces(MediaType.TEXT_HTML)
1.117 + public Viewable openid(@FormParam("openid_identifier") String openid) throws Exception {
1.118 + if (manager == null) {
1.119 + manager = new ConsumerManager();
1.120 + }
1.121 + List l = manager.discover(openid);
1.122 + DiscoveryInformation info = manager.associate(l);
1.123 + AuthRequest auth = manager.authenticate(info, web.path("openid").getURI().toString());
1.124 + FetchRequest fetch = FetchRequest.createFetchRequest();
1.125 + fetch.addAttribute("FirstName", "http://schema.openid.net/namePerson/first", true);
1.126 + fetch.addAttribute("LastName", "http://schema.openid.net/namePerson/last", true);
1.127 + fetch.addAttribute("Email", "http://schema.openid.net/contact/email", true);
1.128 + auth.addExtension(fetch);
1.129 + ids.put(info.getClaimedIdentifier().toString(), info);
1.130 +
1.131 + return viewable("openid.fmt", null, "url", auth.getDestinationUrl(true));
1.132 + }
1.133 +
1.134 @POST
1.135 @Path("login")
1.136 @Produces(MediaType.TEXT_HTML)