-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add revokeToken() to OAuth2Client (RFC 7009) #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * Copyright 2026 The Horde Project (http://www.horde.org/) | ||
| * | ||
| * See the enclosed file LICENSE for license information (BSD). If you | ||
| * did not receive this file, see http://www.horde.org/licenses/bsd. | ||
| * | ||
| * @author Jean Charles Delépine <jean.charles.delepine@u-picardie.fr> | ||
| */ | ||
|
|
||
| namespace Horde\OAuth\Test\Client; | ||
|
|
||
| use Horde\OAuth\Client\OAuth2Client; | ||
| use Horde\OAuth\Client\ProviderConfig; | ||
| use Horde\OAuth\Exception\OAuthException; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Psr\Http\Client\ClientInterface; | ||
| use Psr\Http\Message\RequestFactoryInterface; | ||
| use Psr\Http\Message\RequestInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\StreamFactoryInterface; | ||
| use Psr\Http\Message\StreamInterface; | ||
|
|
||
| #[CoversClass(OAuth2Client::class)] | ||
| final class OAuth2ClientTest extends TestCase | ||
| { | ||
| private ProviderConfig $provider; | ||
| private RequestInterface $request; | ||
| private StreamInterface $stream; | ||
| private StreamFactoryInterface $streamFactory; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->provider = ProviderConfig::fromArray([ | ||
| 'issuer' => 'https://idp.example.org', | ||
| 'authorization_endpoint' => 'https://idp.example.org/authorize', | ||
| 'token_endpoint' => 'https://idp.example.org/token', | ||
| 'revocation_endpoint' => 'https://idp.example.org/revoke', | ||
| ]); | ||
|
|
||
| $this->stream = $this->createStub(StreamInterface::class); | ||
| $this->request = $this->createStub(RequestInterface::class); | ||
| $this->streamFactory = $this->createStub(StreamFactoryInterface::class); | ||
|
|
||
| $this->request->method('withHeader')->willReturnSelf(); | ||
| $this->request->method('withBody')->willReturnSelf(); | ||
| $this->streamFactory->method('createStream')->willReturn($this->stream); | ||
| } | ||
|
|
||
| private function makeClient( | ||
| ClientInterface $httpClient, | ||
| RequestFactoryInterface $requestFactory, | ||
| ?string $clientSecret = 'secret', | ||
| ?ProviderConfig $provider = null, | ||
| ): OAuth2Client { | ||
| return new OAuth2Client( | ||
| provider: $provider ?? $this->provider, | ||
| clientId: 'test-client', | ||
| clientSecret: $clientSecret, | ||
| redirectUri: 'https://horde.example.org/callback', | ||
| httpClient: $httpClient, | ||
| requestFactory: $requestFactory, | ||
| streamFactory: $this->streamFactory, | ||
| ); | ||
| } | ||
|
|
||
| public function testRevokeTokenSendsPostToRevocationEndpoint(): void | ||
| { | ||
| $requestFactory = $this->createMock(RequestFactoryInterface::class); | ||
| $requestFactory | ||
| ->expects(self::once()) | ||
| ->method('createRequest') | ||
| ->with('POST', 'https://idp.example.org/revoke') | ||
| ->willReturn($this->request); | ||
|
|
||
| $response = $this->createStub(ResponseInterface::class); | ||
| $response->method('getStatusCode')->willReturn(200); | ||
|
|
||
| $httpClient = $this->createMock(ClientInterface::class); | ||
| $httpClient | ||
| ->expects(self::once()) | ||
| ->method('sendRequest') | ||
| ->willReturn($response); | ||
|
|
||
| $this->makeClient($httpClient, $requestFactory)->revokeToken('my-access-token'); | ||
| } | ||
|
|
||
| public function testRevokeTokenIncludesTokenInBody(): void | ||
| { | ||
| $requestFactory = $this->createStub(RequestFactoryInterface::class); | ||
| $requestFactory->method('createRequest')->willReturn($this->request); | ||
|
|
||
| $capturedBody = null; | ||
| $streamFactory = $this->createStub(StreamFactoryInterface::class); | ||
| $streamFactory->method('createStream') | ||
| ->willReturnCallback(function (string $body) use (&$capturedBody): StreamInterface { | ||
| $capturedBody = $body; | ||
| return $this->stream; | ||
| }); | ||
|
|
||
| $response = $this->createStub(ResponseInterface::class); | ||
| $response->method('getStatusCode')->willReturn(200); | ||
|
|
||
| $httpClient = $this->createStub(ClientInterface::class); | ||
| $httpClient->method('sendRequest')->willReturn($response); | ||
|
|
||
| $client = new OAuth2Client( | ||
| provider: $this->provider, | ||
| clientId: 'test-client', | ||
| clientSecret: 'secret', | ||
| redirectUri: 'https://horde.example.org/callback', | ||
| httpClient: $httpClient, | ||
| requestFactory: $requestFactory, | ||
| streamFactory: $streamFactory, | ||
| ); | ||
|
|
||
| $client->revokeToken('my-access-token', 'access_token'); | ||
|
|
||
| self::assertStringContainsString('token=my-access-token', $capturedBody); | ||
| self::assertStringContainsString('token_type_hint=access_token', $capturedBody); | ||
| self::assertStringContainsString('client_id=test-client', $capturedBody); | ||
| self::assertStringContainsString('client_secret=secret', $capturedBody); | ||
| } | ||
|
|
||
| public function testRevokeTokenWithDefaultHintIsAccessToken(): void | ||
| { | ||
| $requestFactory = $this->createStub(RequestFactoryInterface::class); | ||
| $requestFactory->method('createRequest')->willReturn($this->request); | ||
|
|
||
| $capturedBody = null; | ||
| $streamFactory = $this->createStub(StreamFactoryInterface::class); | ||
| $streamFactory->method('createStream') | ||
| ->willReturnCallback(function (string $body) use (&$capturedBody): StreamInterface { | ||
| $capturedBody = $body; | ||
| return $this->stream; | ||
| }); | ||
|
|
||
| $response = $this->createStub(ResponseInterface::class); | ||
| $response->method('getStatusCode')->willReturn(200); | ||
|
|
||
| $httpClient = $this->createStub(ClientInterface::class); | ||
| $httpClient->method('sendRequest')->willReturn($response); | ||
|
|
||
| $client = new OAuth2Client( | ||
| provider: $this->provider, | ||
| clientId: 'test-client', | ||
| clientSecret: 'secret', | ||
| redirectUri: 'https://horde.example.org/callback', | ||
| httpClient: $httpClient, | ||
| requestFactory: $requestFactory, | ||
| streamFactory: $streamFactory, | ||
| ); | ||
|
|
||
| $client->revokeToken('my-token'); | ||
|
|
||
| self::assertStringContainsString('token_type_hint=access_token', $capturedBody); | ||
| } | ||
|
|
||
| public function testRevokeTokenWithoutClientSecretOmitsIt(): void | ||
| { | ||
| $requestFactory = $this->createStub(RequestFactoryInterface::class); | ||
| $requestFactory->method('createRequest')->willReturn($this->request); | ||
|
|
||
| $capturedBody = null; | ||
| $streamFactory = $this->createStub(StreamFactoryInterface::class); | ||
| $streamFactory->method('createStream') | ||
| ->willReturnCallback(function (string $body) use (&$capturedBody): StreamInterface { | ||
| $capturedBody = $body; | ||
| return $this->stream; | ||
| }); | ||
|
|
||
| $response = $this->createStub(ResponseInterface::class); | ||
| $response->method('getStatusCode')->willReturn(200); | ||
|
|
||
| $httpClient = $this->createStub(ClientInterface::class); | ||
| $httpClient->method('sendRequest')->willReturn($response); | ||
|
|
||
| $client = new OAuth2Client( | ||
| provider: $this->provider, | ||
| clientId: 'test-client', | ||
| clientSecret: null, | ||
| redirectUri: 'https://horde.example.org/callback', | ||
| httpClient: $httpClient, | ||
| requestFactory: $requestFactory, | ||
| streamFactory: $streamFactory, | ||
| ); | ||
|
|
||
| $client->revokeToken('my-token'); | ||
|
|
||
| self::assertStringNotContainsString('client_secret', $capturedBody); | ||
| } | ||
|
|
||
| public function testRevokeTokenThrowsWhenNoRevocationEndpoint(): void | ||
| { | ||
| $provider = ProviderConfig::fromArray([ | ||
| 'issuer' => 'https://idp.example.org', | ||
| 'authorization_endpoint' => 'https://idp.example.org/authorize', | ||
| 'token_endpoint' => 'https://idp.example.org/token', | ||
| ]); | ||
|
|
||
| $httpClient = $this->createStub(ClientInterface::class); | ||
| $requestFactory = $this->createStub(RequestFactoryInterface::class); | ||
|
|
||
| $client = new OAuth2Client( | ||
| provider: $provider, | ||
| clientId: 'test-client', | ||
| clientSecret: 'secret', | ||
| redirectUri: 'https://horde.example.org/callback', | ||
| httpClient: $httpClient, | ||
| requestFactory: $requestFactory, | ||
| streamFactory: $this->streamFactory, | ||
| ); | ||
|
|
||
| $this->expectException(OAuthException::class); | ||
| $client->revokeToken('my-token'); | ||
| } | ||
|
|
||
| public function testRevokeTokenThrowsOnErrorResponse(): void | ||
| { | ||
| $requestFactory = $this->createStub(RequestFactoryInterface::class); | ||
| $requestFactory->method('createRequest')->willReturn($this->request); | ||
|
|
||
| $body = $this->createStub(StreamInterface::class); | ||
| $body->method('__toString')->willReturn('{"error":"invalid_token","error_description":"Token expired"}'); | ||
|
|
||
| $response = $this->createStub(ResponseInterface::class); | ||
| $response->method('getStatusCode')->willReturn(400); | ||
| $response->method('getBody')->willReturn($body); | ||
|
|
||
| $httpClient = $this->createStub(ClientInterface::class); | ||
| $httpClient->method('sendRequest')->willReturn($response); | ||
|
|
||
| $this->expectException(OAuthException::class); | ||
| $this->makeClient($httpClient, $requestFactory)->revokeToken('bad-token'); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a bug but:
RFC 7009 §2.1 only says "the client also includes its authentication credentials" without prescribing the method, so this is spec-compliant.
Providers that advertise basic only on the revocation endpoint will reject the request.
Should we check tokenEndpointAuthMethodsSupported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I should have reused the same logic as
tokenRequest().The revocation endpoint often shares the same auth method as the token
endpoint. I'll apply the same
tokenEndpointAuthMethodsSupportedcheck:use HTTP Basic when
client_secret_basicis advertised exclusively,falling back to
client_secret_postotherwise.