This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/oidc-ownership in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git
commit 78deb5474168b9150923e75d4d15df234ea2a55b Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Sat Dec 5 15:35:24 2026 +0000 Check ownership before revoking an authorization code grant in the OIDC console --- services/oidc/pom.xml | 11 ++ .../oidc/clients/ClientRegistrationService.java | 14 ++- .../clients/ClientRegistrationServiceTest.java | 139 +++++++++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) diff --git a/services/oidc/pom.xml b/services/oidc/pom.xml index e50fe6e3..dd76cc13 100644 --- a/services/oidc/pom.xml +++ b/services/oidc/pom.xml @@ -78,6 +78,17 @@ <artifactId>commons-text</artifactId> <version>${commons.text.version}</version> </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <version>${junit.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> <plugins> diff --git a/services/oidc/src/main/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationService.java b/services/oidc/src/main/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationService.java index 696b6941..d4746c78 100644 --- a/services/oidc/src/main/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationService.java +++ b/services/oidc/src/main/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationService.java @@ -264,7 +264,19 @@ public class ClientRegistrationService { checkSecurityContext(); if (dataProvider instanceof AuthorizationCodeDataProvider) { - ((AuthorizationCodeDataProvider)dataProvider).removeCodeGrant(code); + Client c = getRegisteredClient(id); + if (c == null) { + throw new InvalidRegistrationException("The client id is invalid"); + } + // Only remove the code grant if it was issued to this client for the current user + AuthorizationCodeDataProvider codeDataProvider = (AuthorizationCodeDataProvider)dataProvider; + UserSubject subject = new OidcUserSubject(getUserName()); + for (ServerAuthorizationCodeGrant codeGrant : codeDataProvider.getCodeGrants(c, subject)) { + if (codeGrant.getCode() != null && codeGrant.getCode().equals(code)) { + codeDataProvider.removeCodeGrant(code); + break; + } + } return getClientCodeGrants(id); } return null; diff --git a/services/oidc/src/test/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationServiceTest.java b/services/oidc/src/test/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationServiceTest.java new file mode 100644 index 00000000..0974dfae --- /dev/null +++ b/services/oidc/src/test/java/org/apache/cxf/fediz/service/oidc/clients/ClientRegistrationServiceTest.java @@ -0,0 +1,139 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.fediz.service.oidc.clients; + +import java.lang.reflect.Proxy; +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.ws.rs.core.SecurityContext; + +import org.apache.cxf.fediz.service.oidc.CSRFUtils; +import org.apache.cxf.jaxrs.ext.MessageContext; +import org.apache.cxf.rs.security.oauth2.common.Client; +import org.apache.cxf.rs.security.oauth2.common.UserSubject; +import org.apache.cxf.rs.security.oauth2.grants.code.AuthorizationCodeDataProvider; +import org.apache.cxf.rs.security.oauth2.grants.code.ServerAuthorizationCodeGrant; +import org.apache.cxf.rs.security.oauth2.provider.ClientRegistrationProvider; +import org.springframework.mock.web.MockHttpServletRequest; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ClientRegistrationServiceTest { + + private static final String CSRF_TOKEN = "csrf-token"; + + private final List<String> removedCodes = new ArrayList<>(); + + @Test + public void testRevokeOwnCodeGrant() { + ClientRegistrationService service = createService("alice"); + service.revokeClientCodeGrant("alice-client", "alice-code", CSRF_TOKEN); + assertEquals(Collections.singletonList("alice-code"), removedCodes); + } + + @Test + public void testRevokeCodeGrantOfAnotherUserIsIgnored() { + ClientRegistrationService service = createService("alice"); + // A code issued to a different client/user must not be removed via alice's client + service.revokeClientCodeGrant("alice-client", "bob-code", CSRF_TOKEN); + assertTrue(removedCodes.isEmpty()); + } + + @Test + public void testRevokeCodeGrantForUnownedClientIsRejected() { + ClientRegistrationService service = createService("alice"); + assertThrows(InvalidRegistrationException.class, + () -> service.revokeClientCodeGrant("bob-client", "bob-code", CSRF_TOKEN)); + assertTrue(removedCodes.isEmpty()); + } + + private ClientRegistrationService createService(String userName) { + Client aliceClient = createClient("alice-client", "alice"); + Client bobClient = createClient("bob-client", "bob"); + List<ServerAuthorizationCodeGrant> grants = new ArrayList<>(); + grants.add(createGrant("alice-code", aliceClient, "alice")); + grants.add(createGrant("bob-code", bobClient, "bob")); + + ClientRegistrationService service = new ClientRegistrationService(); + service.setClientProvider(proxy(ClientRegistrationProvider.class, (method, args) -> + "getClients".equals(method) ? List.of(aliceClient, bobClient) : null)); + service.setDataProvider(proxy(AuthorizationCodeDataProvider.class, (method, args) -> { + if ("getCodeGrants".equals(method)) { + Client c = (Client)args[0]; + UserSubject subject = (UserSubject)args[1]; + List<ServerAuthorizationCodeGrant> result = new ArrayList<>(); + for (ServerAuthorizationCodeGrant grant : grants) { + if (grant.getClient().getClientId().equals(c.getClientId()) + && grant.getSubject().getLogin().equals(subject.getLogin())) { + result.add(grant); + } + } + return result; + } else if ("removeCodeGrant".equals(method)) { + removedCodes.add((String)args[0]); + } + return null; + })); + service.init(); + + MockHttpServletRequest request = new MockHttpServletRequest(); + request.getSession().setAttribute(CSRFUtils.CSRF_TOKEN, CSRF_TOKEN); + SecurityContext sc = proxy(SecurityContext.class, (method, args) -> { + if ("getUserPrincipal".equals(method)) { + return (Principal)() -> userName; + } + return "isUserInRole".equals(method) ? Boolean.TRUE : null; + }); + service.setMessageContext(proxy(MessageContext.class, (method, args) -> { + if ("getSecurityContext".equals(method)) { + return sc; + } + return "getHttpServletRequest".equals(method) ? request : null; + })); + return service; + } + + private static Client createClient(String clientId, String owner) { + Client client = new Client(clientId, "secret", true, clientId); + client.setResourceOwnerSubject(new UserSubject(owner)); + return client; + } + + private static ServerAuthorizationCodeGrant createGrant(String code, Client client, String user) { + ServerAuthorizationCodeGrant grant = new ServerAuthorizationCodeGrant(client, code, 600L, 0L); + grant.setSubject(new UserSubject(user)); + return grant; + } + + private interface Handler { + Object invoke(String method, Object[] args); + } + + private static <T> T proxy(Class<T> type, Handler handler) { + return type.cast(Proxy.newProxyInstance(ClientRegistrationServiceTest.class.getClassLoader(), + new Class<?>[] {type}, (p, method, args) -> handler.invoke(method.getName(), args))); + } +}
