Skip to content

Commit ed8d2fb

Browse files
committed
Separate out OAuth support from Yammer dashlet. New project Share OAuth now provides common functionality.
git-svn-id: https://share-extras.googlecode.com/svn/trunk/Share OAuth@631 a3f5c567-fd0f-3a89-9b71-a290c5a5f590
1 parent 97d1afd commit ed8d2fb

File tree

9 files changed

+1289
-0
lines changed

9 files changed

+1289
-0
lines changed

.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Share OAuth</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>

README.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
OAuth Support for Alfresco Share
2+
================================
3+
4+
Author: Will Abson
5+
6+
This project does not provide any user functions directly, but is intended
7+
as a reusable module that can be used by any other add-ons which require
8+
OAuth support within the Share user interface.
9+
10+
Installation
11+
------------
12+
13+
The add-on has been developed to install on top of an existing Alfresco
14+
3.3 or 3.4 installation.
15+
16+
An Ant build script is provided to build a JAR file containing the
17+
custom files, which can then be installed into the 'tomcat/shared/lib' folder
18+
of your Alfresco installation.
19+
20+
To build the JAR file, run the following command from the base project
21+
directory.
22+
23+
ant clean dist-jar
24+
25+
The command should build a JAR file named share-oauth.jar
26+
in the 'build/dist' directory within your project.
27+
28+
To deploy the dashlet files into a local Tomcat instance for testing, you can
29+
use the hotcopy-tomcat-jar task. You will need to set the tomcat.home
30+
property in Ant.
31+
32+
ant -Dtomcat.home=C:/Alfresco/tomcat clean hotcopy-tomcat-jar
33+
34+
Once you have run this you will need to restart Tomcat so that the classpath
35+
resources in the JAR file are picked up.

build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jar.name=share-oauth.jar

build.xml

Lines changed: 444 additions & 0 deletions
Large diffs are not rendered by default.

lib/ml-ant-http-1.1.1.jar

44.1 KB
Binary file not shown.
8.08 KB
Binary file not shown.

lib/yuicompressor-2.4.2.jar

