Skip to content
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

Make callback and scopes mutable #29

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading