Skip to content

Commit

Permalink
fix: ext related
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 18, 2024
1 parent bd0a918 commit b791bf4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/main/java/io/supertokens/oauth/OAuthToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public static String reSignToken(AppIdentifier appIdentifier, Main main, String
payload.addProperty("iss", iss);
payload.addProperty("stt", tokenType.getValue());

if (tokenType == TokenType.ACCESS_TOKEN) {
// we need to move rsub, tId and sessionHandle from ext to root
Transformations.transformExt(payload);
}

if (payloadUpdate != null) {
for (Map.Entry<String, JsonElement> entry : payloadUpdate.entrySet()) {
if (!NON_OVERRIDABLE_TOKEN_PROPS.contains(entry.getKey())) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/supertokens/oauth/Transformations.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -21,6 +22,7 @@
import io.supertokens.utils.Utils;

public class Transformations {
private static Set<String> EXT_PROPS = Set.of("rsub", "tId", "sessionHandle");

public static Map<String, String> transformRequestHeadersForHydra(Map<String, String> requestHeaders) {
if (requestHeaders == null) {
Expand Down Expand Up @@ -204,4 +206,20 @@ public static JsonObject transformJsonForHydra(JsonObject jsonInput) {
}
return transformedJsonInput;
}

public static void transformExt(JsonObject payload) {
if (payload.has("ext")) {
JsonObject ext = payload.get("ext").getAsJsonObject();
for (String prop : EXT_PROPS) {
if (ext.has(prop)) {
payload.addProperty(prop, ext.get(prop).getAsString());
ext.remove(prop);
}
}

if (ext.entrySet().size() == 0) {
payload.remove("ext");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.supertokens.jwt.exceptions.UnsupportedJWTSigningAlgorithmException;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.oauth.OAuth;
import io.supertokens.oauth.Transformations;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
Expand Down Expand Up @@ -76,13 +77,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws I
JsonObject response = jsonBody.getAsJsonObject();

response.addProperty("iss", iss);
if (response.has("ext")) {
JsonObject ext = response.get("ext").getAsJsonObject();
for (Map.Entry<String, JsonElement> entry : ext.entrySet()) {
response.add(entry.getKey(), entry.getValue());
}
response.remove("ext");
}
Transformations.transformExt(response);

response.addProperty("status", "OK");
return response;
Expand Down

0 comments on commit b791bf4

Please sign in to comment.