summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2014-02-01 18:36:36 -0800
committerJesse Morgan <jesse@jesterpm.net>2014-02-01 18:36:36 -0800
commit76250a91d991d2dbe5817dcbe52b2d2021a7ba21 (patch)
treeb6248edb03e4cb2f12d9d60157f00b76f7aafacc
parentf98e0f85be61702e7e4ba67f4f32dad22aa7ff7f (diff)
Changing user identifier from email to F1 user id.
-rw-r--r--src/com/p4square/f1oauth/F1User.java70
-rw-r--r--src/com/p4square/f1oauth/SecondPartyVerifier.java35
-rw-r--r--src/com/p4square/grow/frontend/GrowFrontend.java2
3 files changed, 102 insertions, 5 deletions
diff --git a/src/com/p4square/f1oauth/F1User.java b/src/com/p4square/f1oauth/F1User.java
new file mode 100644
index 0000000..e5ab487
--- /dev/null
+++ b/src/com/p4square/f1oauth/F1User.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2014 Jesse Morgan
+ */
+
+package com.p4square.f1oauth;
+
+import java.util.Map;
+
+import com.p4square.restlet.oauth.OAuthException;
+import com.p4square.restlet.oauth.OAuthUser;
+
+/**
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class F1User extends OAuthUser {
+ public static final String ID = "@id";
+ public static final String FIRST_NAME = "firstName";
+ public static final String LAST_NAME = "lastName";
+ public static final String ICODE = "@iCode";
+
+ private final Map mData;
+
+ /**
+ * Copy the user information from user into a new F1User.
+ *
+ * @param user Original user.
+ * @param data F1 Person Record.
+ * @throws IllegalStateException if data.get("person") is null.
+ */
+ public F1User(OAuthUser user, Map data) {
+ super(user.getLocation(), user.getToken());
+
+ mData = (Map) data.get("person");
+ if (mData == null) {
+ throw new IllegalStateException("Bad data");
+ }
+
+ setIdentifier(getString(ID));
+ setFirstName(getString(FIRST_NAME));
+ setLastName(getString(LAST_NAME));
+ }
+
+ /**
+ * Get a String from the map.
+ *
+ * @param key The map key.
+ * @return The value associated with the key, or null.
+ */
+ public String getString(String key) {
+ Object blob = get(key);
+
+ if (blob instanceof String) {
+ return (String) blob;
+
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Fetch an object from the F1 record.
+ *
+ * @param key The map key
+ * @return The object in the map or null.
+ */
+ public Object get(String key) {
+ return mData.get(key);
+ }
+}
diff --git a/src/com/p4square/f1oauth/SecondPartyVerifier.java b/src/com/p4square/f1oauth/SecondPartyVerifier.java
index 870fe3e..9635283 100644
--- a/src/com/p4square/f1oauth/SecondPartyVerifier.java
+++ b/src/com/p4square/f1oauth/SecondPartyVerifier.java
@@ -4,6 +4,9 @@
package com.p4square.f1oauth;
+import java.io.IOException;
+import java.util.Map;
+
import org.apache.log4j.Logger;
import com.p4square.restlet.oauth.OAuthException;
@@ -12,6 +15,10 @@ import com.p4square.restlet.oauth.OAuthUser;
import org.restlet.Context;
import org.restlet.Request;
import org.restlet.Response;
+import org.restlet.Restlet;
+import org.restlet.data.Method;
+import org.restlet.data.Status;
+import org.restlet.ext.jackson.JacksonRepresentation;
import org.restlet.security.Verifier;
/**
@@ -22,13 +29,15 @@ import org.restlet.security.Verifier;
public class SecondPartyVerifier implements Verifier {
private static final Logger LOG = Logger.getLogger(SecondPartyVerifier.class);
+ private final Restlet mDispatcher;
private final F1OAuthHelper mHelper;
- public SecondPartyVerifier(F1OAuthHelper helper) {
+ public SecondPartyVerifier(Context context, F1OAuthHelper helper) {
if (helper == null) {
throw new IllegalArgumentException("Helper can not be null.");
}
+ mDispatcher = context.getClientDispatcher();
mHelper = helper;
}
@@ -42,8 +51,10 @@ public class SecondPartyVerifier implements Verifier {
String password = new String(request.getChallengeResponse().getSecret());
try {
- OAuthUser user = mHelper.getAccessToken(username, password);
- user.setIdentifier(username);
+ OAuthUser ouser = mHelper.getAccessToken(username, password);
+
+ // Once we have a user, fetch the people record to get the user id.
+ F1User user = getF1User(ouser);
user.setEmail(username);
// This seems like a hack... but it'll work
@@ -51,10 +62,26 @@ public class SecondPartyVerifier implements Verifier {
return RESULT_VALID;
- } catch (OAuthException e) {
+ } catch (Exception e) {
LOG.info("OAuth Exception: " + e, e);
}
return RESULT_INVALID; // Invalid credentials
}
+
+ private F1User getF1User(OAuthUser user) throws OAuthException, IOException {
+ Request request = new Request(Method.GET, user.getLocation() + ".json");
+ request.setChallengeResponse(user.getChallengeResponse());
+ Response response = mDispatcher.handle(request);
+
+ Status status = response.getStatus();
+ if (status.isSuccess()) {
+ JacksonRepresentation<Map> entity = new JacksonRepresentation<Map>(response.getEntity(), Map.class);
+ Map data = entity.getObject();
+ return new F1User(user, data);
+
+ } else {
+ throw new OAuthException(status);
+ }
+ }
}
diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java
index e2cb6c8..1db123f 100644
--- a/src/com/p4square/grow/frontend/GrowFrontend.java
+++ b/src/com/p4square/grow/frontend/GrowFrontend.java
@@ -127,7 +127,7 @@ public class GrowFrontend extends FMFacade {
SessionCheckingAuthenticator sessionChk = new SessionCheckingAuthenticator(context, true);
// This is used to authenticate the user
- SecondPartyVerifier f1Verifier = new SecondPartyVerifier(getHelper());
+ SecondPartyVerifier f1Verifier = new SecondPartyVerifier(context, getHelper());
LoginFormAuthenticator loginAuth = new LoginFormAuthenticator(context, false, f1Verifier);
loginAuth.setLoginFormUrl(loginPage);
loginAuth.setLoginPostUrl(loginPost);