Skip to content
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
25 changes: 1 addition & 24 deletions src/Token/EsiaAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,18 @@

namespace Ekapusta\OAuth2Esia\Token;

use Ekapusta\OAuth2Esia\Interfaces\Token\ScopedTokenInterface;
use InvalidArgumentException;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\ValidationData;
use League\OAuth2\Client\Token\AccessToken;

class EsiaAccessToken extends AccessToken implements ScopedTokenInterface
class EsiaAccessToken extends TrustedEsiaAccessToken
{
private $parsedToken;

public function __construct(array $options, $publicKeyPath, Signer $signer)
{
parent::__construct($options);

$this->parsedToken = (new Parser())->parse($this->accessToken);
$this->resourceOwnerId = $this->parsedToken->getClaim('urn:esia:sbj_id');

if (!$this->parsedToken->validate(new ValidationData())) {
throw new InvalidArgumentException('Access token is invalid: '.var_export($options, true));
}

if (!$this->parsedToken->verify($signer, new Key(file_get_contents($publicKeyPath)))) {
throw new InvalidArgumentException('Access token can not be verified: '.var_export($options, true));
}
}

public function getScopes()
{
$scopes = [];
foreach (explode(' ', $this->parsedToken->getClaim('scope', '')) as $scope) {
$scopes[] = parse_url($scope, PHP_URL_PATH);
}

return $scopes;
}
}
36 changes: 36 additions & 0 deletions src/Token/TrustedEsiaAccessToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Ekapusta\OAuth2Esia\Token;

use Ekapusta\OAuth2Esia\Interfaces\Token\ScopedTokenInterface;
use InvalidArgumentException;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\ValidationData;
use League\OAuth2\Client\Token\AccessToken;

class TrustedEsiaAccessToken extends AccessToken implements ScopedTokenInterface
{
protected $parsedToken;

public function __construct(array $options)
{
parent::__construct($options);

$this->parsedToken = (new Parser())->parse($this->accessToken);
$this->resourceOwnerId = $this->parsedToken->getClaim('urn:esia:sbj_id');

if (!$this->parsedToken->validate(new ValidationData())) {
throw new InvalidArgumentException('Access token is invalid: '.var_export($options, true));
}
}

public function getScopes()
{
$scopes = [];
foreach (explode(' ', $this->parsedToken->getClaim('scope', '')) as $scope) {
$scopes[] = parse_url($scope, PHP_URL_PATH);
}

return $scopes;
}
}