You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2011/11/02 16:15:39 UTC

svn commit: r1196619 - in /incubator/wookie/trunk: WebContent/WEB-INF/ etc/ddl-schema/ features/oauth/ modules/jcr/src/org/apache/wookie/beans/jcr/ scratchpad/widgets/oAuth/ scratchpad/widgets/oAuth/scripts/ scratchpad/widgets/oAuth/style/ src/META-INF...

Author: scottbw
Date: Wed Nov  2 15:15:39 2011
New Revision: 1196619

URL: http://svn.apache.org/viewvc?rev=1196619&view=rev
Log:
Added oAuth Feature (see WOOKIE-100) plus test widget. Thanks to Hoang Minh Tien for a fantastic patch.

Added:
    incubator/wookie/trunk/features/oauth/
    incubator/wookie/trunk/features/oauth/feature.xml
    incubator/wookie/trunk/features/oauth/oauth.js
    incubator/wookie/trunk/scratchpad/widgets/oAuth/
    incubator/wookie/trunk/scratchpad/widgets/oAuth/build.xml
    incubator/wookie/trunk/scratchpad/widgets/oAuth/config.xml
    incubator/wookie/trunk/scratchpad/widgets/oAuth/index.html
    incubator/wookie/trunk/scratchpad/widgets/oAuth/scripts/
    incubator/wookie/trunk/scratchpad/widgets/oAuth/scripts/main.js
    incubator/wookie/trunk/scratchpad/widgets/oAuth/style/
    incubator/wookie/trunk/scratchpad/widgets/oAuth/style/screen.css
    incubator/wookie/trunk/src/org/apache/wookie/beans/IOAuthToken.java
    incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/impl/OAuthTokenImpl.java
    incubator/wookie/trunk/src/org/apache/wookie/feature/oauth/
    incubator/wookie/trunk/src/org/apache/wookie/feature/oauth/oAuthClient.java
Modified:
    incubator/wookie/trunk/WebContent/WEB-INF/dwr.xml
    incubator/wookie/trunk/etc/ddl-schema/wookie-schema.xml
    incubator/wookie/trunk/modules/jcr/src/org/apache/wookie/beans/jcr/JCRPersistenceManager.java
    incubator/wookie/trunk/src/META-INF/persistence.xml
    incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/JPAPersistenceManager.java
    incubator/wookie/trunk/src/org/apache/wookie/beans/util/IPersistenceManager.java

Modified: incubator/wookie/trunk/WebContent/WEB-INF/dwr.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/WebContent/WEB-INF/dwr.xml?rev=1196619&r1=1196618&r2=1196619&view=diff
==============================================================================
--- incubator/wookie/trunk/WebContent/WEB-INF/dwr.xml (original)
+++ incubator/wookie/trunk/WebContent/WEB-INF/dwr.xml Wed Nov  2 15:15:39 2011
@@ -26,7 +26,14 @@
       <include method="getParticipants"/>
       <include method="getViewer"/>      
     </create>
-    
+
+    <create creator="new" javascript="OAuthConnector" scope="application">
+      <param name="class" value="org.apache.wookie.feature.oauth.oAuthClient"/>  
+      <include method="authenticate"/>
+      <include method="updateToken"/>
+      <include method="queryToken"/>
+    </create>
+        
     <convert converter="object" match="org.apache.wookie.ajaxmodel.impl.PreferenceDelegate" javascript="Preference">
         <param name="force" value="true"/>
         <param name="include" value="dkey, dvalue, readOnly"/>

Modified: incubator/wookie/trunk/etc/ddl-schema/wookie-schema.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/etc/ddl-schema/wookie-schema.xml?rev=1196619&r1=1196618&r2=1196619&view=diff
==============================================================================
--- incubator/wookie/trunk/etc/ddl-schema/wookie-schema.xml (original)
+++ incubator/wookie/trunk/etc/ddl-schema/wookie-schema.xml Wed Nov  2 15:15:39 2011
@@ -330,4 +330,19 @@
     </index>
   </table>
 
