Skip to content

Commit

Permalink
Merge pull request #29 from xp-forge/feature/mutable-scopes-and-callback
Browse files Browse the repository at this point in the history
Make callback and scopes mutable
  • Loading branch information
thekid authored Apr 8, 2024
2 parents d785dea + 196a115 commit 2bed0fa
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/php/web/auth/oauth/OAuth2Flow.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions src/main/php/web/auth/oauth/OAuthFlow.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace web\auth\oauth;

use util\URI;
use web\auth\{Flow, UserInfo, AuthenticationError};

abstract class OAuthFlow extends Flow {
Expand All @@ -8,6 +9,12 @@ abstract class OAuthFlow extends Flow {
/** @return ?util.URI */
public function callback() { return $this->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
Expand Down
8 changes: 8 additions & 0 deletions src/test/php/web/auth/unittest/OAuth1FlowTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
17 changes: 17 additions & 0 deletions src/test/php/web/auth/unittest/OAuth2FlowTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 2bed0fa

Please sign in to comment.