diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2016-03-20 17:07:26 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2016-03-20 17:22:38 -0700 |
commit | 55cba1e0f3373fa69d3b9a66f455ad36ab4b82cf (patch) | |
tree | bed424a7c2989b8bbd5fb874ae23b9ef674ecd8b /src/com/p4square | |
parent | cac52cf3a07fb4c032f352ec48b56640b246f04f (diff) |
Adding support for Church Community Builder login.
Beginning with this change all of the Church Management System
integration logic is moving into implementations of the new
IntegrationDriver interface. The desired IntegrationDriver can be
selected by setting the integrationDriver config to the appropriate
class name.
This commit is only moving login support. Progress reporting will move
in a later commit.
Diffstat (limited to 'src/com/p4square')
10 files changed, 307 insertions, 23 deletions
diff --git a/src/com/p4square/f1oauth/FellowshipOneIntegrationDriver.java b/src/com/p4square/f1oauth/FellowshipOneIntegrationDriver.java new file mode 100644 index 0000000..e72df5e --- /dev/null +++ b/src/com/p4square/f1oauth/FellowshipOneIntegrationDriver.java @@ -0,0 +1,45 @@ +package com.p4square.f1oauth; + +import com.codahale.metrics.MetricRegistry; +import com.p4square.grow.config.Config; +import com.p4square.grow.frontend.IntegrationDriver; +import org.restlet.Context; +import org.restlet.security.Verifier; + +/** + * The FellowshipOneIntegrationDriver creates implementations of various + * objects to support integration with Fellowship One. + */ +public class FellowshipOneIntegrationDriver implements IntegrationDriver { + + private final Context mContext; + private final MetricRegistry mMetricRegistry; + private final Config mConfig; + private final F1Access mAPI; + + public FellowshipOneIntegrationDriver(final Context context) { + mContext = context; + mConfig = (Config) context.getAttributes().get("com.p4square.grow.config"); + mMetricRegistry = (MetricRegistry) context.getAttributes().get("com.p4square.grow.metrics"); + + mAPI = new F1Access(context, + mConfig.getString("f1ConsumerKey", ""), + mConfig.getString("f1ConsumerSecret", ""), + mConfig.getString("f1BaseUrl", "staging.fellowshiponeapi.com"), + mConfig.getString("f1ChurchCode", "pfseawa"), + F1Access.UserType.WEBLINK); + mAPI.setMetricRegistry(mMetricRegistry); + } + + /** + * @return An F1Access instance. + */ + public F1Access getF1Access() { + return mAPI; + } + + @Override + public Verifier newUserAuthenticationVerifier() { + return new SecondPartyVerifier(mContext, mAPI); + } +} diff --git a/src/com/p4square/grow/ccb/CCBUser.java b/src/com/p4square/grow/ccb/CCBUser.java new file mode 100644 index 0000000..7313172 --- /dev/null +++ b/src/com/p4square/grow/ccb/CCBUser.java @@ -0,0 +1,37 @@ +package com.p4square.grow.ccb; + +import com.p4square.ccbapi.model.IndividualProfile; +import org.restlet.security.User; + +/** + * CCBUser is an adapter between a CCB IndividualProfile and a Restlet User. + * + * Note: CCBUser prefixes the user's identifier with "CCB-". This is done to + * ensure the identifier does not collide with identifiers from other + * systems. + */ +public class CCBUser extends User { + + private final IndividualProfile mProfile; + + /** + * Wrap an IndividualProfile inside a User object. + * + * @param profile The CCB IndividualProfile for the user. + */ + public CCBUser(final IndividualProfile profile) { + mProfile = profile; + + setIdentifier("CCB-" + mProfile.getId()); + setFirstName(mProfile.getFirstName()); + setLastName(mProfile.getLastName()); + setEmail(mProfile.getEmail()); + } + + /** + * @return The IndividualProfile of the user. + */ + public IndividualProfile getProfile() { + return mProfile; + } +} diff --git a/src/com/p4square/grow/ccb/CCBUserVerifier.java b/src/com/p4square/grow/ccb/CCBUserVerifier.java new file mode 100644 index 0000000..db10b75 --- /dev/null +++ b/src/com/p4square/grow/ccb/CCBUserVerifier.java @@ -0,0 +1,50 @@ +package com.p4square.grow.ccb; + +import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.model.GetIndividualProfilesRequest; +import com.p4square.ccbapi.model.GetIndividualProfilesResponse; +import org.apache.log4j.Logger; +import org.restlet.Request; +import org.restlet.Response; +import org.restlet.security.Verifier; + +/** + * CCBUserVerifier authenticates a user through the CCB individual_profile_from_login_password API. + */ +public class CCBUserVerifier implements Verifier { + private static final Logger LOG = Logger.getLogger(CCBUserVerifier.class); + + private final CCBAPI mAPI; + + public CCBUserVerifier(final CCBAPI api) { + mAPI = api; + } + + @Override + public int verify(Request request, Response response) { + if (request.getChallengeResponse() == null) { + return RESULT_MISSING; // no credentials + } + + final String username = request.getChallengeResponse().getIdentifier(); + final char[] password = request.getChallengeResponse().getSecret(); + + try { + GetIndividualProfilesResponse resp = mAPI.getIndividualProfiles( + new GetIndividualProfilesRequest().withLoginPassword(username, password)); + + if (resp.getIndividuals().size() == 1) { + // Wrap the IndividualProfile up in an User and update the user on the request. + final CCBUser user = new CCBUser(resp.getIndividuals().get(0)); + LOG.info("Successfully authenticated " + user.getIdentifier()); + request.getClientInfo().setUser(user); + return RESULT_VALID; + } + + } catch (Exception e) { + LOG.error("CCB API Exception: " + e, e); + } + + return RESULT_INVALID; // Invalid credentials + } +} diff --git a/src/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java b/src/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java new file mode 100644 index 0000000..3aeca2c --- /dev/null +++ b/src/com/p4square/grow/ccb/ChurchCommunityBuilderIntegrationDriver.java @@ -0,0 +1,50 @@ +package com.p4square.grow.ccb; + +import com.codahale.metrics.MetricRegistry; +import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.CCBAPIClient; +import com.p4square.grow.config.Config; +import com.p4square.grow.frontend.IntegrationDriver; +import org.restlet.Context; +import org.restlet.security.Verifier; + +import java.net.URI; +import java.net.URISyntaxException; + +/** + * The ChurchCommunityBuilderIntegrationDriver is used to integrate Grow with Church Community Builder. + */ +public class ChurchCommunityBuilderIntegrationDriver implements IntegrationDriver { + + private final Context mContext; + private final MetricRegistry mMetricRegistry; + private final Config mConfig; + + private final CCBAPI mAPI; + + public ChurchCommunityBuilderIntegrationDriver(final Context context) { + mContext = context; + mConfig = (Config) context.getAttributes().get("com.p4square.grow.config"); + mMetricRegistry = (MetricRegistry) context.getAttributes().get("com.p4square.grow.metrics"); + + try { + CCBAPI api = new CCBAPIClient(new URI(mConfig.getString("CCBAPIURL", "")), + mConfig.getString("CCBAPIUser", ""), + mConfig.getString("CCBAPIPassword", "")); + + if (mMetricRegistry != null) { + api = new MonitoredCCBAPI(api, mMetricRegistry); + } + + mAPI = api; + + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + + @Override + public Verifier newUserAuthenticationVerifier() { + return new CCBUserVerifier(mAPI); + } +} diff --git a/src/com/p4square/grow/ccb/MonitoredCCBAPI.java b/src/com/p4square/grow/ccb/MonitoredCCBAPI.java new file mode 100644 index 0000000..6903460 --- /dev/null +++ b/src/com/p4square/grow/ccb/MonitoredCCBAPI.java @@ -0,0 +1,68 @@ +package com.p4square.grow.ccb; + +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.Timer; +import com.p4square.ccbapi.CCBAPI; +import com.p4square.ccbapi.model.GetCustomFieldLabelsResponse; +import com.p4square.ccbapi.model.GetIndividualProfilesRequest; +import com.p4square.ccbapi.model.GetIndividualProfilesResponse; + +import java.io.IOException; + +/** + * MonitoredCCBAPI is a CCBAPI decorator which records metrics for each API call. + */ +public class MonitoredCCBAPI implements CCBAPI { + + private final CCBAPI mAPI; + private final MetricRegistry mMetricRegistry; + + public MonitoredCCBAPI(final CCBAPI api, final MetricRegistry metricRegistry) { + if (api == null) { + throw new IllegalArgumentException("api must not be null."); + } + mAPI = api; + + if (metricRegistry == null) { + throw new IllegalArgumentException("metricRegistry must not be null."); + } + mMetricRegistry = metricRegistry; + } + + @Override + public GetCustomFieldLabelsResponse getCustomFieldLabels() throws IOException { + final Timer.Context timer = mMetricRegistry.timer("CCBAPI.getCustomFieldLabels.time").time(); + boolean success = false; + try { + final GetCustomFieldLabelsResponse resp = mAPI.getCustomFieldLabels(); + success = true; + return resp; + } finally { + timer.stop(); + mMetricRegistry.counter("CCBAPI.getCustomFieldLabels.success").inc(success ? 1 : 0); + mMetricRegistry.counter("CCBAPI.getCustomFieldLabels.failure").inc(!success ? 1 : 0); + } + } + + @Override + public GetIndividualProfilesResponse getIndividualProfiles(GetIndividualProfilesRequest request) + throws IOException { + final Timer.Context timer = mMetricRegistry.timer("CCBAPI.getIndividualProfiles").time(); + boolean success = false; + try { + final GetIndividualProfilesResponse resp = mAPI.getIndividualProfiles(request); + mMetricRegistry.counter("CCBAPI.getCustomFieldLabels.count").inc(resp.getIndividuals().size()); + success = true; + return resp; + } finally { + timer.stop(); + mMetricRegistry.counter("CCBAPI.getCustomFieldLabels.success").inc(success ? 1 : 0); + mMetricRegistry.counter("CCBAPI.getCustomFieldLabels.failure").inc(!success ? 1 : 0); + } + } + + @Override + public void close() throws IOException { + mAPI.close(); + } +} diff --git a/src/com/p4square/grow/frontend/AssessmentResultsPage.java b/src/com/p4square/grow/frontend/AssessmentResultsPage.java index 2035ce8..9b66794 100644 --- a/src/com/p4square/grow/frontend/AssessmentResultsPage.java +++ b/src/com/p4square/grow/frontend/AssessmentResultsPage.java @@ -7,6 +7,7 @@ package com.p4square.grow.frontend; import java.util.Date; import java.util.Map; +import com.p4square.f1oauth.FellowshipOneIntegrationDriver; import freemarker.template.Template; import org.restlet.data.MediaType; @@ -117,7 +118,8 @@ public class AssessmentResultsPage extends FreeMarkerPageResource { attribute.setStartDate(new Date()); attribute.setComment(JsonEncodedProvider.MAPPER.writeValueAsString(results)); - F1API f1 = mGrowFrontend.getF1Access().getAuthenticatedApi(user); + F1API f1 = ((FellowshipOneIntegrationDriver) mGrowFrontend.getThirdPartyIntegrationFactory()) + .getF1Access().getAuthenticatedApi(user); if (!f1.addAttribute(user.getIdentifier(), attribute)) { LOG.error("addAttribute failed for " + user.getIdentifier() + " with attribute " + attributeName); diff --git a/src/com/p4square/grow/frontend/ChapterCompletePage.java b/src/com/p4square/grow/frontend/ChapterCompletePage.java index f07a870..2dd1ecf 100644 --- a/src/com/p4square/grow/frontend/ChapterCompletePage.java +++ b/src/com/p4square/grow/frontend/ChapterCompletePage.java @@ -7,6 +7,7 @@ package com.p4square.grow.frontend; import java.util.Date; import java.util.Map; +import com.p4square.f1oauth.FellowshipOneIntegrationDriver; import freemarker.template.Template; import org.restlet.data.MediaType; @@ -172,7 +173,8 @@ public class ChapterCompletePage extends FreeMarkerPageResource { Attribute attribute = new Attribute(attributeName); attribute.setStartDate(new Date()); - F1API f1 = mGrowFrontend.getF1Access().getAuthenticatedApi(user); + F1API f1 = ((FellowshipOneIntegrationDriver) mGrowFrontend.getThirdPartyIntegrationFactory()) + .getF1Access().getAuthenticatedApi(user); if (!f1.addAttribute(user.getIdentifier(), attribute)) { LOG.error("addAttribute failed for " + user.getIdentifier() + " with attribute " + attributeName); diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java index 1d221cc..b5f62fb 100644 --- a/src/com/p4square/grow/frontend/GrowFrontend.java +++ b/src/com/p4square/grow/frontend/GrowFrontend.java @@ -6,15 +6,12 @@ package com.p4square.grow.frontend; import java.io.File; import java.io.IOException; - -import java.util.Arrays; -import java.util.UUID; +import java.lang.reflect.Constructor; import freemarker.template.Template; import org.restlet.Application; import org.restlet.Component; -import org.restlet.Client; import org.restlet.Context; import org.restlet.Restlet; import org.restlet.data.Protocol; @@ -32,13 +29,11 @@ import com.p4square.fmfacade.FreeMarkerPageResource; import com.p4square.grow.config.Config; -import com.p4square.f1oauth.F1Access; -import com.p4square.f1oauth.SecondPartyVerifier; - import com.p4square.restlet.metrics.MetricRouter; import com.p4square.session.SessionCheckingAuthenticator; import com.p4square.session.SessionCreatingAuthenticator; +import org.restlet.security.Verifier; /** * This is the Restlet Application implementing the Grow project front-end. @@ -54,7 +49,7 @@ public class GrowFrontend extends FMFacade { private final Config mConfig; private final MetricRegistry mMetricRegistry; - private F1Access mHelper; + private IntegrationDriver mIntegrationFactory; public GrowFrontend() { this(new Config(), new MetricRegistry()); @@ -81,20 +76,26 @@ public class GrowFrontend extends FMFacade { FreeMarkerPageResource.baseRootObject(getContext(), this)); } + getContext().getAttributes().put("com.p4square.grow.config", mConfig); + getContext().getAttributes().put("com.p4square.grow.metrics", mMetricRegistry); + super.start(); } - synchronized F1Access getF1Access() { - if (mHelper == null) { - mHelper = new F1Access(getContext(), mConfig.getString("f1ConsumerKey", ""), - mConfig.getString("f1ConsumerSecret", ""), - mConfig.getString("f1BaseUrl", "staging.fellowshiponeapi.com"), - mConfig.getString("f1ChurchCode", "pfseawa"), - F1Access.UserType.WEBLINK); - mHelper.setMetricRegistry(mMetricRegistry); + public synchronized IntegrationDriver getThirdPartyIntegrationFactory() { + if (mIntegrationFactory == null) { + final String driverClassName = getConfig().getString("integrationDriver", + "com.p4square.f1oauth.FellowshipOneIntegrationDriver"); + try { + Class<?> clazz = Class.forName(driverClassName); + Constructor<?> constructor = clazz.getConstructor(Context.class); + mIntegrationFactory = (IntegrationDriver) constructor.newInstance(getContext()); + } catch (Exception e) { + LOG.error("Failed to instantiate IntegrationDriver " + driverClassName); + } } - return mHelper; + return mIntegrationFactory; } @Override @@ -141,8 +142,8 @@ public class GrowFrontend extends FMFacade { SessionCheckingAuthenticator sessionChk = new SessionCheckingAuthenticator(context, true); // This is used to authenticate the user - SecondPartyVerifier f1Verifier = new SecondPartyVerifier(context, getF1Access()); - LoginFormAuthenticator loginAuth = new LoginFormAuthenticator(context, false, f1Verifier); + Verifier verifier = getThirdPartyIntegrationFactory().newUserAuthenticationVerifier(); + LoginFormAuthenticator loginAuth = new LoginFormAuthenticator(context, false, verifier); loginAuth.setLoginFormUrl(loginPage); loginAuth.setLoginPostUrl(loginPost); loginAuth.setDefaultPage(defaultPage); diff --git a/src/com/p4square/grow/frontend/IntegrationDriver.java b/src/com/p4square/grow/frontend/IntegrationDriver.java new file mode 100644 index 0000000..3370116 --- /dev/null +++ b/src/com/p4square/grow/frontend/IntegrationDriver.java @@ -0,0 +1,17 @@ +package com.p4square.grow.frontend; + +import org.restlet.security.Verifier; + +/** + * An IntegrationDriver is used to create implementations of various objects + * used to integration Grow with a particular Church Management System. + */ +public interface IntegrationDriver { + + /** + * Create a new Restlet Verifier to authenticate users when they login to the site. + * + * @return A Verifier. + */ + Verifier newUserAuthenticationVerifier(); +} diff --git a/src/com/p4square/grow/frontend/NewAccountResource.java b/src/com/p4square/grow/frontend/NewAccountResource.java index 54c1790..5c13017 100644 --- a/src/com/p4square/grow/frontend/NewAccountResource.java +++ b/src/com/p4square/grow/frontend/NewAccountResource.java @@ -6,12 +6,12 @@ package com.p4square.grow.frontend; import java.util.Map; +import com.p4square.f1oauth.FellowshipOneIntegrationDriver; import freemarker.template.Template; import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Status; -import org.restlet.resource.ServerResource; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.ext.freemarker.TemplateRepresentation; @@ -44,7 +44,14 @@ public class NewAccountResource extends FreeMarkerPageResource { super.doInit(); mGrowFrontend = (GrowFrontend) getApplication(); - mHelper = mGrowFrontend.getF1Access(); + + final IntegrationDriver driver = mGrowFrontend.getThirdPartyIntegrationFactory(); + if (driver instanceof FellowshipOneIntegrationDriver) { + mHelper = ((FellowshipOneIntegrationDriver) driver).getF1Access(); + } else { + LOG.error("NewAccountResource only works with F1!"); + mHelper = null; + } mErrorMessage = ""; @@ -83,6 +90,11 @@ public class NewAccountResource extends FreeMarkerPageResource { @Override protected Representation post(Representation rep) { + if (mHelper == null) { + mErrorMessage += "F1 support is not enabled! "; + return get(); + } + Form form = new Form(rep); String firstname = form.getFirstValue("firstname"); |