+  <table name="OAuthToken">
+    <column name="id" primaryKey="true" required="true" type="INTEGER"/>
+    <column name="jpa_version" type="INTEGER"/>
+    <column name="authzUrl" required="true" size="255" type="VARCHAR"/>
+    <column name="accessToken" required="true" size="255" type="VARCHAR"/>
+    <column name="clientId" required="true" size="255" type="VARCHAR"/>
+    <column name="expires" required="true" type="BIGINT"/>
+    <column name="widget_instance_id" type="INTEGER"/>
+    <foreign-key foreignTable="WidgetInstance" name="FKOAuthToken1">
+      <reference foreign="id" local="widget_instance_id"/>
+    </foreign-key>
+    <index name="IXOAuthToken1">
+      <index-column name="widget_instance_id"/>
+    </index>
+  </table>
 </database>

Added: incubator/wookie/trunk/features/oauth/feature.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/features/oauth/feature.xml?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/features/oauth/feature.xml (added)
+++ incubator/wookie/trunk/features/oauth/feature.xml Wed Nov  2 15:15:39 2011
@@ -0,0 +1,22 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<feature>
+	<name>http://oauth.net/2</name>
+	<script src="/wookie/dwr/interface/OAuthConnector.js"/>
+	<script src="oauth.js"/>
+</feature>
\ No newline at end of file

Added: incubator/wookie/trunk/features/oauth/oauth.js
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/features/oauth/oauth.js?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/features/oauth/oauth.js (added)
+++ incubator/wookie/trunk/features/oauth/oauth.js Wed Nov  2 15:15:39 2011
@@ -0,0 +1,74 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+oAuth = new function OAuth() {
+	this.access_token = null;
+	this.client_id = null;
+	this.status = null; // null: init, O: not being authenticated, F: authentication failed, A: authenticated
+	
+	this.init = function() {
+	
+		token_bunch = window.location.hash;
+		if (token_bunch.length > 0) {
+			OAuthConnector.updateToken(
+					widget.instanceid_key + token_bunch, 
+					{callback: function(result) {
+						if (result != "invalid") {
+							window.opener.location.reload();
+						}
+					}, async: false});
+			window.close();
+		}
+		
+		OAuthConnector.queryToken(widget.instanceid_key, {
+			callback: function(token_info) {
+				if (token_info != "invalid") {
+					oAuth.access_token = token_info;
+					oAuth.client_id = widget.instanceid_key;
+					oAuth.status = "A";
+				} else { 
+					oAuth.status = "O";			
+				}									
+			}, async:false});
+	}
+	
+	this.proxify = function(url) {
+		returnedUrl = widget.proxyUrl + "?instanceid_key=" + widget.instanceid_key + "&url=" + url;
+		if (oAuth.client_id != null && oAuth.access_token != null) {
+			returnedUrl = returnedUrl + "&client_id=" + oAuth.client_id + "&access_token=" + oAuth.access_token;
+		}
+		return returnedUrl;
+	}
+	
+	this.authenticate = function() {
+		OAuthConnector.authenticate(
+				widget.instanceid_key + "#" + window.location,
+				{callback: function(redirectUrl) {
+					window.open(redirectUrl, "authentication_popup", "width=500, height=400");
+				}, async: false});
+	}
+	
+	this.showStatus = function(container_id) {
+		if (oAuth.status == null || oAuth.status == "O") {
+			document.getElementById(container_id).innerHTML = "Not yet authenticated";
+		} else if (oAuth.status == "F") {
+			document.getElementById(container_id).innerHTML = "Authentication failed";
+		} else if (oAuth.status == "A") {
+			document.getElementById(container_id).innerHTML = "Authenticated";
+		}
+	}
+}
+
+oAuth.init();
+window.oauth = oAuth;
\ No newline at end of file

Modified: incubator/wookie/trunk/modules/jcr/src/org/apache/wookie/beans/jcr/JCRPersistenceManager.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/modules/jcr/src/org/apache/wookie/beans/jcr/JCRPersistenceManager.java?rev=1196619&r1=1196618&r2=1196619&view=diff
==============================================================================
--- incubator/wookie/trunk/modules/jcr/src/org/apache/wookie/beans/jcr/JCRPersistenceManager.java (original)
+++ incubator/wookie/trunk/modules/jcr/src/org/apache/wookie/beans/jcr/JCRPersistenceManager.java Wed Nov  2 15:15:39 2011
@@ -57,6 +57,7 @@ import org.apache.wookie.beans.IDescript
 import org.apache.wookie.beans.IFeature;
 import org.apache.wookie.beans.ILicense;
 import org.apache.wookie.beans.IName;
