Initial support log via OpenID. Now I need to generate a login cookie and we'll be done
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 25 Nov 2010 23:08:20 +0100
changeset 2783a472605338f
parent 271 aa1c63b58149
child 279 cc6a819a1f8f
Initial support log via OpenID. Now I need to generate a login cookie and we'll be done
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties
freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/openid.fmt
     1.1 --- a/freemarkerdor/pom.xml	Sat Oct 23 22:51:43 2010 +0200
     1.2 +++ b/freemarkerdor/pom.xml	Thu Nov 25 23:08:20 2010 +0100
     1.3 @@ -86,6 +86,17 @@
     1.4        <version>${statisticsVersion}</version>
     1.5        <scope>test</scope>
     1.6      </dependency>
     1.7 +    <dependency>
     1.8 +      <groupId>org.openid4java</groupId>
     1.9 +      <artifactId>openid4java-server</artifactId>
    1.10 +      <version>0.9.5</version>
    1.11 +      <exclusions>
    1.12 +        <exclusion>
    1.13 +          <artifactId>xercesImpl</artifactId>
    1.14 +          <groupId>xerces</groupId>
    1.15 +        </exclusion>
    1.16 +      </exclusions>
    1.17 +    </dependency>
    1.18    </dependencies>
    1.19    <build>
    1.20      <plugins>
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sat Oct 23 22:51:43 2010 +0200
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Thu Nov 25 23:08:20 2010 +0100
     2.3 @@ -32,7 +32,6 @@
     2.4  import java.io.StringWriter;
     2.5  import java.net.URI;
     2.6  import java.text.MessageFormat;
     2.7 -import java.text.ParseException;
     2.8  import java.util.ArrayList;
     2.9  import java.util.Arrays;
    2.10  import java.util.Date;
    2.11 @@ -44,6 +43,8 @@
    2.12  import java.util.Properties;
    2.13  import java.util.ResourceBundle;
    2.14  import java.util.concurrent.Callable;
    2.15 +import java.util.logging.Level;
    2.16 +import java.util.logging.Logger;
    2.17  import javax.ws.rs.DefaultValue;
    2.18  import javax.ws.rs.FormParam;
    2.19  import javax.ws.rs.GET;
    2.20 @@ -61,6 +62,16 @@
    2.21  import javax.ws.rs.core.NewCookie;
    2.22  import javax.ws.rs.core.Response;
    2.23  import javax.ws.rs.core.Response.ResponseBuilder;
    2.24 +import org.openid4java.consumer.ConsumerManager;
    2.25 +import org.openid4java.consumer.VerificationResult;
    2.26 +import org.openid4java.discovery.DiscoveryInformation;
    2.27 +import org.openid4java.message.AuthRequest;
    2.28 +import org.openid4java.message.MessageExtension;
    2.29 +import org.openid4java.message.Parameter;
    2.30 +import org.openid4java.message.ParameterList;
    2.31 +import org.openid4java.message.ax.AxMessage;
    2.32 +import org.openid4java.message.ax.FetchRequest;
    2.33 +import org.openid4java.message.ax.FetchResponse;
    2.34  import org.openide.util.Exceptions;
    2.35  import org.w3c.dom.Document;
    2.36  
    2.37 @@ -83,10 +94,13 @@
    2.38          }
    2.39          version = p.getProperty("version", "unknown"); // NOI18N
    2.40      }
    2.41 +    private static final Logger LOG = Logger.getLogger(UI.class.getName());
    2.42      private static WebResource base;
    2.43      private static WebResource stat;
    2.44      private static WebResource web;
    2.45      private static Requests requests;
    2.46 +    private static ConsumerManager manager;
    2.47 +    private static Map<String,DiscoveryInformation> ids = new HashMap<String, DiscoveryInformation>();
    2.48  
    2.49      @Context
    2.50      private HttpHeaders headers;
    2.51 @@ -124,6 +138,85 @@
    2.52          return viewable("login.fmt", null);
    2.53      }
    2.54  
    2.55 +    @GET
    2.56 +    @Path("openid")
    2.57 +    @Produces(MediaType.TEXT_HTML)
    2.58 +    public Viewable openidResponse(
    2.59 +        @QueryParam("openid.assoc_handle") String handle,
    2.60 +        @QueryParam("openid.claimed_id") String claimedID,
    2.61 +        @QueryParam("openid.identity") String identity,
    2.62 +        @QueryParam("openid.mode") String mode /*id_res */,
    2.63 +        @QueryParam("openid.ns") String ns, /* http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0 */
    2.64 +        @QueryParam("openid.op_endpoint") String endpoint,
    2.65 +        @QueryParam("openid.response_nonce") String nonce,
    2.66 +        @QueryParam("openid.return_to") String returnTo,
    2.67 +        @QueryParam("openid.sig") String sig,
    2.68 +        @QueryParam("openid.signed") String signed
    2.69 +    ) throws Exception {
    2.70 +        if (signed == null) {
    2.71 +            return viewable("openid.fmt", null);
    2.72 +        }
    2.73 +        
    2.74 +        if (manager == null) {
    2.75 +            manager = new ConsumerManager(); 
    2.76 +        }
    2.77 +        ParameterList pl = new ParameterList();
    2.78 +        pl.set(new Parameter("openid.assoc_handle", handle));
    2.79 +        pl.set(new Parameter("openid.claimed_id", claimedID));
    2.80 +        pl.set(new Parameter("openid.identity", identity));
    2.81 +        pl.set(new Parameter("openid.mode", mode));
    2.82 +        pl.set(new Parameter("openid.ns", ns));
    2.83 +        pl.set(new Parameter("openid.op_endpoint", endpoint));
    2.84 +        pl.set(new Parameter("openid.response_nonce", nonce));
    2.85 +        pl.set(new Parameter("openid.return_to", returnTo));
    2.86 +        pl.set(new Parameter("openid.sig", sig));
    2.87 +        pl.set(new Parameter("openid.signed", signed));
    2.88 +        
    2.89 +        DiscoveryInformation info = ids.get(claimedID);
    2.90 +        VerificationResult res = manager.verify(returnTo, pl, info);
    2.91 +        String userId = res != null && res.getVerifiedId() != null ? 
    2.92 +            res.getVerifiedId().getIdentifier() : null;
    2.93 +        
    2.94 +        if (res.getAuthResponse().hasExtension(AxMessage.OPENID_NS_AX)) {
    2.95 +            MessageExtension ext = res.getAuthResponse().getExtension(AxMessage.OPENID_NS_AX);
    2.96 +
    2.97 +            if (ext instanceof FetchResponse) {
    2.98 +                FetchResponse fetchResp = (FetchResponse) ext;
    2.99 +
   2.100 +                String firstName = fetchResp.getAttributeValue("FirstName");
   2.101 +                String lastName = fetchResp.getAttributeValue("LastName");
   2.102 +                String email = fetchResp.getAttributeValue("Email");
   2.103 +                
   2.104 +                LOG.log(Level.INFO, "First name: {0}", firstName);
   2.105 +                LOG.log(Level.INFO, "Last name: {0}", lastName);
   2.106 +                LOG.log(Level.INFO, "Email: {0}", email);
   2.107 +                
   2.108 +                
   2.109 +            } 
   2.110 +        }        
   2.111 +        
   2.112 +        return viewable("openid.fmt", null, "id", userId);
   2.113 +    }
   2.114 +    @POST
   2.115 +    @Path("openid")
   2.116 +    @Produces(MediaType.TEXT_HTML)
   2.117 +    public Viewable openid(@FormParam("openid_identifier") String openid) throws Exception {
   2.118 +        if (manager == null) {
   2.119 +            manager = new ConsumerManager(); 
   2.120 +        }
   2.121 +        List l = manager.discover(openid);
   2.122 +        DiscoveryInformation info = manager.associate(l);
   2.123 +        AuthRequest auth = manager.authenticate(info, web.path("openid").getURI().toString());
   2.124 +        FetchRequest fetch = FetchRequest.createFetchRequest();
   2.125 +        fetch.addAttribute("FirstName", "http://schema.openid.net/namePerson/first", true);
   2.126 +        fetch.addAttribute("LastName", "http://schema.openid.net/namePerson/last", true);
   2.127 +        fetch.addAttribute("Email", "http://schema.openid.net/contact/email", true);
   2.128 +        auth.addExtension(fetch);        
   2.129 +        ids.put(info.getClaimedIdentifier().toString(), info);
   2.130 +        
   2.131 +        return viewable("openid.fmt", null, "url", auth.getDestinationUrl(true));
   2.132 +    }
   2.133 +    
   2.134      @POST
   2.135      @Path("login")
   2.136      @Produces(MediaType.TEXT_HTML)
     3.1 --- a/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties	Sat Oct 23 22:51:43 2010 +0200
     3.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/Bundle.properties	Thu Nov 25 23:08:20 2010 +0100
     3.3 @@ -85,6 +85,8 @@
     3.4  LOGIN=Login!
     3.5  logged=You are logged in as {0}.
     3.6  home=Start!
     3.7 +OPENID=Login via OpenID:
     3.8 +REDIRECT=Redirecting to OpenID provider...
     3.9  
    3.10  
    3.11  OPTIONS=Options
    3.12 @@ -92,7 +94,7 @@
    3.13  CHANGE_EMAIL=Update!
    3.14  LANGUAGE=Language:
    3.15  en=English
    3.16 -cs=\u010Cesky
    3.17 +cs=\u010cesky
    3.18  LOCALE=en
    3.19  CHANGE_LANGUAGE=Change!
    3.20  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/freemarkerdor/src/main/resources/cz/xelfi/quoridor/freemarkerdor/UI/openid.fmt	Thu Nov 25 23:08:20 2010 +0100
     4.3 @@ -0,0 +1,39 @@
     4.4 +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     4.5 +<html>
     4.6 +  <head>
     4.7 +    <title>${bundle.TITLE_PLAIN}</title>
     4.8 +    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     4.9 +    <#if url?? >
    4.10 +      <meta http-equiv="REFRESH" content="1;URL=${url}">
    4.11 +    </#if>
    4.12 +    <script type="text/javascript">
    4.13 +        function setFocus() {
    4.14 +            var name = document.getElementById("openid_identifier");
    4.15 +            name.focus();
    4.16 +        }
    4.17 +    </script>  </head>
    4.18 +  <body bgcolor="white" onload="setFocus();">
    4.19 +      <h1>${bundle.TITLE}</h1>
    4.20 +      <h2>${bundle.LOGIN}</h2>
    4.21 +
    4.22 +      <#if id?? >
    4.23 +        ${bundle("logged", id?string)}
    4.24 +        <a href="/">${bundle.home}</a>
    4.25 +      <#else>
    4.26 +
    4.27 +      <b>${message!""}</b>
    4.28 +      <#if url?? >
    4.29 +        <p>
    4.30 +         ${bundle.REDIRECT}
    4.31 +      <#else>
    4.32 +          <form action="/openid" method="post">
    4.33 +              <label for="openid_indentifier">${bundle.OPENID}</label> 
    4.34 +              <input type="text" name="openid_identifier" id="openid_identifier"/>
    4.35 +              <input type="submit" value="${bundle.LOGIN}"/>
    4.36 +          </form>
    4.37 +      </#if>
    4.38 +      </#if>
    4.39 +      <hr/>
    4.40 +      ${bundle("copyright", version)}
    4.41 +  </body>
    4.42 +</html>
    4.43 \ No newline at end of file