831 KB
Binary file not shown.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/**
2+
* Copyright (C) 20010-2011 Share Extras contributors.
3+
*
4+
* This file is part of the Share Extras project.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.sharextras.webscripts.connector;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
import java.util.regex.Matcher;
24+
import java.util.regex.Pattern;
25+
26+
import javax.servlet.http.HttpServletRequest;
27+
import javax.servlet.http.HttpServletResponse;
28+
29+
import org.apache.commons.logging.Log;
30+
import org.apache.commons.logging.LogFactory;
31+
import org.springframework.extensions.config.RemoteConfigElement.ConnectorDescriptor;
32+
import org.springframework.extensions.webscripts.connector.ConnectorContext;
33+
import org.springframework.extensions.webscripts.connector.EndpointManager;
34+
import org.springframework.extensions.webscripts.connector.HttpConnector;
35+
import org.springframework.extensions.webscripts.connector.HttpOAuthConnector;
36+
import org.springframework.extensions.webscripts.connector.RemoteClient;
37+
import org.springframework.extensions.webscripts.connector.Response;
38+
import org.springframework.extensions.webscripts.connector.ResponseStatus;
39+
40+
public class HttpOAuthConnector extends HttpConnector
41+
{
42+
public static final String PARAM_CONSUMER_KEY = "consumer-key";
43+
public static final String PARAM_CONSUMER_SECRET = "consumer-secret";
44+
public static final String PARAM_SIGNATURE_METHOD = "signature-method";
45+
46+
public static final String SIGNATURE_METHOD_PLAINTEXT = "PLAINTEXT";
47+
48+
private static Log logger = LogFactory.getLog(HttpOAuthConnector.class);
49+
50+
public HttpOAuthConnector(ConnectorDescriptor descriptor,
51+
String endpoint) {
52+
super(descriptor, endpoint);
53+
}
54+
55+
private String getConsumerKey()
56+
{
57+
return descriptor.getStringProperty(PARAM_CONSUMER_KEY);
58+
}
59+
60+
private String getConsumerSecret()
61+
{
62+
return descriptor.getStringProperty(PARAM_CONSUMER_SECRET);
63+
}
64+
65+
private String getSignatureMethod()
66+
{
67+
return descriptor.getStringProperty(PARAM_SIGNATURE_METHOD);
68+
}
69+
70+
private String generateSignature(Map<String, String> oauthParams)
71+
{
72+
if (getSignatureMethod().equals(SIGNATURE_METHOD_PLAINTEXT))
73+
{
74+
StringBuffer signatureBuffer = new StringBuffer(getConsumerSecret()).append("%26");
75+
String tokenSecret = oauthParams.get("oauth_token_secret");
76+
if (tokenSecret != null && !tokenSecret.equals(""))
77+
{
78+
signatureBuffer.append(tokenSecret);
79+
}
80+
return signatureBuffer.toString();
81+
}
82+
else
83+
{
84+
// TODO do we need to throw an exception?
85+
return null;
86+
}
87+
}
88+
89+
public Response call(String uri, ConnectorContext context, HttpServletRequest req, HttpServletResponse res)
90+
{
91+
if (logger.isDebugEnabled())
92+
logger.debug("Requested Method: " + (context != null ? context.getMethod() : "GET") + " URI: " + uri);
93+
Response response = null;
94+
if (EndpointManager.allowConnect(this.endpoint))
95+
{
96+
RemoteClient remoteClient = initRemoteClient(context);
97+
98+
String auth = req.getHeader("X-OAuth-Data");
99+
if (auth != null && !auth.equals(""))
100+
{
101+
if (logger.isDebugEnabled())
102+
logger.debug("Found OAuth data " + auth);
103+
104+
Pattern p = Pattern.compile("(.+)=\"(.+)\"");
105+
String[] authParams = auth.split(",");
106+
Map<String, String> authMap = new HashMap<String, String>(authParams.length);
107+
for (int i = 0; i < authParams.length; i++)
108+
{
109+
Matcher m = p.matcher(authParams[i]);
110+
if (m.matches())
111+
{
112+
authMap.put(m.group(1), m.group(2));
113+
}
114+
}
115+
if (!authMap.containsKey("oauth_consumer_key"))
116+
{
117+
authMap.put("oauth_consumer_key", getConsumerKey());
118+
}
119+
if (!authMap.containsKey("oauth_signature"))
120+
{
121+
authMap.put("oauth_signature", generateSignature(authMap));
122+
}
123+
124+
StringBuffer authBuffer = new StringBuffer("OAuth ");
125+
authBuffer.append("oauth_token").append("=\"").append(authMap.get("oauth_token")).append("\",");
126+
for (Map.Entry<String, String> entry : authMap.entrySet())
127+
{
128+
if (!entry.getKey().equals("oauth_token_secret") &&
129+
!entry.getKey().equals("oauth_signature") &&
130+
!entry.getKey().equals("oauth_token"))
131+
{
132+
authBuffer.append(entry.getKey()).append("=\"").append(entry.getValue()).append("\",");
133+
}
134+
}
135+
authBuffer.append("oauth_signature").append("=\"").append(authMap.get("oauth_signature")).append("\"");
136+
137+
if (logger.isDebugEnabled())
138+
logger.debug("Adding Authorization header with data: " + authBuffer.toString());
139+
140+
Map<String, String> headers = new HashMap<String, String>(1);
141+
headers.put("Authorization", authBuffer.toString());
142+
remoteClient.setRequestProperties(headers);
143+
}
144+
145+
// call client and process response
146+
response = remoteClient.call(uri, req, res);
147+
processResponse(remoteClient, response);
148+
}
149+
else
150+
{
151+
ResponseStatus status = new ResponseStatus();
152+
status.setCode(ResponseStatus.STATUS_INTERNAL_SERVER_ERROR);
153+
response = new Response(status);
154+
}
155+
return response;
156+
}
157+
}

0 commit comments

Comments
 (0)