+import org.apache.wookie.beans.IOAuthToken;
 import org.apache.wookie.beans.IParam;
 import org.apache.wookie.beans.IParticipant;
 import org.apache.wookie.beans.IPreference;
@@ -1385,4 +1386,9 @@ public class JCRPersistenceManager imple
             return cache.remove(path);
         }
     }
+    
+    public IOAuthToken findOAuthToken(IWidgetInstance widgetInstance) {
+       // TODO Auto-generated method stub
+       return null;
+    }
 }

Added: incubator/wookie/trunk/scratchpad/widgets/oAuth/build.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/scratchpad/widgets/oAuth/build.xml?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/scratchpad/widgets/oAuth/build.xml (added)
+++ incubator/wookie/trunk/scratchpad/widgets/oAuth/build.xml Wed Nov  2 15:15:39 2011
@@ -0,0 +1,23 @@
+<?xml version="1.0"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<project default="build-widget" basedir="." name="widget build file">
+	<property name="wookie.widgets.dir" location="../"/>
+	<property name="widget.shortname" value="oAuth"/>
+	
+	<import file="../build.xml"/>
+</project>
\ No newline at end of file

Added: incubator/wookie/trunk/scratchpad/widgets/oAuth/config.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/scratchpad/widgets/oAuth/config.xml?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/scratchpad/widgets/oAuth/config.xml (added)
+++ incubator/wookie/trunk/scratchpad/widgets/oAuth/config.xml Wed Nov  2 15:15:39 2011
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<widget xmlns="http://www.w3.org/ns/widgets" id="http://infosec.fundp.ac.be/widgets/oAuth" version="0.1" width="230" height="300">  
+  <name>oAuth</name>  
+  <description>Sample widget accessing oAuth protected service</description>  
+  <content src="index.html"/>  
+  <icon src="images/icon.png"/>  
+  <author>FUNDP</author>  
+  <licence>Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.</licence>
+  <feature name="http://oauth.net/2" required="true">
+  	<param name="clientId" value="auto"/>
+  	<param name="authzServer" value="https://hogwarts.info.fundp.ac.be/google/implicit" />
+  </feature>
+  <access origin="https://hogwarts.info.fundp.ac.be:443"/>
+  <access origin="https://hogwarts.info.fundp.ac.be:80"/>
+</widget>

Added: incubator/wookie/trunk/scratchpad/widgets/oAuth/index.html
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/scratchpad/widgets/oAuth/index.html?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/scratchpad/widgets/oAuth/index.html (added)
+++ incubator/wookie/trunk/scratchpad/widgets/oAuth/index.html Wed Nov  2 15:15:39 2011
@@ -0,0 +1,53 @@
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+	<head>
+        <meta http-equiv="pragma" content="no-cache"/>
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+        <link rel="stylesheet" type="text/css" href="style/screen.css" />
+        <script type="text/javascript" src="scripts/main.js" charset="utf-8"></script>
+        <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
+		<title>oAuth</title>
+	</head>
+	<body>
+      <script type="text/javascript">
+      $(document).ready(function() {
+		window.oauth.showStatus("wookie-footer");
+      });
+      </script>	
+	  <div id="wookie-widget">
+		<div id="wookie-toolbar">Quotes</div>
+		<div id="wookie-content">
+			<div id="quoteBubble">Just click & see !!!</div>	
+			<div id="quotePortrait"></div>
+		</div>
+		<div id="wookie-settings">
+	      <p><input type="submit" class="wookie-form-button" value="Show quotes" onclick="showQuotes('funny')"/></p>
+	      <p><input type="submit" class="wookie-form-button" value="login" onclick="oAuth.authenticate()"/></p>
+	    </div>
+	    <div id="notices"></div>
+		<div id="wookie-footer"></div>
+	  </div>
+	  
+	  
+	</body>
+<!-- fix forIE cashing - must go here even though its in the wrong place
+<META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
+</HEAD>
+-->
+</html>
\ No newline at end of file

