Skip to content

Commit 27c89c6

Browse files
committed
Add OAuth 2.0 token web scripts copied from Chatter add-on
1 parent d528f39 commit 27c89c6

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.alfresco.integrations.salesforce.chatter;
2+
3+
import java.io.IOException;
4+
5+
import org.alfresco.service.cmr.oauth2.OAuth2CredentialsStoreService;
6+
import org.alfresco.service.cmr.remotecredentials.OAuth2CredentialsInfo;
7+
import org.json.JSONException;
8+
import org.json.JSONStringer;
9+
import org.json.JSONWriter;
10+
import org.springframework.extensions.webscripts.AbstractWebScript;
11+
import org.springframework.extensions.webscripts.Format;
12+
import org.springframework.extensions.webscripts.Status;
13+
import org.springframework.extensions.webscripts.WebScriptException;
14+
import org.springframework.extensions.webscripts.WebScriptRequest;
15+
import org.springframework.extensions.webscripts.WebScriptResponse;
16+
17+
/**
18+
* Fetch an OAuth 2.0 ticket from the credentials store.
19+
*
20+
* @author Will Abson
21+
*/
22+
public class GetOAuthToken extends AbstractWebScript
23+
{
24+
25+
// Services
26+
private OAuth2CredentialsStoreService oauth2CredentialsStoreService;
27+
28+
public void setOauth2CredentialsStoreService(OAuth2CredentialsStoreService oauth2CredentialsStoreService)
29+
{
30+
this.oauth2CredentialsStoreService = oauth2CredentialsStoreService;
31+
}
32+
33+
@Override
34+
public void execute(WebScriptRequest req, WebScriptResponse resp)
35+
throws IOException
36+
{
37+
String keyName = req.getServiceMatch().getTemplateVars().get("name");
38+
39+
if (keyName == null || "".equals(keyName))
40+
{
41+
throw new WebScriptException("A key name must be specified");
42+
}
43+
44+
OAuth2CredentialsInfo credentialInfo = oauth2CredentialsStoreService.getPersonalOAuth2Credentials(keyName);
45+
46+
if (credentialInfo != null)
47+
{
48+
try
49+
{
50+
resp.setContentType(Format.JSON.mimetype());
51+
resp.setContentEncoding("UTF-8");
52+
// Start object
53+
JSONWriter jsonObj = new JSONStringer().object();
54+
// Add string values
55+
jsonObj.key("accessToken").value(credentialInfo.getOAuthAccessToken());
56+
jsonObj.key("refreshToken").value(credentialInfo.getOAuthRefreshToken());
57+
jsonObj.key("ticketExpiresAt").value(credentialInfo.getOAuthTicketExpiresAt());
58+
jsonObj.key("ticketTokenIssuedAt").value(credentialInfo.getOAuthTicketIssuedAt());
59+
// End object
60+
jsonObj.endObject();
61+
62+
// Write JSON to the response body
63+
resp.getWriter().write(jsonObj.toString());
64+
}
65+
catch (JSONException e)
66+
{
67+
throw new WebScriptException("Error building JSON data", e);
68+
}
69+
}
70+
else
71+
{
72+
throw new WebScriptException(Status.STATUS_NOT_FOUND, "Could not find credentials with name " + keyName);
73+
}
74+
}
75+
76+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.alfresco.integrations.salesforce.chatter;
2+
3+
import java.io.IOException;
4+
import java.util.Date;
5+
6+
import org.alfresco.service.cmr.oauth2.OAuth2CredentialsStoreService;
7+
import org.alfresco.service.cmr.remoteticket.NoSuchSystemException;
8+
import org.apache.commons.logging.Log;
9+
import org.apache.commons.logging.LogFactory;
10+
import org.json.JSONException;
11+
import org.json.JSONObject;
12+
import org.json.JSONTokener;
13+
import org.springframework.extensions.webscripts.AbstractWebScript;
14+
import org.springframework.extensions.webscripts.WebScriptException;
15+
import org.springframework.extensions.webscripts.WebScriptRequest;
16+
import org.springframework.extensions.webscripts.WebScriptResponse;
17+
18+
/**
19+
* Save an OAuth 2.0 ticket into the credentials store.
20+
*
21+
* @author Will Abson
22+
*/
23+
public class SaveOAuthToken extends AbstractWebScript
24+
{
25+
26+
private static Log logger = LogFactory.getLog(SaveOAuthToken.class);
27+
28+
// Services
29+
private OAuth2CredentialsStoreService oauth2CredentialsStoreService;
30+
31+
public void setOauth2CredentialsStoreService(OAuth2CredentialsStoreService oauth2CredentialsStoreService)
32+
{
33+
this.oauth2CredentialsStoreService = oauth2CredentialsStoreService;
34+
}
35+
36+
@Override
37+
public void execute(WebScriptRequest req, WebScriptResponse arg1)
38+
throws IOException
39+
{
40+
String jsonStr = req.getContent().getContent();
41+
if (logger.isDebugEnabled())
42+
{
43+
logger.debug("Got JSON data: " + jsonStr);
44+
}
45+
46+
try
47+
{
48+
JSONObject reqJson = new JSONObject(new JSONTokener(jsonStr));
49+
50+
String remoteSystem = reqJson.getString("name"),
51+
accessToken = reqJson.getString("accessToken"),
52+
refreshToken = reqJson.getString("refreshToken");
53+
54+
if (logger.isDebugEnabled())
55+
{
56+
logger.debug("Name: " + remoteSystem);
57+
logger.debug("Access token: " + accessToken);
58+
logger.debug("Refresh token: " + refreshToken);
59+
}
60+
61+
Date expiresIn = null; // TODO need to pick an arbitrary date?
62+
63+
try
64+
{
65+
oauth2CredentialsStoreService.storePersonalOAuth2Credentials(remoteSystem, accessToken, refreshToken, expiresIn, new Date());
66+
}
67+
catch (NoSuchSystemException nsse)
68+
{
69+
throw nsse;
70+
}
71+
72+
}
73+
catch (JSONException e)
74+
{
75+
throw new WebScriptException("A problem occurred parsing the request JSON", e);
76+
}
77+
78+
}
79+
80+
}

0 commit comments

Comments
 (0)