From 196a115a8e2ebdd0a596f4593af72e188ff12312 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Mon, 8 Apr 2024 01:11:01 +0200 Subject: [PATCH] Make callback and scopes mutable --- .../php/web/auth/oauth/OAuth2Flow.class.php | 6 ++++++ src/main/php/web/auth/oauth/OAuthFlow.class.php | 7 +++++++ .../web/auth/unittest/OAuth1FlowTest.class.php | 8 ++++++++ .../web/auth/unittest/OAuth2FlowTest.class.php | 17 +++++++++++++++++ 4 files changed, 38 insertions(+) diff --git a/src/main/php/web/auth/oauth/OAuth2Flow.class.php b/src/main/php/web/auth/oauth/OAuth2Flow.class.php index ae7858c..a480a48 100755 --- a/src/main/php/web/auth/oauth/OAuth2Flow.class.php +++ b/src/main/php/web/auth/oauth/OAuth2Flow.class.php @@ -44,6 +44,12 @@ public function __construct($auth, $tokens, $consumer, $callback= null, $scopes= /** @return string[] */ public function scopes() { return $this->scopes; } + /** @param string[] $scopes */ + public function requesting($scopes): self { + $this->scopes= $scopes; + return $this; + } + /** * Refreshes access token given a refresh token if necessary. * diff --git a/src/main/php/web/auth/oauth/OAuthFlow.class.php b/src/main/php/web/auth/oauth/OAuthFlow.class.php index 4b8b5ca..a7d062c 100755 --- a/src/main/php/web/auth/oauth/OAuthFlow.class.php +++ b/src/main/php/web/auth/oauth/OAuthFlow.class.php @@ -1,5 +1,6 @@ callback; } + /** @param ?string|util.URI $callback */ + public function calling($callback): self { + $this->callback= null === $callback || $callback instanceof URI ? $callback : new URI($callback); + return $this; + } + /** * Returns user info which fetched from the given endpoint using the * authorized OAuth2 client diff --git a/src/test/php/web/auth/unittest/OAuth1FlowTest.class.php b/src/test/php/web/auth/unittest/OAuth1FlowTest.class.php index a2dc8ce..c05ce8b 100755 --- a/src/test/php/web/auth/unittest/OAuth1FlowTest.class.php +++ b/src/test/php/web/auth/unittest/OAuth1FlowTest.class.php @@ -27,6 +27,14 @@ public function callback() { Assert::equals(new URI(self::CALLBACK), (new OAuth1Flow(self::AUTH, [self::ID, self::SECRET], self::CALLBACK))->callback()); } + #[Test] + public function calling() { + Assert::equals( + new URI('/test'), + (new OAuth1Flow(self::AUTH, [self::ID, self::SECRET], self::CALLBACK))->calling('/test')->callback() + ); + } + #[Test, Values(from: 'paths')] public function fetches_request_token_then_redirects_to_auth($path) { $request= ['oauth_token' => 'T']; diff --git a/src/test/php/web/auth/unittest/OAuth2FlowTest.class.php b/src/test/php/web/auth/unittest/OAuth2FlowTest.class.php index 232dfea..c07c4b1 100755 --- a/src/test/php/web/auth/unittest/OAuth2FlowTest.class.php +++ b/src/test/php/web/auth/unittest/OAuth2FlowTest.class.php @@ -51,12 +51,29 @@ public function callback() { Assert::equals(new URI(self::CALLBACK), (new OAuth2Flow(self::AUTH, self::TOKENS, self::CONSUMER, self::CALLBACK))->callback()); } + #[Test] + public function calling() { + Assert::equals( + new URI('/test'), + (new OAuth2Flow(self::AUTH, self::TOKENS, self::CONSUMER, self::CALLBACK))->calling('/test')->callback() + ); + } + #[Test] public function scopes() { $scopes= ['user', 'profile']; Assert::equals($scopes, (new OAuth2Flow(self::AUTH, self::TOKENS, self::CONSUMER, self::CALLBACK, $scopes))->scopes()); } + #[Test] + public function requesting_scopes() { + $scopes= ['user', 'profile']; + Assert::equals( + $scopes, + (new OAuth2Flow(self::AUTH, self::TOKENS, self::CONSUMER, self::CALLBACK))->requesting($scopes)->scopes() + ); + } + #[Test] public function scopes_defaults_to_user() { Assert::equals(['user'], (new OAuth2Flow(self::AUTH, self::TOKENS, self::CONSUMER, self::CALLBACK))->scopes());