-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for PKCE for OAuth2 #36
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,62 @@ | ||
| <?php namespace web\auth\oauth; | ||
|
|
||
| use lang\IllegalArgumentException; | ||
|
|
||
| /** @test web.auth.unittest.ByPKCETest */ | ||
| class ByPKCE extends Credentials { | ||
| const SUPPORTED= ['S256', 'plain']; | ||
|
|
||
| private $challenge, $method; | ||
|
|
||
| /** | ||
| * Creates credentials with a client ID and method. | ||
| * Support the `S256` and `plain` methods. | ||
| * | ||
| * @param string $clientId | ||
| * @param string $method | ||
| * @throws lang.IllegalArgumentException | ||
| */ | ||
| public function __construct($clientId, $method) { | ||
| parent::__construct($clientId); | ||
|
|
||
| if ('S256' === $method) { | ||
| $this->challenge= fn($verifier) => JWT::encode(hash('sha256', $verifier, true)); | ||
| } else if ('plain' === $method) { | ||
| $this->challenge= fn($verifier) => $verifier; | ||
| } else { | ||
| throw new IllegalArgumentException('Unsupported method '.$method.', expected one of ['.implode(', ', self::SUPPORTED).']'); | ||
| } | ||
| $this->method= $method; | ||
| } | ||
|
|
||
| /** @return string */ | ||
| public function method() { return $this->method; } | ||
|
|
||
| /** Returns authorization seed */ | ||
| public function seed(): array { | ||
| static $UNRESERVED= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'; | ||
|
|
||
| $random= random_bytes(64); | ||
| $verifier= ''; | ||
| for ($i= 0; $i < 64; $i++) { | ||
| $verifier.= $UNRESERVED[ord($random[$i]) % 66]; | ||
| } | ||
| return ['verifier' => $verifier]; | ||
| } | ||
|
|
||
| /** Returns parameters to be passed on to authorization */ | ||
| public function pass(array $seed): array { | ||
| return [ | ||
| 'code_challenge' => ($this->challenge)($seed['verifier']), | ||
| 'code_challenge_method' => $this->method, | ||
| ]; | ||
| } | ||
|
|
||
| /** Returns parameters to be used in authentication process */ | ||
| public function params(string $endpoint, array $seed= []): array { | ||
| return [ | ||
| 'client_id' => $this->key, | ||
| 'code_verifier' => $seed['verifier'], | ||
| ]; | ||
| } | ||
| } | ||
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
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
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,50 @@ | ||
| <?php namespace web\auth\unittest; | ||
|
|
||
| use lang\IllegalArgumentException; | ||
| use test\{Assert, Expect, Test, Values}; | ||
| use web\auth\oauth\ByPKCE; | ||
|
|
||
| class ByPKCETest { | ||
| const CLIENT_ID= 'b2ba8814'; | ||
| const TEST_SEED= ['verifier' => 'test-challenge']; | ||
|
|
||
| /** @return iterable */ | ||
| private function challenges() { | ||
| yield ['S256', 'Xuq1l4Pllrvf6AJ2BfBwnQFQKBK7dnKAbolZ3zvWFlw']; // base64(sha256(TEST_SEED[verifier])) | ||
| yield ['plain', 'test-challenge']; | ||
| } | ||
|
|
||
| #[Test, Values(ByPKCE::SUPPORTED)] | ||
| public function can_create_with($method) { | ||
| new ByPKCE(self::CLIENT_ID, $method); | ||
| } | ||
|
|
||
| #[Test, Values(['S128', 'invalid']), Expect(IllegalArgumentException::class)] | ||
| public function unsupported($method) { | ||
| new ByPKCE(self::CLIENT_ID, $method); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function seed_creates_verifier() { | ||
| Assert::matches( | ||
| '/^[a-zA-Z0-9._~-]{64}$/', | ||
| (new ByPKCE(self::CLIENT_ID, 'S256'))->seed()['verifier'] | ||
| ); | ||
| } | ||
|
|
||
| #[Test, Values(from: 'challenges')] | ||
| public function pass($method, $challenge) { | ||
| Assert::equals( | ||
| ['code_challenge' => $challenge, 'code_challenge_method' => $method], | ||
| (new ByPKCE(self::CLIENT_ID, $method))->pass(self::TEST_SEED) | ||
| ); | ||
| } | ||
|
|
||
| #[Test] | ||
| public function params() { | ||
| Assert::equals( | ||
| ['client_id' => self::CLIENT_ID, 'code_verifier' => 'test-challenge'], | ||
| (new ByPKCE(self::CLIENT_ID, 'S256'))->params('https://test/oauth/tokens', self::TEST_SEED) | ||
| ); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.