Added: incubator/wookie/trunk/scratchpad/widgets/oAuth/scripts/main.js
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/scratchpad/widgets/oAuth/scripts/main.js?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/scratchpad/widgets/oAuth/scripts/main.js (added)
+++ incubator/wookie/trunk/scratchpad/widgets/oAuth/scripts/main.js Wed Nov  2 15:15:39 2011
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+var reqNr = 0;
+var myXHR = new XMLHttpRequest();
+
+function showQuotes(quoteType) {
+	jsonrpc_url = 'https://hogwarts.info.fundp.ac.be/google/quotes';
+    if (window.oauth && typeof window.oauth.proxify == 'function'){
+        jsonrpc_url = window.oauth.proxify(jsonrpc_url);
+    }
+	var request = {jsonrpc: '2.0',
+		    method: 'getQuote',
+			id: 'req-'+(++reqNr),
+			params: {
+				quotes: quoteType
+			}
+		};
+	myXHR.open('POST', jsonrpc_url, true);
+	myXHR.setRequestHeader('Content-Type', 'application/json');
+	myXHR.onreadystatechange = handler;
+	myXHR.send(JSON.stringify(request));
+}
+
+function handler(evtXHR) {
+	if (myXHR.readyState == 4){
+		if (myXHR.status == 200) {
+			data = JSON.parse(myXHR.responseText);
+			if(data.quote == null){
+				notify('Widget instance is not authenticated, Please login');
+			} else {
+				notify('');
+				document.getElementById('quoteBubble').innerHTML = '<a>' + data.quote + '</a>';
+				document.getElementById('quotePortrait').innerHTML = '<img src="' + data.portrait + '">' + data.authorName + '</img>';
+			}
+		} else {
+			notify("Invocation Errors Occured. Status: " + myXHR.status);
+		}
+	}
+}
+
+function notify(message){
+	if (window.widget && widget.showNotification){
+		widget.showNotification(message, function(){widget.show()});
+	} else {
+		document.getElementById('notices').innerHTML = "<p>"+message+"</p>";
+	}
+}
\ No newline at end of file

Added: incubator/wookie/trunk/scratchpad/widgets/oAuth/style/screen.css
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/scratchpad/widgets/oAuth/style/screen.css?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/scratchpad/widgets/oAuth/style/screen.css (added)
+++ incubator/wookie/trunk/scratchpad/widgets/oAuth/style/screen.css Wed Nov  2 15:15:39 2011
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+html, body {
+  height:100%; 
+  max-height:100%; 
+  padding:0; 
+  margin:0; 
+  border:0; 
+  font-family: "trebuchet ms", tahoma, verdana, arial, sans-serif;
+  /* hide overflow:hidden from IE5/Mac */ 
+  /* \*/ 
+  overflow: hidden; 
+  /* */ 
+}
+
+div#quoteBubble {
+    background-repeat: no-repeat;
+    display: block;
+    height: 80px;
+    margin: 0 auto;
+}
+
+div#quoteBubble a {
+    color: blue;
+    display: block;
+    font-size: 12px;
+    line-height: 16px;
+    padding: 8px 7px 0 11px;
+}
+
+#wookie-widget {
+	min-height:100%;
+    position:relative;
+}
+
+#wookie-toolbar {
+	background-color:#C0C0C0;
+    text-align:center;
+    font-size:75%;
+    height:16px;
+}
+
+#wookie-widget-icon {
+	float:left;
+}
+
+#wookie-content {
+	padding-top:5px;
+    padding-bottom:1em;   /* Height of the footer */
+    background-color:#FFFFFF
+}
+
+#wookie-settings {
+	display:block;
+}
+
+#wookie-footer {
+	background-color:#C0C0C0;
+	position:absolute;
+    bottom:0;
+    width:100%;
+    font-size:75%;
+    text-align:center;
+    height:1.5em;   /* Height of the footer */
+}
+
+input.wookie-form-button {
+   font-size:9px;
+   font-family:Verdana,sans-serif;
+   font-weight:bold;
+   color:#fcfcfc;
+   background-color:#000000;
+   border-style:solid;
+   border-color:#cccccc;
+   border-width:1px;
+}
\ No newline at end of file

Modified: incubator/wookie/trunk/src/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/META-INF/persistence.xml?rev=1196619&r1=1196618&r2=1196619&view=diff
==============================================================================
--- incubator/wookie/trunk/src/META-INF/persistence.xml (original)
+++ incubator/wookie/trunk/src/META-INF/persistence.xml Wed Nov  2 15:15:39 2011
@@ -42,6 +42,7 @@
         <class>org.apache.wookie.beans.jpa.impl.WidgetInstanceImpl</class>
         <class>org.apache.wookie.beans.jpa.impl.WidgetServiceImpl</class>
         <class>org.apache.wookie.beans.jpa.impl.WidgetTypeImpl</class>
