# HG changeset patch # User Jaroslav Tulach # Date 1292684496 -3600 # Node ID cc6a819a1f8ffb71611257cc681ac733c0e7ad53 # Parent 02542f49f3270522d9326320b0ccd6d2fe5d8756# Parent 3a472605338ff396d74293e34503a8149c13b76f Merging in support for OpenID diff -r 02542f49f327 -r cc6a819a1f8f freemarkerdor/pom.xml --- a/freemarkerdor/pom.xml Sun Nov 28 09:38:14 2010 +0100 +++ b/freemarkerdor/pom.xml Sat Dec 18 16:01:36 2010 +0100 @@ -86,6 +86,17 @@ ${statisticsVersion} test + + org.openid4java + openid4java-server + 0.9.5 + + + xercesImpl + xerces + + + diff -r 02542f49f327 -r cc6a819a1f8f freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sun Nov 28 09:38:14 2010 +0100 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Sat Dec 18 16:01:36 2010 +0100 @@ -32,7 +32,6 @@ import java.io.StringWriter; import java.net.URI; import java.text.MessageFormat; -import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; @@ -44,6 +43,8 @@ import java.util.Properties; import java.util.ResourceBundle; import java.util.concurrent.Callable; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.ws.rs.DefaultValue; import javax.ws.rs.FormParam; import javax.ws.rs.GET; @@ -61,6 +62,16 @@ import javax.ws.rs.core.NewCookie; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.ResponseBuilder; +import org.openid4java.consumer.ConsumerManager; +import org.openid4java.consumer.VerificationResult; +import org.openid4java.discovery.DiscoveryInformation; +import org.openid4java.message.AuthRequest; +import org.openid4java.message.MessageExtension; +import org.openid4java.message.Parameter; +import org.openid4java.message.ParameterList; +import org.openid4java.message.ax.AxMessage; +import org.openid4java.message.ax.FetchRequest; +import org.openid4java.message.ax.FetchResponse; import org.openide.util.Exceptions; import org.w3c.dom.Document; @@ -83,10 +94,13 @@ } version = p.getProperty("version", "unknown"); // NOI18N } + private static final Logger LOG = Logger.getLogger(UI.class.getName()); private static WebResource base; private static WebResource stat; private static WebResource web; private static Requests requests; + private static ConsumerManager manager; + private static Map ids = new HashMap(); @Context private HttpHeaders headers; @@ -124,6 +138,85 @@ return viewable("login.fmt", null); } + @GET + @Path("openid") + @Produces(MediaType.TEXT_HTML) + public Viewable openidResponse( + @QueryParam("openid.assoc_handle") String handle, + @QueryParam("openid.claimed_id") String claimedID, + @QueryParam("openid.identity") String identity, + @QueryParam("openid.mode") String mode /*id_res */, + @QueryParam("openid.ns") String ns, /* http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0 */ + @QueryParam("openid.op_endpoint") String endpoint, + @QueryParam("openid.response_nonce") String nonce, + @QueryParam("openid.return_to") String returnTo, + @QueryParam("openid.sig") String sig, + @QueryParam("openid.signed") String signed + ) throws Exception { + if (signed == null) { + return viewable("openid.fmt", null); + } + + if (manager == null) { + manager = new ConsumerManager(); + } + ParameterList pl = new ParameterList(); + pl.set(new Parameter("openid.assoc_handle", handle)); + pl.set(new Parameter("openid.claimed_id", claimedID)); + pl.set(new Parameter("openid.identity", identity)); + pl.set(new Parameter("openid.mode", mode)); + pl.set(new Parameter("openid.ns", ns)); + pl.set(new Parameter("openid.op_endpoint", endpoint)); + pl.set(new Parameter("openid.response_nonce", nonce)); + pl.set(new Parameter("openid.return_to", returnTo)); + pl.set(new Parameter("openid.sig", sig)); + pl.set(new Parameter("openid.signed", signed)); + + DiscoveryInformation info = ids.get(claimedID); + VerificationResult res = manager.verify(returnTo, pl, info); + String userId = res != null && res.getVerifiedId() != null ? + res.getVerifiedId().getIdentifier() : null; + + if (res.getAuthResponse().hasExtension(AxMessage.OPENID_NS_AX)) { + MessageExtension ext = res.getAuthResponse().getExtension(AxMessage.OPENID_NS_AX); + + if (ext instanceof FetchResponse) { + FetchResponse fetchResp = (FetchResponse) ext; + + String firstName = fetchResp.getAttributeValue("FirstName"); + String lastName = fetchResp.getAttributeValue("LastName"); + String email = fetchResp.getAttributeValue("Email"); + + LOG.log(Level.INFO, "First name: {0}", firstName); + LOG.log(Level.INFO, "Last name: {0}", lastName); + LOG.log(Level.INFO, "Email: {0}", email); + + + } + } + + return viewable("openid.fmt", null, "id", userId); + } + @POST + @Path("openid") + @Produces(MediaType.TEXT_HTML) + public Viewable openid(@FormParam("openid_identifier") String openid) throws Exception { + if (manager == null) { + manager = new ConsumerManager(); + } + List l = manager.discover(openid); + DiscoveryInformation info = manager.associate(l); + AuthRequest auth = manager.authenticate(info, web.path("openid").getURI().toString()); + FetchRequest fetch = FetchRequest.createFetchRequest(); + fetch.addAttribute("FirstName", "http://schema.openid.net/namePerson/first", true); + fetch.addAttribute("LastName", "http://schema.openid.net/namePerson/last", true); + fetch.addAttribute("Email", "http://schema.openid.net/contact/email", true); + auth.addExtension(fetch); + ids.put(info.getClaimedIdentifier().toString(), info); + + return viewable("openid.fmt", null, "url", auth.getDestinationUrl(true)); + } + @POST @Path("login") @Produces(MediaType.TEXT_HTML) diff -r 02542f49f327 -r cc6a819a1f8f freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties Sun Nov 28 09:38:14 2010 +0100 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties Sat Dec 18 16:01:36 2010 +0100 @@ -85,6 +85,8 @@ LOGIN=Login! logged=You are logged in as {0}. home=Start! +OPENID=Login via OpenID: +REDIRECT=Redirecting to OpenID provider... OPTIONS=Options @@ -92,7 +94,7 @@ CHANGE_EMAIL=Update! LANGUAGE=Language: en=English -cs=\u010Cesky +cs=\u010cesky LOCALE=en CHANGE_LANGUAGE=Change! diff -r 02542f49f327 -r cc6a819a1f8f freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/openid.fmt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/openid.fmt Sat Dec 18 16:01:36 2010 +0100 @@ -0,0 +1,39 @@ + + + + ${bundle.TITLE_PLAIN} + + <#if url?? > + + + + +

${bundle.TITLE}

+

${bundle.LOGIN}

+ + <#if id?? > + ${bundle("logged", id?string)} + ${bundle.home} + <#else> + + ${message!""} + <#if url?? > +

+ ${bundle.REDIRECT} + <#else> +

+ + + +
+ + +
+ ${bundle("copyright", version)} + + \ No newline at end of file