Skip to content

Commit

Permalink
Extract callback() to base class
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jan 1, 2024
1 parent be5b276 commit 6cab32f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
5 changes: 1 addition & 4 deletions src/main/php/web/auth/oauth/OAuth1Flow.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class OAuth1Flow extends OAuthFlow {
const SESSION_KEY= 'oauth1::flow';

private $service, $signature, $callback;
private $service, $signature;

/**
* Creates a new OAuth 1 flow
Expand Down Expand Up @@ -38,9 +38,6 @@ public function __construct($service, $consumer, $callback= null) {
}
}

/** @return ?util.URI */
public function callback() { return $this->callback; }

/**
* Obtain a request token
*
Expand Down
25 changes: 2 additions & 23 deletions src/main/php/web/auth/oauth/OAuth2Flow.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
use lang\IllegalStateException;
use peer\http\HttpConnection;
use util\{Random, Secret, URI};
use web\auth\{Flow, UserInfo, AuthenticationError};
use web\session\Sessions;

class OAuth2Flow extends Flow {
class OAuth2Flow extends OAuthFlow {
const SESSION_KEY= 'oauth2::flow';

private $auth, $backend, $scopes, $callback, $rand;
private $auth, $backend, $scopes, $rand;

/**
* Creates a new OAuth 2 flow
Expand Down Expand Up @@ -41,29 +40,9 @@ public function __construct($auth, $tokens, $consumer, $callback= null, $scopes=
$this->rand= new Random();
}

/** @return ?util.URI */
public function callback() { return $this->callback; }

/** @return string[] */
public function scopes() { return $this->scopes; }

/**
* Returns user info which fetched from the given endpoint using the
* authorized OAuth2 client
*
* @param string|util.URI $endpoint
* @return web.auth.UserInfo
*/
public function fetchUser($endpoint= null): UserInfo {
return new UserInfo(function(Client $client) use($endpoint) {
$response= $client->fetch((string)$endpoint);
if ($response->status() >= 400) {
throw new AuthenticationError('Unexpected status '.$response->status().' from '.$endpoint);
}
return $response->value();
});
}

/**
* Refreshes access token given a refresh token if necessary.
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/php/web/auth/oauth/OAuthFlow.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
use web\auth\{Flow, UserInfo, AuthenticationError};

abstract class OAuthFlow extends Flow {
protected $callback;

/** @return ?util.URI */
public function callback() { return $this->callback; }

/**
* Returns user info which fetched from the given endpoint using the
Expand Down

0 comments on commit 6cab32f

Please sign in to comment.