Skip to content

Commit

Permalink
Fix code quality issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marcriemer committed May 26, 2024
1 parent 174c2b0 commit 58cbe2a
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 56 deletions.
5 changes: 4 additions & 1 deletion examples/src/Repositories/IdTokenRepository.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\Repositories;

use DateTimeImmutable;
use Lcobucci\JWT\Encoding\ChainedFormatter;
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Token\Builder;
Expand All @@ -27,7 +30,7 @@ public function getBuilder(AccessTokenEntityInterface $accessToken): Builder
$builder = (new Builder(new JoseEncoder(), ChainedFormatter::withUnixTimestampDates()))
->permittedFor($accessToken->getClient()->getIdentifier())
->issuedBy($this->issuedBy)
->issuedAt(new \DateTimeImmutable())
->issuedAt(new DateTimeImmutable())
->expiresAt($accessToken->getExpiryDateTime())
->relatedTo($accessToken->getUserIdentifier());

Expand Down
44 changes: 27 additions & 17 deletions src/ClaimExtractor.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server;

use InvalidArgumentException;
use League\OAuth2\Server\Entities\ClaimSetEntry;
use League\OAuth2\Server\Entities\ClaimSetEntryInterface;
use League\OAuth2\Server\Entities\ClaimSetInterface;
use League\OAuth2\Server\Entities\ScopeEntityInterface;

use function array_filter;
use function array_intersect;
use function array_keys;
use function array_merge;
use function in_array;
use function sprintf;

/**
* ClaimExtractor
*
Expand All @@ -21,9 +32,14 @@ class ClaimExtractor implements ClaimExtractorInterface
*
* @var ClaimSetEntryInterface[]
*/
protected $claimSets = [];
protected array $claimSets = [];

protected $protectedClaims = ['profile', 'email', 'address', 'phone'];
/**
* Protected claims
*
* @var string[]
*/
protected array $protectedClaims = ['profile', 'email', 'address', 'phone'];

/**
* ClaimExtractor constructor
Expand All @@ -39,17 +55,16 @@ public function __construct(array $claimSets = [])
}

/**
* @param ClaimSetEntryInterface $claimSetEntry
*
* @return $this
*
* @throws \InvalidArgumentException
*/
public function addClaimSet(ClaimSetEntryInterface $claimSetEntry): ClaimExtractor
{
if (\in_array($claimSetEntry->getScope(), $this->protectedClaims) && !$this->getClaimSet($claimSetEntry->getScope())) {
throw new \InvalidArgumentException(
\sprintf('%s is a protected scope and is pre-defined by the OpenID Connect specification.', $claimSetEntry->getScope())
if (in_array($claimSetEntry->getScope(), $this->protectedClaims) && !$this->getClaimSet($claimSetEntry->getScope())) {

Check failure on line 65 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.1, prefer-lowest, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 65 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.1, prefer-lowest, ubuntu-latest)

Only booleans are allowed in a negated boolean, League\OAuth2\Server\Entities\ClaimSetEntryInterface|null given.

Check failure on line 65 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3, prefer-lowest, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 65 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3, prefer-lowest, ubuntu-latest)

Only booleans are allowed in a negated boolean, League\OAuth2\Server\Entities\ClaimSetEntryInterface|null given.
throw new InvalidArgumentException(
sprintf('%s is a protected scope and is pre-defined by the OpenID Connect specification.', $claimSetEntry->getScope())
);
}

Expand All @@ -58,11 +73,6 @@ public function addClaimSet(ClaimSetEntryInterface $claimSetEntry): ClaimExtract
return $this;
}

/**
* @param string $scope
*
* @return ClaimSetEntryInterface|null
*/
public function getClaimSet(string $scope): ?ClaimSetEntryInterface
{
foreach ($this->claimSets as $set) {
Expand All @@ -77,7 +87,7 @@ public function getClaimSet(string $scope): ?ClaimSetEntryInterface
/**
* Get claimSets
*
* @return array
* @return ClaimSetInterface[]
*/
public function getClaimSets(): array
{
Expand All @@ -90,7 +100,7 @@ public function getClaimSets(): array
public function extract(array $scopes, array $claims): array
{
$claimData = [];
$keys = \array_keys($claims);
$keys = array_keys($claims);

foreach ($scopes as $scope) {
$scopeName = ($scope instanceof ScopeEntityInterface) ? $scope->getIdentifier() : $scope;

Check failure on line 106 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.1, prefer-lowest, ubuntu-latest)

Else branch is unreachable because ternary operator condition is always true.

Check failure on line 106 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.1, prefer-lowest, ubuntu-latest)

Instanceof between League\OAuth2\Server\Entities\ScopeEntityInterface and League\OAuth2\Server\Entities\ScopeEntityInterface will always evaluate to true.

Check failure on line 106 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3, prefer-lowest, ubuntu-latest)

Else branch is unreachable because ternary operator condition is always true.

Check failure on line 106 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3, prefer-lowest, ubuntu-latest)

Instanceof between League\OAuth2\Server\Entities\ScopeEntityInterface and League\OAuth2\Server\Entities\ScopeEntityInterface will always evaluate to true.
Expand All @@ -100,21 +110,21 @@ public function extract(array $scopes, array $claims): array
continue;
}

$intersected = \array_intersect($claimSet->getClaims(), $keys);
$intersected = array_intersect($claimSet->getClaims(), $keys);

if (empty($intersected)) {

Check failure on line 115 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.1, prefer-lowest, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 115 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3, prefer-lowest, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.
continue;
}

$data = \array_filter(
$data = array_filter(
$claims,
function ($key) use ($intersected) {
return \in_array($key, $intersected);
return in_array($key, $intersected);

Check failure on line 122 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.1, prefer-lowest, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.

Check failure on line 122 in src/ClaimExtractor.php

View workflow job for this annotation

GitHub Actions / Static Analysis (8.3, prefer-lowest, ubuntu-latest)

Call to function in_array() requires parameter #3 to be set.
},
ARRAY_FILTER_USE_KEY
);

$claimData = \array_merge($claimData, $data);
$claimData = array_merge($claimData, $data);
}

return $claimData;
Expand Down
10 changes: 7 additions & 3 deletions src/ClaimExtractorInterface.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server;

use League\OAuth2\Server\Entities\ScopeEntityInterface;

interface ClaimExtractorInterface
{
/**
* For given scopes and aggregated claims get all claims that have been configured on the extractor.
*
* @param array $scopes
* @param array $claims
* @param array<int, ScopeEntityInterface> $scopes
* @param array<string, string> $claims
*
* @return array
* @return array<string, string>
*/
public function extract(array $scopes, array $claims): array;
}
24 changes: 9 additions & 15 deletions src/Entities/ClaimSetEntry.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\Entities;

/**
Expand All @@ -12,27 +14,19 @@
class ClaimSetEntry implements ClaimSetEntryInterface
{
/**
* @var string
*/
protected $scope;

/**
* @var array
* Summary of __construct
*
* @param string $scope Scope of the claimset
* @param string[] $claims The claims
*/
protected $claims;

public function __construct(
string $scope,
array $claims
protected string $scope,
protected array $claims
) {
$this->scope = $scope;
$this->claims = $claims;
}

/**
* Get scope
*
* @return string
*/
public function getScope(): string
{
Expand All @@ -42,7 +36,7 @@ public function getScope(): string
/**
* Get claims
*
* @return ClaimSetInterface[]
* @return string[]
*/
public function getClaims(): array
{
Expand Down
2 changes: 2 additions & 0 deletions src/Entities/ClaimSetEntryInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\Entities;

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Entities/ClaimSetInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\Entities;

/**
Expand All @@ -11,5 +13,10 @@
*/
interface ClaimSetInterface
{
/**
* Get Claims
*
* @return string[]
*/
public function getClaims(): array;
}
7 changes: 4 additions & 3 deletions src/IdTokenClaimsCreatedEvent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server;

use Lcobucci\JWT\Builder;
Expand All @@ -16,11 +18,10 @@ final class IdTokenClaimsCreatedEvent extends IdTokenEvent
/**
* Builder
*
* @var Builder
*/
private $builder;
private Builder $builder;

public function __construct($name, Builder $builder)
public function __construct(string $name, Builder $builder)
{
parent::__construct($name);
$this->builder = $builder;
Expand Down
6 changes: 4 additions & 2 deletions src/IdTokenEvent.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server;

use League\Event\Event;
use League\OAuth2\Server\EventEmitting\AbstractEvent;

/**
* IdTokenEvent
*
* @author Marc Riemer <[email protected]>
*/
class IdTokenEvent extends Event
class IdTokenEvent extends AbstractEvent
{
public const ID_TOKEN_ISSUED = 'id_token.issued';

Expand Down
8 changes: 4 additions & 4 deletions src/IdTokenIssuedEvent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server;

use Lcobucci\JWT\Token;
Expand All @@ -14,21 +16,19 @@ final class IdTokenIssuedEvent extends IdTokenEvent
/**
* Token
*
* @var Token
*/
private $token;
private Token $token;

/**
* Get Token
*
* @return Token
*/
public function getToken(): Token
{
return $this->token;
}

public function __construct($name, Token $token)
public function __construct(mixed $name, Token $token)
{
parent::__construct($name);
$this->token = $token;
Expand Down
9 changes: 2 additions & 7 deletions src/Repositories/ClaimSetRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\Repositories;

use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
Expand All @@ -14,12 +16,5 @@
*/
interface ClaimSetRepositoryInterface
{
/**
* Get ClaimSetEntries
*
* @param AccessTokenEntityInterface $authCode
*
* @return ClaimSetEntryInterface
*/
public function getClaimSetEntry(AccessTokenEntityInterface $authCode): ClaimSetEntryInterface;
}
4 changes: 2 additions & 2 deletions src/Repositories/IdTokenRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\Repositories;

use Lcobucci\JWT\Builder;
Expand All @@ -17,8 +19,6 @@ interface IdTokenRepositoryInterface
* Creates new token builder and may add some standard claims
*
* @param AccessTokenEntityInterface $token Issued access token
*
* @return Builder
*/
public function getBuilder(AccessTokenEntityInterface $token): Builder;
}
8 changes: 6 additions & 2 deletions src/ResponseTypes/UserInfoResponse.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

declare(strict_types=1);

namespace League\OAuth2\Server\ResponseTypes;

use League\OAuth2\Server\Entities\ClaimSetInterface;
use Psr\Http\Message\ResponseInterface;

use function json_encode;

/**
* A simple user info response for the ResourceServer class
*
Expand All @@ -20,15 +24,15 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function generateHttpResponse(ResponseInterface $response)
public function generateHttpResponse(ResponseInterface $response): ResponseInterface
{
$response = $response
->withStatus(200)
->withHeader('pragma', 'no-cache')
->withHeader('cache-control', 'no-store')
->withHeader('content-type', 'application/json; charset=UTF-8');

$response->getBody()->write(\json_encode($this->claimSet->getClaims()));
$response->getBody()->write(json_encode($this->claimSet->getClaims()));

return $response;
}
Expand Down

0 comments on commit 58cbe2a

Please sign in to comment.