2019-08-01 14:47:12 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\components\Tokens;
|
|
|
|
|
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;
|
|
|
|
use Lcobucci\JWT\Token;
|
|
|
|
use Yii;
|
|
|
|
|
|
|
|
class TokensFactory {
|
|
|
|
|
|
|
|
public const SUB_ACCOUNT_PREFIX = 'ely|';
|
|
|
|
|
|
|
|
public static function createForAccount(Account $account, AccountSession $session = null): Token {
|
|
|
|
$payloads = [
|
|
|
|
'ely-scopes' => 'accounts_web_user',
|
|
|
|
'sub' => self::SUB_ACCOUNT_PREFIX . $account->id,
|
|
|
|
];
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|