+        <class>org.apache.wookie.beans.jpa.impl.OAuthTokenImpl</class>
 
         <exclude-unlisted-classes>true</exclude-unlisted-classes>
 

Added: incubator/wookie/trunk/src/org/apache/wookie/beans/IOAuthToken.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/beans/IOAuthToken.java?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/beans/IOAuthToken.java (added)
+++ incubator/wookie/trunk/src/org/apache/wookie/beans/IOAuthToken.java Wed Nov  2 15:15:39 2011
@@ -0,0 +1,39 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.wookie.beans;
+
+public interface IOAuthToken extends IBean {
+
+  String getAuthzUrl();
+
+  void setAuthzUrl(String authzUrl);
+
+  String getAccessToken();
+
+  void setAccessToken(String accessToken);
+
+  public String getClientId();
+
+  public void setClientId(String clientId);
+
+  public long getExpires();
+
+  public void setExpires(long expires);
+
+  public boolean isExpires();
+
+  public IWidgetInstance getWidgetInstance();
+
+  public void setWidgetInstance(IWidgetInstance widgetInstance);
+}

Modified: incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/JPAPersistenceManager.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/JPAPersistenceManager.java?rev=1196619&r1=1196618&r2=1196619&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/JPAPersistenceManager.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/JPAPersistenceManager.java Wed Nov  2 15:15:39 2011
@@ -55,6 +55,7 @@ import org.apache.wookie.beans.IDescript
 import org.apache.wookie.beans.IFeature;
 import org.apache.wookie.beans.ILicense;
 import org.apache.wookie.beans.IName;
+import org.apache.wookie.beans.IOAuthToken;
 import org.apache.wookie.beans.IParam;
 import org.apache.wookie.beans.IParticipant;
 import org.apache.wookie.beans.IPreference;
@@ -76,6 +77,7 @@ import org.apache.wookie.beans.jpa.impl.
 import org.apache.wookie.beans.jpa.impl.FeatureImpl;
 import org.apache.wookie.beans.jpa.impl.LicenseImpl;
 import org.apache.wookie.beans.jpa.impl.NameImpl;
+import org.apache.wookie.beans.jpa.impl.OAuthTokenImpl;
 import org.apache.wookie.beans.jpa.impl.ParamImpl;
 import org.apache.wookie.beans.jpa.impl.ParticipantImpl;
 import org.apache.wookie.beans.jpa.impl.PreferenceDefaultImpl;
@@ -138,6 +140,7 @@ public class JPAPersistenceManager imple
         INTERFACE_TO_CLASS_MAP.put(IWidgetInstance.class, WidgetInstanceImpl.class);
         INTERFACE_TO_CLASS_MAP.put(IWidgetService.class, WidgetServiceImpl.class);
         INTERFACE_TO_CLASS_MAP.put(IWidgetType.class, WidgetTypeImpl.class);
+        INTERFACE_TO_CLASS_MAP.put(IOAuthToken.class, OAuthTokenImpl.class);
 
         BEAN_INTERFACE_TO_CLASS_MAP.put(IAccessRequest.class, AccessRequestImpl.class);
         BEAN_INTERFACE_TO_CLASS_MAP.put(IApiKey.class, ApiKeyImpl.class);
@@ -149,6 +152,7 @@ public class JPAPersistenceManager imple
         BEAN_INTERFACE_TO_CLASS_MAP.put(IWidgetDefault.class, WidgetDefaultImpl.class);
         BEAN_INTERFACE_TO_CLASS_MAP.put(IWidgetInstance.class, WidgetInstanceImpl.class);
         BEAN_INTERFACE_TO_CLASS_MAP.put(IWidgetService.class, WidgetServiceImpl.class);
+        BEAN_INTERFACE_TO_CLASS_MAP.put(IOAuthToken.class, OAuthTokenImpl.class);
 
         BEAN_INTERFACE_TO_ID_FIELD_TYPE_MAP.put(IAccessRequest.class, Integer.class);
         BEAN_INTERFACE_TO_ID_FIELD_TYPE_MAP.put(IApiKey.class, Integer.class);
@@ -158,6 +162,7 @@ public class JPAPersistenceManager imple
         BEAN_INTERFACE_TO_ID_FIELD_TYPE_MAP.put(IWidgetDefault.class, String.class);
         BEAN_INTERFACE_TO_ID_FIELD_TYPE_MAP.put(IWidgetInstance.class, Integer.class);
         BEAN_INTERFACE_TO_ID_FIELD_TYPE_MAP.put(IWidgetService.class, Integer.class);
