Skip to content

Commit

Permalink
oauth2 context updates
Browse files Browse the repository at this point in the history
  • Loading branch information
VSydor committed Aug 2, 2023
1 parent deacb4a commit 7ee0a49
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 298 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.impactupgrade.nucleus.client;

import com.impactupgrade.nucleus.dao.HibernateDao;
import com.impactupgrade.nucleus.entity.Organization;
import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.util.OAuth2;
import org.json.JSONObject;

public class OrgConfiguredClient {

protected final Environment env;
protected final HibernateDao<Long, Organization> organizationDao;

public OrgConfiguredClient(Environment env) {
this.env = env;
this.organizationDao = new HibernateDao<>(Organization.class);
}

protected JSONObject getEnvJson() {
return getOrganization(env.getConfig().apiKey).getEnvironmentJson();
}

protected void updateEnvJson(String clientConfigKey, OAuth2.Context context) {
Organization org = getOrganization(env.getConfig().apiKey);
JSONObject envJson = org.getEnvironmentJson();
JSONObject clientConfigJson = envJson.getJSONObject(clientConfigKey);

clientConfigJson.put("accessToken", context.accessToken());
clientConfigJson.put("expiresAt", context.expiresAt() != null ? context.expiresAt() : null);
clientConfigJson.put("refreshToken", context.refreshToken());

org.setEnvironmentJson(envJson);
organizationDao.update(org);
}

private Organization getOrganization(String apiKey) {
return organizationDao.getQueryResult(
"from Organization o where o.nucleusApiKey=:apiKey",
query -> query.setParameter("apiKey", apiKey)
).get();
}
}
39 changes: 7 additions & 32 deletions src/main/java/com/impactupgrade/nucleus/client/RaiselyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.impactupgrade.nucleus.dao.HibernateDao;
import com.impactupgrade.nucleus.entity.Organization;
import com.impactupgrade.nucleus.environment.Environment;
import com.impactupgrade.nucleus.environment.EnvironmentConfig;
import com.impactupgrade.nucleus.util.HttpClient;
Expand All @@ -15,38 +13,23 @@

import static com.impactupgrade.nucleus.util.HttpClient.get;

