70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?php
|
|
namespace Misuzu\OAuth2;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
use Index\UriBase64;
|
|
use Index\Db\DbResult;
|
|
|
|
class OAuth2AuthorisationInfo {
|
|
public function __construct(
|
|
public private(set) string $id,
|
|
public private(set) string $appId,
|
|
public private(set) string $userId,
|
|
public private(set) string $uriId,
|
|
public private(set) string $challengeCode,
|
|
public private(set) string $challengeMethod,
|
|
public private(set) string $scope,
|
|
public private(set) string $code,
|
|
public private(set) int $createdTime,
|
|
public private(set) int $expiresTime
|
|
) {}
|
|
|
|
public static function fromResult(DbResult $result): OAuth2AuthorisationInfo {
|
|
return new OAuth2AuthorisationInfo(
|
|
id: $result->getString(0),
|
|
appId: $result->getString(1),
|
|
userId: $result->getString(2),
|
|
uriId: $result->getString(3),
|
|
challengeCode: $result->getString(4),
|
|
challengeMethod: $result->getString(5),
|
|
scope: $result->getString(6),
|
|
code: $result->getString(7),
|
|
createdTime: $result->getInteger(8),
|
|
expiresTime: $result->getInteger(9),
|
|
);
|
|
}
|
|
|
|
public function verifyCodeChallenge(string $codeVerifier): bool {
|
|
if($this->challengeMethod === 'plain')
|
|
return hash_equals($this->challengeCode, $codeVerifier);
|
|
|
|
if($this->challengeMethod === 'S256') {
|
|
$knownHash = UriBase64::decode($this->challengeCode);
|
|
$userHash = hash('sha256', $codeVerifier, true);
|
|
return hash_equals($knownHash, $userHash);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/** @var string[] */
|
|
public array $scopes {
|
|
get => explode(' ', $this->scope);
|
|
}
|
|
|
|
public CarbonImmutable $createdAt {
|
|
get => CarbonImmutable::createFromTimestampUTC($this->createdTime);
|
|
}
|
|
|
|
public bool $expired {
|
|
get => time() > $this->expiresTime;
|
|
}
|
|
|
|
public CarbonImmutable $expiresAt {
|
|
get => CarbonImmutable::createFromTimestampUTC($this->expiresTime);
|
|
}
|
|
|
|
public int $remainingLifetime {
|
|
get => max(0, $this->expiresTime - time());
|
|
}
|
|
}
|