+        BEAN_INTERFACE_TO_ID_FIELD_TYPE_MAP.put(IOAuthToken.class, Integer.class);
         
         DB_TYPE_TO_JPA_DICTIONARY_MAP.put("db2", "db2");
         DB_TYPE_TO_JPA_DICTIONARY_MAP.put("derby", "derby");
@@ -926,4 +931,21 @@ public class JPAPersistenceManager imple
             return false;
         }
     }
+
+	public IOAuthToken findOAuthToken(IWidgetInstance widgetInstance) {
+
+        if (entityManager == null) {
+            throw new IllegalStateException("Transaction not initiated or already closed");
+        }        
+
+        if (widgetInstance != null) {
+            try {
+                Query query = entityManager.createNamedQuery("ACCESS_TOKEN");
+                query.setParameter("widgetInstance", widgetInstance);
+                return (IOAuthToken) query.getSingleResult();
+            } catch (Exception e) {
+            }
+        }
+        return null;
+	}
 }

Added: incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/impl/OAuthTokenImpl.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/impl/OAuthTokenImpl.java?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/impl/OAuthTokenImpl.java (added)
+++ incubator/wookie/trunk/src/org/apache/wookie/beans/jpa/impl/OAuthTokenImpl.java Wed Nov  2 15:15:39 2011
@@ -0,0 +1,121 @@
+/*
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.wookie.beans.jpa.impl;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+import javax.persistence.Version;
+
+import org.apache.wookie.beans.IOAuthToken;
+import org.apache.wookie.beans.IWidgetInstance;
+
+
+@Entity(name="OAuthToken")
+@Table(name="OAuthToken")
+@NamedQueries({@NamedQuery(name="ACCESS_TOKEN", query="SELECT o FROM OAuthToken o WHERE o.widgetInstance = :widgetInstance")})
+public class OAuthTokenImpl implements IOAuthToken {
+
+    @Id
+    @GeneratedValue(strategy=GenerationType.AUTO)
+    @Column(name="id", nullable=false)
+    private int id;
+
+    @Version
+    @Column(name="jpa_version")
+    @SuppressWarnings("unused")
+    private int jpaVersion;
+    
+    @Basic(optional=false)
+    @Column(name="authzUrl", nullable=true)
+    private String authzUrl;
+
+    @Basic(optional=false)
+    @Column(name="accessToken", nullable=false)
+    private String accessToken;
+    
+    @Basic(optional=false)
+    @Column(name="expires", nullable=false)
+    private long expires;
+    
+    @Basic(optional=false)
+    @Column(name="clientId", nullable=false)
+    private String clientId;
+	
+    @ManyToOne(fetch=FetchType.LAZY, optional=false)
+    @JoinColumn(name="widget_instance_id", referencedColumnName="id")
+    private WidgetInstanceImpl widgetInstance;
+    
+	public String getAuthzUrl() {
+		return authzUrl;
+	}
+
+	public void setAuthzUrl(String authzUrl) {
+		this.authzUrl = authzUrl;
+	}
+
+	public String getAccessToken() {
+		return accessToken;
+	}
+
+	public void setAccessToken(String accessToken) {
+		this.accessToken = accessToken;
+	}
+
+	public String getClientId() {
+		return clientId;
+	}
+
+	public void setClientId(String clientId) {
+		this.clientId = clientId;
+	}
+
+	public long getExpires() {
+		return expires;
+	}
+
+	public void setExpires(long expires) {
+		this.expires = expires;
+	}
+	
+	public boolean isExpires() {
+		return System.currentTimeMillis() > expires;
+	}
+
+	public Object getId() {
+		return id;
+	}
+
+	public IWidgetInstance getWidgetInstance() {
+		return widgetInstance;
+	}
+
+	public void setWidgetInstance(IWidgetInstance widgetInstance) {
+		this.widgetInstance = (WidgetInstanceImpl) widgetInstance;
+	}
+/*
+	public void updateInverseRelationship(WidgetInstanceImpl owningObject) {
+		this.widgetInstance = owningObject;
+	}
+*/
+}

