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;
|
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
|
|
|
|
|
|
|
public const SUB_ACCOUNT_PREFIX = 'ely|';
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
public function createForWebAccount(Account $account, AccountSession $session = null): Token {
|
2019-08-01 14:47:12 +05:30
|
|
|
$payloads = [
|
2019-12-04 23:40:15 +05:30
|
|
|
'sub' => $this->buildSub($account->id),
|
2019-12-06 21:01:04 +05:30
|
|
|
'exp' => Carbon::now()->addHour()->getTimestamp(),
|
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
|
2019-08-02 05:59:20 +05:30
|
|
|
$payloads['exp'] = Carbon::now()->addDays(7)->getTimestamp();
|
2019-08-01 14:47:12 +05:30
|
|
|
} else {
|
|
|
|
$payloads['jti'] = $session->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Yii::$app->tokens->create($payloads);
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
public function createForOAuthClient(AccessTokenEntityInterface $accessToken): Token {
|
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()) {
|
|
|
|
$payloads['exp'] = $accessToken->getExpiryDateTime()->getTimestamp();
|
|
|
|
}
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
if ($accessToken->getUserIdentifier() !== null) {
|
2019-12-04 23:40:15 +05:30
|
|
|
$payloads['sub'] = $this->buildSub($accessToken->getUserIdentifier());
|
2019-09-22 02:47:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return Yii::$app->tokens->create($payloads);
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:40:15 +05:30
|
|
|
public function createForMinecraftAccount(Account $account, string $clientToken): Token {
|
|
|
|
return Yii::$app->tokens->create([
|
2019-12-13 16:25:09 +05:30
|
|
|
'scope' => $this->prepareScopes([P::MINECRAFT_SERVER_SESSION]),
|
2019-12-04 23:40:15 +05:30
|
|
|
'ely-client-token' => new EncryptedValue($clientToken),
|
|
|
|
'sub' => $this->buildSub($account->id),
|
|
|
|
'exp' => Carbon::now()->addDays(2)->getTimestamp(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-06 21:01:04 +05:30
|
|
|
/**
|
|
|
|
* @param ScopeEntityInterface[]|string[] $scopes
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|