Skip to content

Commit

Permalink
fix: revoke by client_id
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 19, 2024
1 parent 9e3caf5 commit a529072
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 92 deletions.
8 changes: 2 additions & 6 deletions src/main/java/io/supertokens/oauth/OAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,9 @@ public static JsonObject introspectAccessToken(Main main, AppIdentifier appIdent
return result;
}

public static void revokeAllConsentSessions(Main main, AppIdentifier appIdentifier, Storage storage, String subject, String clientId) throws StorageQueryException {
public static void revokeTokensForClientId(Main main, AppIdentifier appIdentifier, Storage storage, String clientId) throws StorageQueryException {
OAuthStorage oauthStorage = StorageUtils.getOAuthStorage(storage);
if (clientId == null) {
oauthStorage.revoke(appIdentifier, "sub", subject);
} else {
oauthStorage.revoke(appIdentifier, "client_id_sub", clientId + ":" + subject);
}
oauthStorage.revoke(appIdentifier, "client_id", clientId);
}

public static void revokeRefreshToken(Main main, AppIdentifier appIdentifier, Storage storage, String token) throws StorageQueryException, NoSuchAlgorithmException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/supertokens/webserver/Webserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ private void setupRoutes() {
addAPI(new OAuthTokenIntrospectAPI(main));

addAPI(new RevokeOAuthTokenAPI(main));
addAPI(new RevokeOAuthConsentSessionsAPI(main));
addAPI(new RevokeOAuthTokensAPI(main));
addAPI(new RevokeOAuthSessionAPI(main));

StandardContext context = tomcatReference.getContext();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.supertokens.webserver.api.oauth;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.JsonObject;

import io.supertokens.Main;
import io.supertokens.multitenancy.exception.BadPermissionException;
import io.supertokens.oauth.HttpRequestForOry;
import io.supertokens.oauth.OAuth;
import io.supertokens.pluginInterface.RECIPE_ID;
import io.supertokens.pluginInterface.Storage;
import io.supertokens.pluginInterface.exceptions.StorageQueryException;
import io.supertokens.pluginInterface.multitenancy.AppIdentifier;
import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException;
import io.supertokens.webserver.InputParser;
import io.supertokens.webserver.WebserverAPI;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

public class RevokeOAuthTokensAPI extends WebserverAPI {

public RevokeOAuthTokensAPI(Main main){
super(main, RECIPE_ID.OAUTH.toString());
}

@Override
public String getPath() {
return "/recipe/oauth/tokens/revoke";
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
JsonObject input = InputParser.parseJsonObjectOrThrowError(req);
String clientId = InputParser.parseStringOrThrowError(input, "client_id", false);

try {
AppIdentifier appIdentifier = getAppIdentifier(req);
Storage storage = enforcePublicTenantAndGetPublicTenantStorage(req);

OAuth.revokeTokensForClientId(main, appIdentifier, storage, clientId);

Map<String, String> queryParams = new HashMap<>();
queryParams.put("client_id", clientId);

HttpRequestForOry.Response response = OAuthProxyHelper.proxyJsonDELETE(
main, req, resp,
appIdentifier,
storage,
null, // clientIdToCheck
"/admin/oauth2/tokens", // proxyPath
true, // proxyToAdmin
false, // camelToSnakeCaseConversion
queryParams, // queryParams
new JsonObject(), // jsonInput
new HashMap<>() // headers
);

if (response != null) {
response.jsonResponse.getAsJsonObject().addProperty("status", "OK");
super.sendJsonResponse(200, response.jsonResponse, resp);
}
} catch (IOException | TenantOrAppNotFoundException | BadPermissionException | StorageQueryException e) {
throw new ServletException(e);
}
}
}

0 comments on commit a529072

Please sign in to comment.