Modified: incubator/wookie/trunk/src/org/apache/wookie/beans/util/IPersistenceManager.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/beans/util/IPersistenceManager.java?rev=1196619&r1=1196618&r2=1196619&view=diff
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/beans/util/IPersistenceManager.java (original)
+++ incubator/wookie/trunk/src/org/apache/wookie/beans/util/IPersistenceManager.java Wed Nov  2 15:15:39 2011
@@ -18,6 +18,7 @@ import java.util.Map;
 
 import org.apache.wookie.beans.IAccessRequest;
 import org.apache.wookie.beans.IBean;
+import org.apache.wookie.beans.IOAuthToken;
 import org.apache.wookie.beans.IWidget;
 import org.apache.wookie.beans.IWidgetInstance;
 
@@ -203,5 +204,12 @@ public interface IPersistenceManager
      * @return retrieved matching IAccessRequest beans array or empty array if none found
      */
     IAccessRequest [] findApplicableAccessRequests(IWidget widget);
+    
+    /**
+     * Find oAuth token for widget instance
+     * @param widgetInstance
+     * @return
+     */
+    IOAuthToken findOAuthToken(IWidgetInstance widgetInstance);
  
 }

Added: incubator/wookie/trunk/src/org/apache/wookie/feature/oauth/oAuthClient.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/feature/oauth/oAuthClient.java?rev=1196619&view=auto
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/feature/oauth/oAuthClient.java (added)
+++ incubator/wookie/trunk/src/org/apache/wookie/feature/oauth/oAuthClient.java Wed Nov  2 15:15:39 2011
@@ -0,0 +1,186 @@
+/*
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * 
+ */
+package org.apache.wookie.feature.oauth;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.wookie.Messages;
+import org.apache.wookie.beans.IOAuthToken;
+import org.apache.wookie.beans.IParam;
+import org.apache.wookie.beans.IStartFile;
+import org.apache.wookie.beans.IWidgetInstance;
+import org.apache.wookie.beans.util.IPersistenceManager;
+import org.apache.wookie.beans.util.PersistenceManagerFactory;
+import org.apache.wookie.server.LocaleHandler;
+import org.directwebremoting.WebContextFactory;
+
+public class oAuthClient {
+
+  public String getName() {
+		return "http://oauth.net/2";
+	}
+	
+	public String authenticate(String idKey_RedirectUri) {
+		int iPos = idKey_RedirectUri.indexOf('#');
+		String idKey = idKey_RedirectUri.substring(0, iPos);
+		String redirectUri = idKey_RedirectUri.substring(iPos + 1);
+		if(idKey == null) return "invalid";		
+		IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
+		IWidgetInstance widgetInstance = persistenceManager.findWidgetInstanceByIdKey(idKey);
+		if(widgetInstance==null) return "invalid";
+		
+		Collection<IStartFile> startFiles = widgetInstance.getWidget().getStartFiles();
+		String startFileUrl = null;
+		for(IStartFile startFile : startFiles) {
+			iPos = redirectUri.indexOf(startFile.getUrl());
+			if (iPos > -1) {
+				startFileUrl = startFile.getUrl();
+				break;
+			}
+		}
+		if (startFileUrl != null) {
+			redirectUri = redirectUri.substring(0, iPos + startFileUrl.length()) + "?idkey=" + idKey;
+		}
+		
+		try {
+			redirectUri = URLEncoder.encode(redirectUri, "UTF-8");
+		} catch (UnsupportedEncodingException e) {
+		}
+		
+		Collection<org.apache.wookie.beans.IFeature> widgetFeatures = widgetInstance.getWidget().getFeatures();
+		org.apache.wookie.beans.IFeature oAuthFeature = null;
+		for (org.apache.wookie.beans.IFeature aFeature : widgetFeatures) {
+			if (getName().equals(aFeature.getFeatureName())) {
+				oAuthFeature = aFeature;
+				break;
+			}
+		}
+		
+		if (oAuthFeature == null) {
+			return "";
+		}
+		
+		Collection<IParam> oAuthParams = oAuthFeature.getParameters();
+		String clientId = idKey;
+		String authzServer = null;
+		for (IParam aParam : oAuthParams) {
+			if ("authzServer".equals(aParam.getParameterName())) {
+				authzServer = aParam.getParameterValue();
+			}
+		}
+		
+		IOAuthToken oauthToken = persistenceManager.findOAuthToken(widgetInstance);
+		if (oauthToken != null) {
+			persistenceManager.delete(oauthToken);
+		}
+		
+		String url = authzServer + "?client_id=" + clientId + "&response_type=code&redirect_uri=" + redirectUri; 
+		
+		return url;
+	}
+	
+	public String queryToken(String idKey) {
+		if(idKey == null) return "invalid";
+		IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
+		IWidgetInstance widgetInstance = persistenceManager.findWidgetInstanceByIdKey(idKey);
+		if(widgetInstance==null) return "invalid";
+		
+		IOAuthToken oauthToken = persistenceManager.findOAuthToken(widgetInstance);
+		if (oauthToken != null) {
+			if (!oauthToken.isExpires())
+				return oauthToken.getAccessToken() ;
+		}
+		return "invalid";
+	}
+		
+	public String updateToken(String idKey_tokenBunch) {
+		int iPos = idKey_tokenBunch.indexOf('#');
+		String idKey = idKey_tokenBunch.substring(0, iPos);
+		String tokenBunch = idKey_tokenBunch.substring(iPos + 1);
+		
+		Map<String,String> oAuthTokenBunch = new HashMap<String, String>();
+		iPos = 0;
+		int iEqual, iOffset = 0;
+		String fragment = tokenBunch;
+		do {
+			iPos = tokenBunch.indexOf('&', iOffset);
+			if (iPos < 0) {
+				iPos = tokenBunch.length();
+			}
+			
+			fragment = tokenBunch.substring(iOffset, iPos);
+			iOffset = iOffset + iPos + 1;
+			iEqual = fragment.indexOf('=');
+			if (iEqual < 0) continue;
+			oAuthTokenBunch.put(fragment.substring(0, iEqual), fragment.substring(iEqual + 1));
+		} while (iOffset < tokenBunch.length());
+		
+		IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
+		IWidgetInstance widgetInstance = persistenceManager.findWidgetInstanceByIdKey(idKey);
+		HttpServletRequest request = WebContextFactory.get().getHttpServletRequest();
+		Messages localizedMessages = LocaleHandler.localizeMessages(request);		
+
+		if(widgetInstance==null) {
+			return localizedMessages.getString("WidgetAPIImpl.0"); //$NON-NLS-1$
+		}
+
+		Map<String, String> oAuthParams = queryOAuthParams(idKey);
+		if (oAuthParams == null) {
+			return localizedMessages.getString("WidgetAPIImpl.0"); //$NON-NLS-1$			
+		}
+		
+		IOAuthToken oauthToken = persistenceManager.findOAuthToken(widgetInstance);
+		if (oauthToken == null) oauthToken = persistenceManager.newInstance(IOAuthToken.class);
+		oauthToken.setAccessToken(oAuthTokenBunch.get("access_token"));
+		oauthToken.setExpires(System.currentTimeMillis() + 1000 * Integer.parseInt(oAuthTokenBunch.get("expires_in")));
+		oauthToken.setClientId(oAuthParams.get("clientId"));
+		oauthToken.setAuthzUrl(oAuthParams.get("authzServer"));
+		oauthToken.setWidgetInstance(widgetInstance);
+		persistenceManager.save(oauthToken);
+		return oauthToken.getAccessToken();
+	}
+	
+	private Map<String, String> queryOAuthParams(String idKey) {
+		IPersistenceManager persistenceManager = PersistenceManagerFactory.getPersistenceManager();
+		IWidgetInstance widgetInstance = persistenceManager.findWidgetInstanceByIdKey(idKey);
+		if(widgetInstance==null) return null;
+		
+		Collection<org.apache.wookie.beans.IFeature> widgetFeatures = widgetInstance.getWidget().getFeatures();
+		org.apache.wookie.beans.IFeature oAuthFeature = null;
+		for (org.apache.wookie.beans.IFeature aFeature : widgetFeatures) {
+			if (getName().equals(aFeature.getFeatureName())) {
+				oAuthFeature = aFeature;
+				break;
+			}
+		}
+		if (oAuthFeature == null) return null;
+		
+		Collection<IParam> oAuthParams = oAuthFeature.getParameters();
+		Map<String, String> oAuthParamMap = new HashMap<String, String>();
+		for (IParam aParam : oAuthParams) {
+			oAuthParamMap.put(aParam.getParameterName(), aParam.getParameterValue());
+		}
+		return oAuthParamMap;
+	}
+
+}
\ No newline at end of file