Skip to content

Commit

Permalink
Added SsoToken class with common code for retrieving token info from …
Browse files Browse the repository at this point in the history
…API response.
  • Loading branch information
OrangeAndGreen committed Dec 4, 2024
1 parent 445ec10 commit b4dad76
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.commcare.android.database.connect.models;

import org.commcare.android.storage.framework.Persisted;
import org.commcare.connect.network.SsoToken;
import org.commcare.models.framework.Persisting;
import org.commcare.modern.database.Table;
import org.commcare.modern.models.MetaField;
Expand Down Expand Up @@ -111,9 +112,9 @@ public Date getHqTokenExpiration() {
return hqTokenExpiration;
}

public void updateHqToken(String token, Date expirationDate) {
hqToken = token;
hqTokenExpiration = expirationDate;
public void updateHqToken(SsoToken token) {
hqToken = token.token;
hqTokenExpiration = token.expiration;
}

public boolean getConnectIdLinked() { return connectIdLinked; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.commcare.android.storage.framework.Persisted;
import org.commcare.connect.ConnectConstants;
import org.commcare.connect.network.SsoToken;
import org.commcare.core.network.AuthInfo;
import org.commcare.models.framework.Persisting;
import org.commcare.modern.database.Table;
Expand Down Expand Up @@ -190,9 +191,9 @@ public boolean shouldRequireSecondaryPhoneVerification() {
return (new Date()).after(verifySecondaryPhoneByDate);
}

public void updateConnectToken(String token, Date expirationDate) {
connectToken = token;
connectTokenExpiration = expirationDate;
public void updateConnectToken(SsoToken token) {
connectToken = token.token;
connectTokenExpiration = token.expiration;
}

public AuthInfo.TokenAuth getConnectToken() {
Expand Down
5 changes: 3 additions & 2 deletions app/src/org/commcare/connect/ConnectDatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.commcare.android.database.connect.models.ConnectPaymentUnitRecord;
import org.commcare.android.database.connect.models.ConnectUserRecord;
import org.commcare.android.database.global.models.ConnectKeyRecord;
import org.commcare.connect.network.SsoToken;
import org.commcare.dalvik.R;
import org.commcare.models.database.AndroidDbHelper;
import org.commcare.models.database.SqlStorage;
Expand Down Expand Up @@ -248,13 +249,13 @@ public static void storeApp(Context context, ConnectLinkedAppRecord record) {
getConnectStorage(context, ConnectLinkedAppRecord.class).write(record);
}

public static void storeHqToken(Context context, String appId, String userId, String token, Date expiration) {
public static void storeHqToken(Context context, String appId, String userId, SsoToken token) {
ConnectLinkedAppRecord record = getAppData(context, appId, userId);
if (record == null) {
record = new ConnectLinkedAppRecord(appId, userId, false, "");
}

record.updateHqToken(token, expiration);
record.updateHqToken(token);

getConnectStorage(context, ConnectLinkedAppRecord.class).write(record);
}
Expand Down
45 changes: 13 additions & 32 deletions app/src/org/commcare/connect/network/ApiConnectId.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
Expand Down Expand Up @@ -71,22 +72,12 @@ public static AuthInfo.TokenAuth retrieveHqTokenApi(Context context, String hqUs
API_VERSION_NONE, new AuthInfo.NoAuth(), params, true, false);
if (postResult.responseCode == 200) {
try {
String responseAsString = new String(StreamsUtil.inputStreamToByteArray(
postResult.responseStream));
JSONObject json = new JSONObject(responseAsString);
String key = ConnectConstants.CONNECT_KEY_TOKEN;
if (json.has(key)) {
String token = json.getString(key);
Date expiration = new Date();
key = ConnectConstants.CONNECT_KEY_EXPIRES;
int seconds = json.has(key) ? json.getInt(key) : 0;
expiration.setTime(expiration.getTime() + ((long)seconds * 1000));

String seatedAppId = CommCareApplication.instance().getCurrentApp().getUniqueId();
ConnectDatabaseHelper.storeHqToken(context, seatedAppId, hqUsername, token, expiration);

return new AuthInfo.TokenAuth(token);
}
SsoToken token = SsoToken.fromResponseStream(postResult.responseStream);

String seatedAppId = CommCareApplication.instance().getCurrentApp().getUniqueId();
ConnectDatabaseHelper.storeHqToken(context, seatedAppId, hqUsername, token);

return new AuthInfo.TokenAuth(token.token);
} catch (IOException | JSONException e) {
Logger.exception("Parsing return from HQ OIDC call", e);
}
Expand Down Expand Up @@ -130,22 +121,12 @@ public static AuthInfo.TokenAuth retrieveConnectIdTokenSync(Context context) {
API_VERSION_CONNECT_ID, new AuthInfo.NoAuth(), params, true, false);
if (postResult.responseCode == 200) {
try {
String responseAsString = new String(StreamsUtil.inputStreamToByteArray(
postResult.responseStream));
postResult.responseStream.close();
JSONObject json = new JSONObject(responseAsString);
String key = ConnectConstants.CONNECT_KEY_TOKEN;
if (json.has(key)) {
String token = json.getString(key);
Date expiration = new Date();
key = ConnectConstants.CONNECT_KEY_EXPIRES;
int seconds = json.has(key) ? json.getInt(key) : 0;
expiration.setTime(expiration.getTime() + ((long)seconds * 1000));
user.updateConnectToken(token, expiration);
ConnectDatabaseHelper.storeUser(context, user);

return new AuthInfo.TokenAuth(token);
}
SsoToken token = SsoToken.fromResponseStream(postResult.responseStream);

user.updateConnectToken(token);
ConnectDatabaseHelper.storeUser(context, user);

return new AuthInfo.TokenAuth(token.token);
} catch (IOException | JSONException e) {
Logger.exception("Parsing return from Connect OIDC call", e);
}
Expand Down
38 changes: 38 additions & 0 deletions app/src/org/commcare/connect/network/SsoToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.commcare.connect.network;

import org.commcare.connect.ConnectConstants;
import org.javarosa.core.io.StreamsUtil;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class SsoToken {
public String token;
public Date expiration;

public SsoToken(String token, Date expiration) {
this.token = token;
this.expiration = expiration;
}

public static SsoToken fromResponseStream(InputStream stream) throws IOException, JSONException {
String responseAsString = new String(StreamsUtil.inputStreamToByteArray(
stream));
JSONObject json = new JSONObject(responseAsString);
String key = ConnectConstants.CONNECT_KEY_TOKEN;

if (!json.has(key)) {
throw new RuntimeException("SSO API response missing access token");
}
String token = json.getString(key);
Date expiration = new Date();
key = ConnectConstants.CONNECT_KEY_EXPIRES;
int seconds = json.has(key) ? json.getInt(key) : 0;
expiration.setTime(expiration.getTime() + ((long)seconds * 1000));

return new SsoToken(token, expiration);
}
}

0 comments on commit b4dad76

Please sign in to comment.