Skip to content

Commit

Permalink
Add list of scopes required explicitly
Browse files Browse the repository at this point in the history
And replace `delegatesOwner` with normal scope check
  • Loading branch information
cl8n committed Dec 3, 2022
1 parent 355aff9 commit 29ff9ab
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions app/Models/OAuth/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ class Token extends PassportToken

public $timestamps = true;

/**
* Whether the resource owner is delegated to the client's owner.
*
* @return bool
*/
public function delegatesOwner(): bool
public function can($scope)
{
return in_array('delegate', $this->scopes, true);
static $scopesRequiredExplicitly;
$scopesRequiredExplicitly ??= new Set(['delegate']);

// Skip checking "*" for scopes that require an explicit request
if ($scopesRequiredExplicitly->contains($scope)) {
return in_array($scope, $this->scopes, true);
}

return parent::can($scope);
}

/**
Expand All @@ -38,7 +41,7 @@ public function delegatesOwner(): bool
*/
public function getResourceOwner(): ?User
{
if ($this->isClientCredentials() && $this->delegatesOwner()) {
if ($this->isClientCredentials() && $this->can('delegate')) {
return $this->client->user;
}

Expand Down Expand Up @@ -118,12 +121,12 @@ public function validate()
throw new InvalidScopeException('* is not allowed with Client Credentials');
}

if ($this->delegatesOwner() && !$this->client->user->isBot()) {
if ($scopes->contains('delegate') && !$this->client->user->isBot()) {
throw new InvalidScopeException('Delegation with Client Credentials is only available to chat bots.');
}

if (!$scopes->intersect($scopesRequireDelegation)->isEmpty()) {
if (!$this->delegatesOwner()) {
if (!$scopes->contains('delegate')) {
throw new InvalidScopeException('delegate scope is required.');
}

Expand All @@ -134,7 +137,7 @@ public function validate()
}
} else {
// delegation is only available for client_credentials.
if ($this->delegatesOwner()) {
if ($scopes->contains('delegate')) {
throw new InvalidScopeException('delegate scope is only valid for client_credentials tokens.');
}

Expand Down

0 comments on commit 29ff9ab

Please sign in to comment.