2019-08-01 14:47:12 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\components\Tokens;
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
use api\rbac\Permissions as P;
|
|
|
|
use api\rbac\Roles as R;
|
2019-08-02 05:59:20 +05:30
|
|
|
use Carbon\Carbon;
|
2019-08-01 14:47:12 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\AccountSession;
|
2019-12-06 21:01:04 +05:30
|
|
|
use DateTime;
|
2019-08-01 14:47:12 +05:30
|
|
|
use Lcobucci\JWT\Token;
|
2024-12-02 15:40:55 +05:30
|
|
|
use Lcobucci\JWT\UnencryptedToken;
|
2019-09-22 02:47:21 +05:30
|
|
|
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
|
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface;
|
2019-08-01 14:47:12 +05:30
|
|
|
use Yii;
|
2019-12-04 23:40:15 +05:30
|
|
|
use yii\base\Component;
|
2019-08-01 14:47:12 +05:30
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
class TokensFactory extends Component {
|
2019-08-01 14:47:12 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public const string SUB_ACCOUNT_PREFIX = 'ely|';
|
2019-08-01 14:47:12 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function createForWebAccount(Account $account, AccountSession $session = null): UnencryptedToken {
|
2019-08-01 14:47:12 +05:30
|
|
|
$payloads = [
|
2019-12-04 23:40:15 +05:30
|
|
|
'sub' => $this->buildSub($account->id),
|
2024-12-02 15:40:55 +05:30
|
|
|
'exp' => Carbon::now()->addHour()->toDateTimeImmutable(),
|
2019-12-13 16:25:09 +05:30
|
|
|
'scope' => $this->prepareScopes([R::ACCOUNTS_WEB_USER]),
|
2019-08-01 14:47:12 +05:30
|
|
|
];
|
|
|
|
if ($session === null) {
|
|
|
|
// If we don't remember a session, the token should live longer
|
|
|
|
// so that the session doesn't end while working with the account
|
2024-12-02 15:40:55 +05:30
|
|
|
$payloads['exp'] = Carbon::now()->addDays(7)->toDateTimeImmutable();
|
2019-08-01 14:47:12 +05:30
|
|
|
} else {
|
2024-12-02 15:40:55 +05:30
|
|
|
$payloads['jti'] = (string)$session->id;
|
2019-08-01 14:47:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return Yii::$app->tokens->create($payloads);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function createForOAuthClient(AccessTokenEntityInterface $accessToken): UnencryptedToken {
|
2019-09-22 02:47:21 +05:30
|
|
|
$payloads = [
|
2019-12-13 16:25:09 +05:30
|
|
|
'client_id' => $accessToken->getClient()->getIdentifier(),
|
|
|
|
'scope' => $this->prepareScopes($accessToken->getScopes()),
|
2019-09-22 02:47:21 +05:30
|
|
|
];
|
2019-12-06 21:01:04 +05:30
|
|
|
if ($accessToken->getExpiryDateTime() > new DateTime()) {
|
2024-12-02 15:40:55 +05:30
|
|
|
$payloads['exp'] = $accessToken->getExpiryDateTime();
|
2019-12-06 21:01:04 +05:30
|
|
|
}
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
if ($accessToken->getUserIdentifier() !== null) {
|
2024-12-02 15:40:55 +05:30
|
|
|
$payloads['sub'] = $this->buildSub((int)$accessToken->getUserIdentifier());
|
2019-09-22 02:47:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return Yii::$app->tokens->create($payloads);
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function createForMinecraftAccount(Account $account, string $clientToken): UnencryptedToken {
|
2019-12-04 23:40:15 +05:30
|
|
|
return Yii::$app->tokens->create([
|
2022-01-24 16:31:11 +05:30
|
|
|
'scope' => $this->prepareScopes([P::OBTAIN_OWN_ACCOUNT_INFO, P::MINECRAFT_SERVER_SESSION]),
|
2019-12-04 23:40:15 +05:30
|
|
|
'ely-client-token' => new EncryptedValue($clientToken),
|
|
|
|
'sub' => $this->buildSub($account->id),
|
2024-12-02 15:40:55 +05:30
|
|
|
'exp' => Carbon::now()->addDays(2)->toDateTimeImmutable(),
|
2019-12-04 23:40:15 +05:30
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-06 21:01:04 +05:30
|
|
|
/**
|
|
|
|
* @param ScopeEntityInterface[]|string[] $scopes
|
|
|
|
*/
|
|
|
|
private function prepareScopes(array $scopes): string {
|
2019-12-13 16:25:09 +05:30
|
|
|
return implode(' ', array_map(function($scope): string {
|
2019-12-06 21:01:04 +05:30
|
|
|
if ($scope instanceof ScopeEntityInterface) {
|
|
|
|
return $scope->getIdentifier();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $scope;
|
|
|
|
}, $scopes));
|
2019-12-04 23:40:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private function buildSub(int $accountId): string {
|
2019-09-22 02:47:21 +05:30
|
|
|
return self::SUB_ACCOUNT_PREFIX . $accountId;
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
}
|