summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2014-09-09 07:44:01 -0700
committerJesse Morgan <jesse@jesterpm.net>2014-09-09 07:44:01 -0700
commitc2feb363e513c0dea83d507eb9ba1918748d4e8e (patch)
tree8ba314d14f86aff178adac5ba46ca35e037c6569 /src
parent66469ff2f76ac916e289e066377570b38554fdcc (diff)
Moving F1OAuthHelper to F1Access.
F1Access will provide a collection of F1 API methods, rather than placing all of those methods in F1User as I was originally planning.
Diffstat (limited to 'src')
-rw-r--r--src/com/p4square/f1oauth/F1Access.java207
-rw-r--r--src/com/p4square/f1oauth/F1OAuthHelper.java144
-rw-r--r--src/com/p4square/f1oauth/F1User.java72
-rw-r--r--src/com/p4square/f1oauth/SecondPartyAuthenticator.java4
-rw-r--r--src/com/p4square/f1oauth/SecondPartyVerifier.java6
-rw-r--r--src/com/p4square/grow/frontend/AssessmentResultsPage.java2
-rw-r--r--src/com/p4square/grow/frontend/GrowFrontend.java12
-rw-r--r--src/com/p4square/grow/frontend/NewAccountResource.java6
-rw-r--r--src/com/p4square/restlet/oauth/OAuthHelper.java4
9 files changed, 226 insertions, 231 deletions
diff --git a/src/com/p4square/f1oauth/F1Access.java b/src/com/p4square/f1oauth/F1Access.java
new file mode 100644
index 0000000..35957bf
--- /dev/null
+++ b/src/com/p4square/f1oauth/F1Access.java
@@ -0,0 +1,207 @@
+/*
+ * Copyright 2014 Jesse Morgan
+ */
+
+package com.p4square.f1oauth;
+
+import java.net.URLEncoder;
+
+import org.apache.log4j.Logger;
+
+import org.restlet.Context;
+import org.restlet.Response;
+import org.restlet.Request;
+import org.restlet.data.ChallengeResponse;
+import org.restlet.data.ChallengeScheme;
+import org.restlet.data.MediaType;
+import org.restlet.data.Method;
+import org.restlet.data.Status;
+import org.restlet.engine.util.Base64;
+import org.restlet.representation.StringRepresentation;
+
+import com.p4square.restlet.oauth.OAuthException;
+import com.p4square.restlet.oauth.OAuthHelper;
+import com.p4square.restlet.oauth.OAuthUser;
+import com.p4square.restlet.oauth.Token;
+
+/**
+ * F1 API Access.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class F1Access {
+ public enum UserType {
+ WEBLINK, PORTAL;
+ }
+
+ private static final Logger LOG = Logger.getLogger(F1Access.class);
+
+ private static final String VERSION_STRING = "/v1/";
+ private static final String REQUESTTOKEN_URL = "Tokens/RequestToken";
+ private static final String AUTHORIZATION_URL = "Login";
+ private static final String ACCESSTOKEN_URL= "Tokens/AccessToken";
+ private static final String TRUSTED_ACCESSTOKEN_URL = "/AccessToken";
+
+ private final String mBaseUrl;
+ private final String mMethod;
+
+ private final OAuthHelper mOAuthHelper;
+
+ /**
+ */
+ public F1Access(Context context, String consumerKey, String consumerSecret,
+ String baseUrl, String churchCode, UserType userType) {
+
+ switch (userType) {
+ case WEBLINK:
+ mMethod = "WeblinkUser";
+ break;
+ case PORTAL:
+ mMethod = "PortalUser";
+ break;
+ default:
+ throw new IllegalArgumentException("Unknown UserType");
+ }
+
+ mBaseUrl = "https://" + churchCode + "." + baseUrl + VERSION_STRING;
+
+ // Create the OAuthHelper. This implicitly registers the helper to
+ // handle outgoing requests which need OAuth authentication.
+ mOAuthHelper = new OAuthHelper(context, consumerKey, consumerSecret) {
+ @Override
+ protected String getRequestTokenUrl() {
+ return mBaseUrl + REQUESTTOKEN_URL;
+ }
+
+ @Override
+ public String getLoginUrl(Token requestToken, String callback) {
+ String loginUrl = mBaseUrl + mMethod + AUTHORIZATION_URL
+ + "?oauth_token=" + URLEncoder.encode(requestToken.getToken());
+
+ if (callback != null) {
+ loginUrl += "&oauth_callback=" + URLEncoder.encode(callback);
+ }
+
+ return loginUrl;
+ }
+
+ @Override
+ protected String getAccessTokenUrl() {
+ return mBaseUrl + ACCESSTOKEN_URL;
+ }
+ };
+
+ }
+
+ /**
+ * Request an AccessToken for a particular username and password.
+ *
+ * This is an F1 extension to OAuth:
+ * http://developer.fellowshipone.com/docs/v1/Util/AuthDocs.help#2creds
+ */
+ public OAuthUser getAccessToken(String username, String password) throws OAuthException {
+ Request request = new Request(Method.POST, mBaseUrl + mMethod + TRUSTED_ACCESSTOKEN_URL);
+ request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_OAUTH));
+
+ String base64String = Base64.encode((username + " " + password).getBytes(), false);
+ request.setEntity(new StringRepresentation(base64String));
+
+ return mOAuthHelper.processAccessTokenRequest(request);
+ }
+
+ /**
+ * Create a new Account.
+ *
+ * @param firstname The user's first name.
+ * @param lastname The user's last name.
+ * @param email The user's email address.
+ * @param redirect The URL to send the user to after confirming his address.
+ *
+ * @return true if created, false if the account already exists.
+ */
+ public boolean createAccount(String firstname, String lastname, String email, String redirect)
+ throws OAuthException {
+ String req = String.format("{\n\"account\":{\n\"firstName\":\"%s\",\n"
+ + "\"lastName\":\"%s\",\n\"email\":\"%s\",\n"
+ + "\"urlRedirect\":\"%s\"\n}\n}",
+ firstname, lastname, email, redirect);
+
+ Request request = new Request(Method.POST, mBaseUrl + "Accounts");
+ request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_OAUTH));
+ request.setEntity(new StringRepresentation(req, MediaType.APPLICATION_JSON));
+
+ Response response = mOAuthHelper.getResponse(request);
+
+ Status status = response.getStatus();
+ if (Status.SUCCESS_NO_CONTENT.equals(status)) {
+ return true;
+
+ } else if (Status.CLIENT_ERROR_CONFLICT.equals(status)) {
+ return false;
+
+ } else {
+ throw new OAuthException(status);
+ }
+ }
+
+ /*
+ public addAttribute(Attribute attribute, String comment) {
+ String baseUrl = getBaseUrl();
+ Map newAttributeTemplate = null;
+
+ // Get Attribute Template
+ Request request = new Request(Method.GET,
+ baseUrl + "People/" + getIdentifier() + "/Attributes/new.json");
+ request.setChallengeResponse(getChallengeResponse());
+ Response response = getContext().getClientDispatcher().handle(request);
+
+ Representation representation = response.getEntity();
+ try {
+ Status status = response.getStatus();
+ if (status.isSuccess()) {
+ JacksonRepresentation<Map> entity = new JacksonRepresentation<Map>(response.getEntity(), Map.class);
+ newAttributeTemplate = entity.getObject();
+ }
+
+ } finally {
+ if (representation != null) {
+ representation.release();
+ }
+ }
+
+ if (newAttributeTemplate == null) {
+ LOG.error("Could not retrieve attribute template!");
+ return;
+ }
+
+ // Populate Attribute Template
+
+
+ // POST new attribute
+ Request request = new Request(Method.POST,
+ baseUrl + "People/" + getIdentifier() + "/Attributes.json");
+ request.setChallengeResponse(getChallengeResponse());
+ Response response = getContext().getClientDispatcher().handle(request);
+
+ Representation representation = response.getEntity();
+ try {
+ Status status = response.getStatus();
+ if (status.isSuccess()) {
+ JacksonRepresentation<Map> entity = new JacksonRepresentation<Map>(response.getEntity(), Map.class);
+ newAttributeTemplate = entity.getObject();
+ }
+
+ } finally {
+ if (representation != null) {
+ representation.release();
+ }
+ }
+
+ if (newAttributeTemplate == null) {
+ LOG.error("Could retrieve attribute template!");
+ return;
+ }
+
+ }
+ */
+}
diff --git a/src/com/p4square/f1oauth/F1OAuthHelper.java b/src/com/p4square/f1oauth/F1OAuthHelper.java
deleted file mode 100644
index 187fb6b..0000000
--- a/src/com/p4square/f1oauth/F1OAuthHelper.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * Copyright 2013 Jesse Morgan
- */
-
-package com.p4square.f1oauth;
-
-import java.net.URLEncoder;
-
-import org.apache.log4j.Logger;
-
-import org.restlet.Context;
-import org.restlet.Response;
-import org.restlet.Request;
-import org.restlet.data.ChallengeResponse;
-import org.restlet.data.ChallengeScheme;
-import org.restlet.data.MediaType;
-import org.restlet.data.Method;
-import org.restlet.data.Status;
-import org.restlet.engine.util.Base64;
-import org.restlet.representation.StringRepresentation;
-
-import com.p4square.restlet.oauth.OAuthException;
-import com.p4square.restlet.oauth.OAuthHelper;
-import com.p4square.restlet.oauth.OAuthUser;
-import com.p4square.restlet.oauth.Token;
-
-/**
- *
- * @author Jesse Morgan <jesse@jesterpm.net>
- */
-public class F1OAuthHelper extends OAuthHelper {
- public enum UserType {
- WEBLINK, PORTAL;
- }
-
- private static final Logger LOG = Logger.getLogger(F1OAuthHelper.class);
-
- private static final String VERSION_STRING = "/v1/";
- private static final String REQUESTTOKEN_URL = "Tokens/RequestToken";
- private static final String AUTHORIZATION_URL = "Login";
- private static final String ACCESSTOKEN_URL= "Tokens/AccessToken";
- private static final String TRUSTED_ACCESSTOKEN_URL = "/AccessToken";
-
- private final String mBaseUrl;
- private final String mMethod;
-
- /**
- * @param method Either WeblinkUser or PortalUser.
- */
- public F1OAuthHelper(Context context, String consumerKey, String consumerSecret,
- String baseUrl, String churchCode, UserType userType) {
- super(context, consumerKey, consumerSecret);
-
- switch (userType) {
- case WEBLINK:
- mMethod = "WeblinkUser";
- break;
- case PORTAL:
- mMethod = "PortalUser";
- break;
- default:
- throw new IllegalArgumentException("Unknown UserType");
- }
-
- mBaseUrl = "https://" + churchCode + "." + baseUrl + VERSION_STRING;
- }
-
- /**
- * @return The base url for the F1 API, ending with a slash.
- */
- public String getBaseUrl() {
- return mBaseUrl;
- }
-
- /**
- * @return the URL for the initial RequestToken request.
- */
- protected String getRequestTokenUrl() {
- return mBaseUrl + REQUESTTOKEN_URL;
- }
-
- /**
- * @return the URL to redirect the user to for Authentication.
- */
- public String getLoginUrl(Token requestToken, String callback) {
- String loginUrl = mBaseUrl + mMethod + AUTHORIZATION_URL
- + "?oauth_token=" + URLEncoder.encode(requestToken.getToken());
-
- if (callback != null) {
- loginUrl += "&oauth_callback=" + URLEncoder.encode(callback);
- }
-
- return loginUrl;
- }
-
-
- /**
- * @return the URL for the AccessToken request.
- */
- protected String getAccessTokenUrl() {
- return mBaseUrl + ACCESSTOKEN_URL;
- }
-
- /**
- * Request an AccessToken for a particular username and password.
- *
- * This is an F1 extension to OAuth:
- * http://developer.fellowshipone.com/docs/v1/Util/AuthDocs.help#2creds
- */
- public OAuthUser getAccessToken(String username, String password) throws OAuthException {
- Request request = new Request(Method.POST, mBaseUrl + mMethod + TRUSTED_ACCESSTOKEN_URL);
- request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_OAUTH));
-
- String base64String = Base64.encode((username + " " + password).getBytes(), false);
- request.setEntity(new StringRepresentation(base64String));
-
- return processAccessTokenRequest(request);
- }
-
- public boolean createAccount(String firstname, String lastname, String email, String redirect)
- throws OAuthException {
- String req = String.format("{\n\"account\":{\n\"firstName\":\"%s\",\n"
- + "\"lastName\":\"%s\",\n\"email\":\"%s\",\n"
- + "\"urlRedirect\":\"%s\"\n}\n}",
- firstname, lastname, email, redirect);
-
- Request request = new Request(Method.POST, mBaseUrl + "Accounts");
- request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_OAUTH));
- request.setEntity(new StringRepresentation(req, MediaType.APPLICATION_JSON));
-
- Response response = getResponse(request);
-
- Status status = response.getStatus();
- if (Status.SUCCESS_NO_CONTENT.equals(status)) {
- return true;
-
- } else if (Status.CLIENT_ERROR_CONFLICT.equals(status)) {
- return false;
-
- } else {
- throw new OAuthException(status);
- }
- }
-}
diff --git a/src/com/p4square/f1oauth/F1User.java b/src/com/p4square/f1oauth/F1User.java
index 942f534..e5ab487 100644
--- a/src/com/p4square/f1oauth/F1User.java
+++ b/src/com/p4square/f1oauth/F1User.java
@@ -19,7 +19,6 @@ public class F1User extends OAuthUser {
public static final String LAST_NAME = "lastName";
public static final String ICODE = "@iCode";
- private final String mBaseUrl;
private final Map mData;
/**
@@ -29,10 +28,9 @@ public class F1User extends OAuthUser {
* @param data F1 Person Record.
* @throws IllegalStateException if data.get("person") is null.
*/
- public F1User(String baseUrl, OAuthUser user, Map data) {
+ public F1User(OAuthUser user, Map data) {
super(user.getLocation(), user.getToken());
- mBaseUrl = baseUrl;
mData = (Map) data.get("person");
if (mData == null) {
throw new IllegalStateException("Bad data");
@@ -69,72 +67,4 @@ public class F1User extends OAuthUser {
public Object get(String key) {
return mData.get(key);
}
-
- /**
- * @return the F1 API base url.
- */
- public String getBaseUrl() {
- return mBaseUrl;
- }
-
- /*
- public addAttribute(Attribute attribute, String comment) {
- String baseUrl = getBaseUrl();
- Map newAttributeTemplate = null;
-
- // Get Attribute Template
- Request request = new Request(Method.GET,
- baseUrl + "People/" + getIdentifier() + "/Attributes/new.json");
- request.setChallengeResponse(getChallengeResponse());
- Response response = getContext().getClientDispatcher().handle(request);
-
- Representation representation = response.getEntity();
- try {
- Status status = response.getStatus();
- if (status.isSuccess()) {
- JacksonRepresentation<Map> entity = new JacksonRepresentation<Map>(response.getEntity(), Map.class);
- newAttributeTemplate = entity.getObject();
- }
-
- } finally {
- if (representation != null) {
- representation.release();
- }
- }
-
- if (newAttributeTemplate == null) {
- LOG.error("Could not retrieve attribute template!");
- return;
- }
-
- // Populate Attribute Template
-
-
- // POST new attribute
- Request request = new Request(Method.POST,
- baseUrl + "People/" + getIdentifier() + "/Attributes.json");
- request.setChallengeResponse(getChallengeResponse());
- Response response = getContext().getClientDispatcher().handle(request);
-
- Representation representation = response.getEntity();
- try {
- Status status = response.getStatus();
- if (status.isSuccess()) {
- JacksonRepresentation<Map> entity = new JacksonRepresentation<Map>(response.getEntity(), Map.class);
- newAttributeTemplate = entity.getObject();
- }
-
- } finally {
- if (representation != null) {
- representation.release();
- }
- }
-
- if (newAttributeTemplate == null) {
- LOG.error("Could retrieve attribute template!");
- return;
- }
-
- }
- */
}
diff --git a/src/com/p4square/f1oauth/SecondPartyAuthenticator.java b/src/com/p4square/f1oauth/SecondPartyAuthenticator.java
index 1983d69..8deefec 100644
--- a/src/com/p4square/f1oauth/SecondPartyAuthenticator.java
+++ b/src/com/p4square/f1oauth/SecondPartyAuthenticator.java
@@ -21,9 +21,9 @@ import org.restlet.security.Authenticator;
public class SecondPartyAuthenticator extends Authenticator {
private static final Logger LOG = Logger.getLogger(SecondPartyAuthenticator.class);
- private final F1OAuthHelper mHelper;
+ private final F1Access mHelper;
- public SecondPartyAuthenticator(Context context, boolean optional, F1OAuthHelper helper) {
+ public SecondPartyAuthenticator(Context context, boolean optional, F1Access helper) {
super(context, optional);
mHelper = helper;
diff --git a/src/com/p4square/f1oauth/SecondPartyVerifier.java b/src/com/p4square/f1oauth/SecondPartyVerifier.java
index 9fb771f..e2d6d00 100644
--- a/src/com/p4square/f1oauth/SecondPartyVerifier.java
+++ b/src/com/p4square/f1oauth/SecondPartyVerifier.java
@@ -30,9 +30,9 @@ public class SecondPartyVerifier implements Verifier {
private static final Logger LOG = Logger.getLogger(SecondPartyVerifier.class);
private final Restlet mDispatcher;
- private final F1OAuthHelper mHelper;
+ private final F1Access mHelper;
- public SecondPartyVerifier(Context context, F1OAuthHelper helper) {
+ public SecondPartyVerifier(Context context, F1Access helper) {
if (helper == null) {
throw new IllegalArgumentException("Helper can not be null.");
}
@@ -79,7 +79,7 @@ public class SecondPartyVerifier implements Verifier {
if (status.isSuccess()) {
JacksonRepresentation<Map> entity = new JacksonRepresentation<Map>(response.getEntity(), Map.class);
Map data = entity.getObject();
- return new F1User(mHelper.getBaseUrl(), user, data);
+ return new F1User(user, data);
} else {
throw new OAuthException(status);
diff --git a/src/com/p4square/grow/frontend/AssessmentResultsPage.java b/src/com/p4square/grow/frontend/AssessmentResultsPage.java
index 95c3f6a..c205503 100644
--- a/src/com/p4square/grow/frontend/AssessmentResultsPage.java
+++ b/src/com/p4square/grow/frontend/AssessmentResultsPage.java
@@ -22,6 +22,8 @@ import com.p4square.fmfacade.json.JsonRequestClient;
import com.p4square.fmfacade.json.JsonResponse;
import com.p4square.fmfacade.json.ClientException;
+import com.p4square.f1oauth.F1User;
+
import com.p4square.grow.config.Config;
/**
diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java
index 4b193d0..926670b 100644
--- a/src/com/p4square/grow/frontend/GrowFrontend.java
+++ b/src/com/p4square/grow/frontend/GrowFrontend.java
@@ -30,7 +30,7 @@ import com.p4square.fmfacade.FreeMarkerPageResource;
import com.p4square.grow.config.Config;
-import com.p4square.f1oauth.F1OAuthHelper;
+import com.p4square.f1oauth.F1Access;
import com.p4square.f1oauth.SecondPartyVerifier;
import com.p4square.session.SessionCheckingAuthenticator;
@@ -49,7 +49,7 @@ public class GrowFrontend extends FMFacade {
private Config mConfig;
- private F1OAuthHelper mHelper;
+ private F1Access mHelper;
public GrowFrontend() {
this(new Config());
@@ -73,13 +73,13 @@ public class GrowFrontend extends FMFacade {
super.start();
}
- synchronized F1OAuthHelper getHelper() {
+ synchronized F1Access getF1Access() {
if (mHelper == null) {
- mHelper = new F1OAuthHelper(getContext(), mConfig.getString("f1ConsumerKey", ""),
+ mHelper = new F1Access(getContext(), mConfig.getString("f1ConsumerKey", ""),
mConfig.getString("f1ConsumerSecret", ""),
mConfig.getString("f1BaseUrl", "staging.fellowshiponeapi.com"),
mConfig.getString("f1ChurchCode", "pfseawa"),
- F1OAuthHelper.UserType.WEBLINK);
+ F1Access.UserType.WEBLINK);
}
return mHelper;
@@ -129,7 +129,7 @@ public class GrowFrontend extends FMFacade {
SessionCheckingAuthenticator sessionChk = new SessionCheckingAuthenticator(context, true);
// This is used to authenticate the user
- SecondPartyVerifier f1Verifier = new SecondPartyVerifier(context, getHelper());
+ SecondPartyVerifier f1Verifier = new SecondPartyVerifier(context, getF1Access());
LoginFormAuthenticator loginAuth = new LoginFormAuthenticator(context, false, f1Verifier);
loginAuth.setLoginFormUrl(loginPage);
loginAuth.setLoginPostUrl(loginPost);
diff --git a/src/com/p4square/grow/frontend/NewAccountResource.java b/src/com/p4square/grow/frontend/NewAccountResource.java
index 9155a00..54c1790 100644
--- a/src/com/p4square/grow/frontend/NewAccountResource.java
+++ b/src/com/p4square/grow/frontend/NewAccountResource.java
@@ -18,7 +18,7 @@ import org.restlet.ext.freemarker.TemplateRepresentation;
import org.apache.log4j.Logger;
-import com.p4square.f1oauth.F1OAuthHelper;
+import com.p4square.f1oauth.F1Access;
import com.p4square.restlet.oauth.OAuthException;
import com.p4square.fmfacade.FreeMarkerPageResource;
@@ -32,7 +32,7 @@ public class NewAccountResource extends FreeMarkerPageResource {
private static Logger LOG = Logger.getLogger(NewAccountResource.class);
private GrowFrontend mGrowFrontend;
- private F1OAuthHelper mHelper;
+ private F1Access mHelper;
private String mErrorMessage;
@@ -44,7 +44,7 @@ public class NewAccountResource extends FreeMarkerPageResource {
super.doInit();
mGrowFrontend = (GrowFrontend) getApplication();
- mHelper = mGrowFrontend.getHelper();
+ mHelper = mGrowFrontend.getF1Access();
mErrorMessage = "";
diff --git a/src/com/p4square/restlet/oauth/OAuthHelper.java b/src/com/p4square/restlet/oauth/OAuthHelper.java
index 39c1b02..67dd238 100644
--- a/src/com/p4square/restlet/oauth/OAuthHelper.java
+++ b/src/com/p4square/restlet/oauth/OAuthHelper.java
@@ -127,7 +127,7 @@ public abstract class OAuthHelper {
* @return An OAuthUser object wrapping the AccessToken.
* @throws OAuthException if the request failed.
*/
- protected OAuthUser processAccessTokenRequest(Request request) throws OAuthException {
+ public OAuthUser processAccessTokenRequest(Request request) throws OAuthException {
Response response = getResponse(request);
Token accessToken = processTokenRequest(response);
@@ -143,7 +143,7 @@ public abstract class OAuthHelper {
/**
* Helper method to get a Response for a Request.
*/
- protected Response getResponse(Request request) {
+ public Response getResponse(Request request) {
return mDispatcher.handle(request);
}
}