public class RaiselyClient {
public class RaiselyClient extends OrgConfiguredClient {

private static final String RAISELY_API_URL = "https://api.raisely.com/v3";
private static final String AUTH_URL = RAISELY_API_URL + "/login";

protected final Environment env;
protected HibernateDao<Long, Organization> organizationDao;

private final OAuth2.Context oAuth2Context;

public RaiselyClient(Environment env) {
this.env = env;
this.organizationDao = new HibernateDao<>(Organization.class);
super(env);

Organization org = getOrganization();
JSONObject envJson = org.getEnvironmentJson();
JSONObject raiselyJson = envJson.getJSONObject("raisely");
JSONObject raiselyJson = getEnvJson().getJSONObject("raisely");

this.oAuth2Context = new OAuth2.UsernamePasswordContext(
env.getConfig().raisely.username, env.getConfig().raisely.password, Map.of("requestAdminToken", "true"),
raiselyJson.getString("accessToken"), raiselyJson.getLong("expiresAt"), raiselyJson.getString("refreshToken"), AUTH_URL);
}

protected Organization getOrganization() {
return organizationDao.getQueryResult(
"from Organization o where o.nucleusApiKey=:apiKey",
query -> {
query.setParameter("apiKey", env.getConfig().apiKey);
}
).get();
}

//*Note this uses the donation ID from the Stripe metadata. Different from the donation UUID
/*
* From Raisely Support:
Expand All @@ -73,19 +56,11 @@ public RaiselyClient.Donation getDonation(String donationId) {
}

protected HttpClient.HeaderBuilder headers() {
String currentAccessToken = oAuth2Context.accessToken();
if (currentAccessToken != oAuth2Context.refresh().accessToken()) {
Organization org = getOrganization();
JSONObject envJson = org.getEnvironmentJson();
JSONObject raisely = envJson.getJSONObject("raisely");

raisely.put("accessToken", oAuth2Context.accessToken());
raisely.put("expiresAt", oAuth2Context.expiresAt() != null ? oAuth2Context.expiresAt() : null);
raisely.put("refreshToken", oAuth2Context.refreshToken());
org.setEnvironmentJson(envJson);
organizationDao.update(org);
String accessToken = oAuth2Context.accessToken();
if (oAuth2Context.refresh().accessToken() != accessToken) {
// tokens updated - need to update config in db
updateEnvJson("raisely", oAuth2Context);
}

return HttpClient.HeaderBuilder.builder().authBearerToken(oAuth2Context.accessToken());
}

Expand Down
47 changes: 12 additions & 35 deletions src/main/java/com/impactupgrade/nucleus/client/SpokeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,23 @@
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;

// TODO: To eventually become a spoke-phone-java-client open source lib?
public class SpokeClient {
public class SpokeClient extends OrgConfiguredClient {

protected static String AUTH_ENDPOINT = "https://auth.spokephone.com/oauth/token";
protected static String API_ENDPOINT_BASE = "https://integration.spokephone.com/";

protected final Environment env;
protected HibernateDao<Long, Organization> organizationDao;

private final OAuth2.Context oAuth2Context;

public SpokeClient(Environment env) {
this.env = env;
this.organizationDao = new HibernateDao<>(Organization.class);
super(env);

Organization org = getOrganization();
JSONObject envJson = org.getEnvironmentJson();
JSONObject spokeJson = envJson.getJSONObject("spoke");
JSONObject spokeJson = getEnvJson().getJSONObject("spoke");

this.oAuth2Context = new OAuth2.ClientCredentialsContext(
env.getConfig().spoke.clientId, env.getConfig().spoke.clientSecret,
spokeJson.getString("accessToken"), spokeJson.getLong("expiresAt"), spokeJson.getString("refreshToken"), AUTH_ENDPOINT);
}

protected Organization getOrganization() {
return organizationDao.getQueryResult(
"from Organization o where o.nucleusApiKey=:apiKey",
query -> {
query.setParameter("apiKey", env.getConfig().apiKey);
}
).get();
}

public List<Phonebook> getPhonebooks() {
return get(API_ENDPOINT_BASE + "phonebooks", headers(), new GenericType<>() {});
}
Expand Down Expand Up @@ -100,6 +85,15 @@ protected Contact toSpokeContact(CrmContact crmContact, String crmName) {
return contact;
}

protected HttpClient.HeaderBuilder headers() {
String accessToken = oAuth2Context.accessToken();
if (oAuth2Context.refresh().accessToken() != accessToken) {
// tokens updated - need to update config in db
updateEnvJson("spoke", oAuth2Context);
}
return HttpClient.HeaderBuilder.builder().authBearerToken(oAuth2Context.accessToken());
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Phonebook {
public String id;
Expand Down Expand Up @@ -129,23 +123,6 @@ public static class ContactRequest {
public String countryIso;
}

protected HttpClient.HeaderBuilder headers() {
String currentAccessToken = oAuth2Context.accessToken();
if (currentAccessToken != oAuth2Context.refresh().accessToken()) {
Organization org = getOrganization();
JSONObject envJson = org.getEnvironmentJson();
JSONObject spokeJson = envJson.getJSONObject("spoke");

spokeJson.put("accessToken", oAuth2Context.accessToken());
spokeJson.put("expiresAt", oAuth2Context.expiresAt() != null ? oAuth2Context.expiresAt() : null);
spokeJson.put("refreshToken", oAuth2Context.refreshToken());
org.setEnvironmentJson(envJson);
organizationDao.update(org);
}

return HttpClient.HeaderBuilder.builder().authBearerToken(oAuth2Context.accessToken());
}

//TODO: remove once done with testing
public static void main(String[] args) {
Environment env = new Environment() {
Expand Down
Loading

0 comments on commit 7ee0a49

Please sign in to comment.