Skip to content

Commit 0bec30d

Browse files
committed
Share OAuth now uses dedicated web scripts and model to store tokens under the person object, via a secured cm:content child. Retain option to use the old preferences method, but this is no longer the default.
git-svn-id: https://share-extras.googlecode.com/svn/trunk/Share OAuth@664 a3f5c567-fd0f-3a89-9b71-a290c5a5f590
1 parent 4193120 commit 0bec30d

File tree

10 files changed

+347
-61
lines changed

10 files changed

+347
-61
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
3+
4+
<beans>
5+
6+
<!-- Registration of new models -->
7+
<bean id="org.sharextras.oauth.dictionaryBootstrap" parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap">
8+
<property name="models">
9+
<list>
10+
<value>alfresco/extension/tokenstore-model.xml</value>
11+
</list>
12+
</property>
13+
</bean>
14+
15+
<bean id="org.sharextras.oauth.resourceBundle" class="org.alfresco.i18n.ResourceBundleBootstrapComponent">
16+
<property name="resourceBundles">
17+
<list>
18+
<value>alfresco.extension.tokenstore-model</value>
19+
</list>
20+
</property>
21+
</bean>
22+
23+
</beans>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Display labels for User Tokens Model
2+
se_tokenstoremodel.aspect.se_userTokens.title=User Tokens
3+
se_tokenstoremodel.aspect.se_userTokens.description=User Tokens
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<model name="se:tokenstoremodel" xmlns="http://www.alfresco.org/model/dictionary/1.0">
4+
5+
<description>Token Store Model</description>
6+
<author>Will Abson</author>
7+
<version>1.0</version>
8+
9+
<imports>
10+
<!-- Import Alfresco Dictionary Definitions -->
11+
<import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d" />
12+
<!-- Import Alfresco Content Domain Model Definitions -->
13+
<import uri="http://www.alfresco.org/model/content/1.0" prefix="cm" />
14+
<!-- Import Alfresco Datalist Domain Model Definitions -->
15+
<import uri="http://www.alfresco.org/model/datalist/1.0" prefix="dl"/>
16+
</imports>
17+
18+
<!-- Introduction of new namespaces defined by this model -->
19+
<namespaces>
20+
<namespace uri="http://sharextras.org/model/sharextras/1.0" prefix="se" />
21+
</namespaces>
22+
23+
<aspects>
24+
<aspect name="se:userTokens">
25+
<title>User Tokens</title>
26+
<associations>
27+
<child-association name="se:tokenContent">
28+
<source>
29+
<mandatory>false</mandatory>
30+
<many>false</many>
31+
</source>
32+
<target>
33+
<class>cm:content</class>
34+
<mandatory>false</mandatory>
35+
<many>false</many>
36+
</target>
37+
<duplicate>false</duplicate>
38+
<propagateTimestamps>false</propagateTimestamps>
39+
</child-association>
40+
</associations>
41+
</aspect>
42+
</aspects>
43+
44+
</model>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<webscript>
2+
<shortname>Get user token</shortname>
3+
<description>Get user token information</description>
4+
<url>/extras/slingshot/tokenstore/usertoken</url>
5+
<url>/extras/slingshot/tokenstore/usertoken/{path}</url>
6+
<format default="json">extension</format>
7+
<authentication>user</authentication>
8+
</webscript>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function main()
2+
{
3+
jsonStr = "{}";
4+
if (person.hasAspect("se:userTokens"))
5+
{
6+
var children = person.childAssocs["se:tokenContent"];
7+
if (children.length == 1)
8+
{
9+
var tokenContent = children[0].content,
10+
json = jsonUtils.toObject(tokenContent);
11+
12+
// JSON should be in a tree-like structure, e.g. { org { sharextras: { ... } } }
13+
// Base object from URL args should be something like org/sharextras/blah (default otherwise is to return everything)
14+
var basePath = url.templateArgs.path || "",
15+
baseParts = basePath.split("/"),
16+
baseObj = json;
17+
18+
for (var i = 0; i < baseParts.length; i++)
19+
{
20+
if (baseParts[i] != "")
21+
{
22+
baseObj = baseObj[baseParts[i]];
23+
}
24+
}
25+
26+
// Apply any filter
27+
var filteredObj = {}, currFilteredObj = filteredObj, currObj = baseObj, filterParts = (args.filter || "").split("."), part, lastPart;
28+
filteredObj = findValueByDotNotation(baseObj, args.filter || "", {});
29+
30+
jsonStr = jsonUtils.toJSONString(filteredObj);
31+
}
32+
}
33+
model.jsonStr = jsonStr;
34+
}
35+
function findValueByDotNotation(obj, propertyPath, defaultValue)
36+
{
37+
var value = defaultValue ? defaultValue : null;
38+
if (propertyPath && obj)
39+
{
40+
var currObj = obj;
41+
var newObj = {}, ptrObj = newObj;
42+
var props = propertyPath.split(".");
43+
for (var i = 0; i < props.length; i++)
44+
{
45+
currObj = currObj[props[i]];
46+
ptrObj[props[i]] = (typeof currObj == "object") ? {} : currObj;
47+
ptrObj = ptrObj[props[i]];
48+
if (typeof currObj == "undefined")
49+
{
50+
return value;
51+
}
52+
}
53+
return newObj;
54+
}
55+
return value;
56+
};
57+
main();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
${jsonStr}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<webscript>
2+
<shortname>Add user token</shortname>
3+
<description>Add user token information</description>
4+
<url>/extras/slingshot/tokenstore/usertoken</url>
5+
<url>/extras/slingshot/tokenstore/usertoken/{path}</url>
6+
<format default="json">extension</format>
7+
<authentication>user</authentication>
8+
</webscript>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
function main()
2+
{
3+
jsonStr = "{}";
4+
if (person.hasAspect("se:userTokens"))
5+
{
6+
person.addAspect("se:userTokens");
7+
}
8+
var children = person.childAssocs["se:tokenContent"],
9+
contentNode;
10+
if (children == null || children.length == 0)
11+
{
12+
contentNode = person.createNode("userTokens", "cm:content", "se:tokenContent");
13+
contentNode.content = "{}";
14+
contentNode.setInheritsPermissions(false);
15+
}
16+
else
17+
{
18+
contentNode = children[0];
19+
}
20+
var jsonObj = jsonUtils.toObject(contentNode.content);
21+
22+
// JSON should be in a tree-like structure, e.g. { org { sharextras: { ... } } }
23+
// Base path from URL args should be something like org/sharextras/blah (default otherwise is to return everything)
24+
var basePath = url.templateArgs.path || "",
25+
baseParts = basePath.split("/"),
26+
baseObj = jsonObj;
27+
28+
for (var i = 0; i < baseParts.length; i++)
29+
{
30+
if (baseParts[i] != "") // Skip empty parts
31+
{
32+
baseObj = baseObj[baseParts[i]] || (baseObj[baseParts[i]] = {});
33+
}
34+
}
35+
36+
// Get object from the request body
37+
var jsonData = jsonUtils.toObject(requestbody.content);
38+
39+
// Set contents of base object to jsonData
40+
// jsonData should be MERGED into baseObj
41+
mergeObjects(baseObj, jsonData);
42+
43+
// Save the complete object back to the file
44+
contentNode.content = jsonUtils.toJSONString(jsonObj);
45+
46+
jsonStr = jsonUtils.toJSONString(baseObj);
47+
model.jsonStr = jsonStr;
48+
}
49+
/**
50+
* Copy object 2 into object 1
51+
* @param obj1
52+
* @param obj2
53+
* @returns
54+
*/
55+
function mergeObjects(obj1, obj2)
56+
{
57+
if (typeof obj1 != typeof obj2)
58+
{
59+
throw "Objects " + obj1 + " and " + obj2 + " are not of same type";
60+
}
61+
for (p in obj2)
62+
{
63+
if (typeof obj1[p] == "object" && typeof obj2[p] == "object")
64+
{
65+
mergeObjects(obj1[p], obj2[p]);
66+
}
67+
else if (typeof obj1[p] != "undefined" && typeof obj2[p] == "undefined")
68+
{
69+
// Do nothing, no replacement specified
70+
}
71+
else if (typeof obj1[p] == "undefined" && typeof obj2[p] != "undefined")
72+
{
73+
obj1[p] = obj2[p];
74+
}
75+
else
76+
{
77+
// Both non-objects
78+
obj1[p] = obj2[p];
79+
}
80+
}
81+
return obj1;
82+
}
83+
main();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
${jsonStr}

0 commit comments

Comments
 (0)