2017-06-12 17:06:20 +05:30
|
|
|
<?php
|
2019-08-23 13:58:04 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
namespace api\modules\oauth\models;
|
2017-06-12 17:06:20 +05:30
|
|
|
|
2019-09-06 05:02:57 +05:30
|
|
|
use api\components\OAuth2\Entities\UserEntity;
|
2019-12-09 22:01:54 +05:30
|
|
|
use api\components\OAuth2\Events\RequestedRefreshToken;
|
2019-08-02 05:59:20 +05:30
|
|
|
use api\rbac\Permissions as P;
|
2017-06-12 17:06:20 +05:30
|
|
|
use common\models\Account;
|
|
|
|
use common\models\OauthClient;
|
2019-09-18 04:44:05 +05:30
|
|
|
use common\models\OauthSession;
|
2019-08-23 13:58:04 +05:30
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-06-12 17:06:20 +05:30
|
|
|
use League\OAuth2\Server\AuthorizationServer;
|
2019-09-18 04:44:05 +05:30
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface;
|
2019-08-23 13:58:04 +05:30
|
|
|
use League\OAuth2\Server\Exception\OAuthServerException;
|
|
|
|
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2019-09-18 04:44:05 +05:30
|
|
|
use Webmozart\Assert\Assert;
|
2017-06-12 17:06:20 +05:30
|
|
|
use Yii;
|
|
|
|
|
|
|
|
class OauthProcess {
|
|
|
|
|
2017-10-18 05:07:01 +05:30
|
|
|
private const INTERNAL_PERMISSIONS_TO_PUBLIC_SCOPES = [
|
|
|
|
P::OBTAIN_OWN_ACCOUNT_INFO => 'account_info',
|
|
|
|
P::OBTAIN_ACCOUNT_EMAIL => 'account_email',
|
|
|
|
];
|
|
|
|
|
2021-03-29 08:17:27 +05:30
|
|
|
private AuthorizationServer $server;
|
2017-06-12 17:06:20 +05:30
|
|
|
|
|
|
|
public function __construct(AuthorizationServer $server) {
|
|
|
|
$this->server = $server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* A request that should check the passed OAuth2 authorization params and build a response
|
|
|
|
* for our frontend application.
|
2017-06-12 17:06:20 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* The input data is the standard GET parameters list according to the OAuth2 standard:
|
2017-06-12 17:06:20 +05:30
|
|
|
* $_GET = [
|
|
|
|
* client_id,
|
|
|
|
* redirect_uri,
|
|
|
|
* response_type,
|
|
|
|
* scope,
|
|
|
|
* state,
|
|
|
|
* ];
|
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* In addition, you can pass the description value to override the application's description.
|
2017-06-12 17:06:20 +05:30
|
|
|
*
|
2019-12-09 22:01:54 +05:30
|
|
|
* @param ServerRequestInterface $request
|
2017-06-12 17:06:20 +05:30
|
|
|
* @return array
|
|
|
|
*/
|
2019-12-09 22:01:54 +05:30
|
|
|
public function validate(ServerRequestInterface $request): array {
|
2017-06-12 17:06:20 +05:30
|
|
|
try {
|
2019-08-23 13:58:04 +05:30
|
|
|
$authRequest = $this->server->validateAuthorizationRequest($request);
|
|
|
|
$client = $authRequest->getClient();
|
2018-02-28 03:57:35 +05:30
|
|
|
/** @var OauthClient $clientModel */
|
2019-08-23 13:58:04 +05:30
|
|
|
$clientModel = $this->findClient($client->getIdentifier());
|
|
|
|
$response = $this->buildSuccessResponse($request, $clientModel, $authRequest->getScopes());
|
|
|
|
} catch (OAuthServerException $e) {
|
2019-09-18 04:44:05 +05:30
|
|
|
$response = $this->buildCompleteErrorResponse($e);
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* This method generates authorization_code and a link
|
|
|
|
* for the user's further redirect to the client's site.
|
2017-06-12 17:06:20 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* The input data are the same parameters that were necessary for validation request:
|
2017-06-12 17:06:20 +05:30
|
|
|
* $_GET = [
|
|
|
|
* client_id,
|
|
|
|
* redirect_uri,
|
|
|
|
* response_type,
|
|
|
|
* scope,
|
|
|
|
* state,
|
|
|
|
* ];
|
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Also, the accept field, which shows that the user has clicked on the "Accept" button.
|
|
|
|
* If the field is present, it will be interpreted as any value resulting in false positives.
|
|
|
|
* Otherwise, the value will be interpreted as "true".
|
2017-06-12 17:06:20 +05:30
|
|
|
*
|
2019-12-09 22:01:54 +05:30
|
|
|
* @param ServerRequestInterface $request
|
2017-06-12 17:06:20 +05:30
|
|
|
* @return array
|
|
|
|
*/
|
2019-12-09 22:01:54 +05:30
|
|
|
public function complete(ServerRequestInterface $request): array {
|
2017-06-12 17:06:20 +05:30
|
|
|
try {
|
2017-11-21 22:28:55 +05:30
|
|
|
Yii::$app->statsd->inc('oauth.complete.attempt');
|
2019-08-23 13:58:04 +05:30
|
|
|
|
|
|
|
$authRequest = $this->server->validateAuthorizationRequest($request);
|
2018-02-28 03:57:35 +05:30
|
|
|
/** @var Account $account */
|
2017-09-19 22:36:16 +05:30
|
|
|
$account = Yii::$app->user->identity->getAccount();
|
2019-09-18 04:44:05 +05:30
|
|
|
/** @var OauthClient $client */
|
|
|
|
$client = $this->findClient($authRequest->getClient()->getIdentifier());
|
2017-06-12 17:06:20 +05:30
|
|
|
|
2021-03-29 08:17:27 +05:30
|
|
|
$canBeAutoApproved = $this->canBeAutoApproved($account, $client, $authRequest);
|
|
|
|
$acceptParam = ((array)$request->getParsedBody())['accept'] ?? null;
|
|
|
|
if ($acceptParam === null && !$canBeAutoApproved) {
|
|
|
|
throw $this->createAcceptRequiredException();
|
|
|
|
}
|
2019-08-23 13:58:04 +05:30
|
|
|
|
2021-03-29 08:17:27 +05:30
|
|
|
Yii::$app->statsd->inc('oauth.complete.approve_required');
|
2017-06-12 17:06:20 +05:30
|
|
|
|
2021-03-29 08:17:27 +05:30
|
|
|
if ($acceptParam === null && $canBeAutoApproved) {
|
|
|
|
$approved = true;
|
|
|
|
} else {
|
2019-09-18 04:44:05 +05:30
|
|
|
$approved = in_array($acceptParam, [1, '1', true, 'true'], true);
|
2021-03-29 08:17:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if ($approved) {
|
|
|
|
$this->storeOauthSession($account, $client, $authRequest);
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2019-09-06 05:02:57 +05:30
|
|
|
$authRequest->setUser(new UserEntity($account->id));
|
2019-09-18 04:44:05 +05:30
|
|
|
$authRequest->setAuthorizationApproved($approved);
|
|
|
|
$response = $this->server->completeAuthorizationRequest($authRequest, new Response(200));
|
2019-08-23 13:58:04 +05:30
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
$result = [
|
2017-06-12 17:06:20 +05:30
|
|
|
'success' => true,
|
2019-09-18 04:44:05 +05:30
|
|
|
'redirectUri' => $response->getHeaderLine('Location'),
|
2017-06-12 17:06:20 +05:30
|
|
|
];
|
2019-08-23 13:58:04 +05:30
|
|
|
|
2017-11-21 22:28:55 +05:30
|
|
|
Yii::$app->statsd->inc('oauth.complete.success');
|
2019-08-23 13:58:04 +05:30
|
|
|
} catch (OAuthServerException $e) {
|
|
|
|
if ($e->getErrorType() === 'accept_required') {
|
2021-03-29 08:17:27 +05:30
|
|
|
// TODO: revoke access if there previously was an oauth session?
|
2017-11-21 22:28:55 +05:30
|
|
|
Yii::$app->statsd->inc('oauth.complete.fail');
|
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
$result = $this->buildCompleteErrorResponse($e);
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
return $result;
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* The method is executed by the application server to which auth_token or refresh_token was given.
|
2017-06-12 17:06:20 +05:30
|
|
|
*
|
2019-07-15 04:29:56 +05:30
|
|
|
* Input data is a standard list of POST parameters according to the OAuth2 standard:
|
2018-01-02 23:15:04 +05:30
|
|
|
* $_POST = [
|
2017-06-12 17:06:20 +05:30
|
|
|
* client_id,
|
|
|
|
* client_secret,
|
|
|
|
* redirect_uri,
|
|
|
|
* code,
|
|
|
|
* grant_type,
|
|
|
|
* ]
|
2019-07-15 04:29:56 +05:30
|
|
|
* for request with grant_type = authentication_code:
|
2018-01-02 23:15:04 +05:30
|
|
|
* $_POST = [
|
2017-06-12 17:06:20 +05:30
|
|
|
* client_id,
|
|
|
|
* client_secret,
|
|
|
|
* refresh_token,
|
|
|
|
* grant_type,
|
|
|
|
* ]
|
|
|
|
*
|
2019-12-09 22:01:54 +05:30
|
|
|
* @param ServerRequestInterface $request
|
2017-06-12 17:06:20 +05:30
|
|
|
* @return array
|
|
|
|
*/
|
2019-12-09 22:01:54 +05:30
|
|
|
public function getToken(ServerRequestInterface $request): array {
|
2019-08-23 13:58:04 +05:30
|
|
|
$params = (array)$request->getParsedBody();
|
2019-09-22 02:47:21 +05:30
|
|
|
$clientId = $params['client_id'] ?? '';
|
2019-08-23 13:58:04 +05:30
|
|
|
$grantType = $params['grant_type'] ?? 'null';
|
2017-06-12 17:06:20 +05:30
|
|
|
try {
|
2018-01-02 23:15:04 +05:30
|
|
|
Yii::$app->statsd->inc("oauth.issueToken_{$grantType}.attempt");
|
2019-08-23 13:58:04 +05:30
|
|
|
|
2019-12-09 22:01:54 +05:30
|
|
|
$shouldIssueRefreshToken = false;
|
|
|
|
$this->server->getEmitter()->addOneTimeListener(RequestedRefreshToken::class, function() use (&$shouldIssueRefreshToken) {
|
|
|
|
$shouldIssueRefreshToken = true;
|
|
|
|
});
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
$response = $this->server->respondToAccessTokenRequest($request, new Response(200));
|
2019-12-09 22:01:54 +05:30
|
|
|
/** @noinspection JsonEncodingApiUsageInspection at this point json error is not possible */
|
2019-09-22 02:47:21 +05:30
|
|
|
$result = json_decode((string)$response->getBody(), true);
|
2019-12-09 22:01:54 +05:30
|
|
|
if ($shouldIssueRefreshToken) {
|
|
|
|
// Set the refresh_token field to keep compatibility with the old clients,
|
|
|
|
// which will be broken in case when refresh_token field will be missing
|
|
|
|
$result['refresh_token'] = $result['access_token'];
|
|
|
|
}
|
2019-08-23 13:58:04 +05:30
|
|
|
|
2019-12-11 22:14:55 +05:30
|
|
|
if (($result['expires_in'] ?? 0) <= 0) {
|
2019-12-13 18:30:51 +05:30
|
|
|
if ($shouldIssueRefreshToken || $grantType === 'refresh_token') {
|
|
|
|
// Since some of our clients use this field to understand how long the token will live,
|
|
|
|
// we have to give it some value. The tokens with zero lifetime don't expire
|
|
|
|
// but in order not to break the clients storing the value as integer on 32-bit systems,
|
|
|
|
// let's calculate the value based on the unsigned maximum for this type
|
|
|
|
$result['expires_in'] = 2 ** 31 - time();
|
|
|
|
} else {
|
|
|
|
unset($result['expires_in']);
|
|
|
|
}
|
2019-12-11 22:14:55 +05:30
|
|
|
}
|
|
|
|
|
2018-01-02 23:15:04 +05:30
|
|
|
Yii::$app->statsd->inc("oauth.issueToken_client.{$clientId}");
|
|
|
|
Yii::$app->statsd->inc("oauth.issueToken_{$grantType}.success");
|
2019-08-23 13:58:04 +05:30
|
|
|
} catch (OAuthServerException $e) {
|
2018-01-02 23:15:04 +05:30
|
|
|
Yii::$app->statsd->inc("oauth.issueToken_{$grantType}.fail");
|
2019-08-23 13:58:04 +05:30
|
|
|
Yii::$app->response->statusCode = $e->getHttpStatusCode();
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
$result = $this->buildIssueErrorResponse($e);
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2019-09-22 02:47:21 +05:30
|
|
|
return $result;
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
private function findClient(string $clientId): ?OauthClient {
|
2019-08-23 13:58:04 +05:30
|
|
|
return OauthClient::findOne(['id' => $clientId]);
|
2018-02-28 03:57:35 +05:30
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
/**
|
2019-07-15 04:29:56 +05:30
|
|
|
* The method checks whether the current user can be automatically authorized for the specified client
|
|
|
|
* without requesting access to the necessary list of scopes
|
2017-06-12 17:06:20 +05:30
|
|
|
*
|
2017-06-17 23:35:36 +05:30
|
|
|
* @param Account $account
|
2017-06-12 17:06:20 +05:30
|
|
|
* @param OauthClient $client
|
2019-08-23 13:58:04 +05:30
|
|
|
* @param AuthorizationRequest $request
|
2017-06-12 17:06:20 +05:30
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-03-29 08:17:27 +05:30
|
|
|
private function canBeAutoApproved(Account $account, OauthClient $client, AuthorizationRequest $request): bool {
|
2017-06-12 17:06:20 +05:30
|
|
|
if ($client->is_trusted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
$session = $this->findOauthSession($account, $client);
|
|
|
|
if ($session === null) {
|
|
|
|
return false;
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2020-09-30 23:00:04 +05:30
|
|
|
if ($session->isRevoked()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
return empty(array_diff($this->getScopesList($request), $session->getScopes()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function storeOauthSession(Account $account, OauthClient $client, AuthorizationRequest $request): void {
|
|
|
|
$session = $this->findOauthSession($account, $client);
|
|
|
|
if ($session === null) {
|
|
|
|
$session = new OauthSession();
|
|
|
|
$session->account_id = $account->id;
|
|
|
|
$session->client_id = $client->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$session->scopes = array_unique(array_merge($session->getScopes(), $this->getScopesList($request)));
|
2020-09-30 23:00:04 +05:30
|
|
|
$session->last_used_at = time();
|
2019-09-18 04:44:05 +05:30
|
|
|
|
|
|
|
Assert::true($session->save());
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2017-06-17 23:35:36 +05:30
|
|
|
/**
|
2019-08-23 13:58:04 +05:30
|
|
|
* @param ServerRequestInterface $request
|
2017-06-17 23:35:36 +05:30
|
|
|
* @param OauthClient $client
|
2019-09-18 04:44:05 +05:30
|
|
|
* @param ScopeEntityInterface[] $scopes
|
2017-06-17 23:35:36 +05:30
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-08-23 13:58:04 +05:30
|
|
|
private function buildSuccessResponse(ServerRequestInterface $request, OauthClient $client, array $scopes): array {
|
2017-06-12 17:06:20 +05:30
|
|
|
return [
|
|
|
|
'success' => true,
|
2019-07-15 04:29:56 +05:30
|
|
|
// We return only those keys which are related to the OAuth2 standard parameters
|
2019-08-23 13:58:04 +05:30
|
|
|
'oAuth' => array_intersect_key($request->getQueryParams(), array_flip([
|
2017-06-12 17:06:20 +05:30
|
|
|
'client_id',
|
|
|
|
'redirect_uri',
|
|
|
|
'response_type',
|
|
|
|
'scope',
|
|
|
|
'state',
|
|
|
|
])),
|
|
|
|
'client' => [
|
|
|
|
'id' => $client->id,
|
|
|
|
'name' => $client->name,
|
2019-08-23 13:58:04 +05:30
|
|
|
'description' => $request->getQueryParams()['description'] ?? $client->description,
|
2017-06-12 17:06:20 +05:30
|
|
|
],
|
|
|
|
'session' => [
|
2019-08-23 13:58:04 +05:30
|
|
|
'scopes' => $this->buildScopesArray($scopes),
|
2017-06-12 17:06:20 +05:30
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
/**
|
2019-09-18 04:44:05 +05:30
|
|
|
* @param ScopeEntityInterface[] $scopes
|
2019-08-23 13:58:04 +05:30
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function buildScopesArray(array $scopes): array {
|
|
|
|
$result = [];
|
|
|
|
foreach ($scopes as $scope) {
|
|
|
|
$result[] = self::INTERNAL_PERMISSIONS_TO_PUBLIC_SCOPES[$scope->getIdentifier()] ?? $scope->getIdentifier();
|
2017-10-18 05:07:01 +05:30
|
|
|
}
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
return $result;
|
2017-10-18 05:07:01 +05:30
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
private function buildCompleteErrorResponse(OAuthServerException $e): array {
|
2019-09-13 03:49:03 +05:30
|
|
|
$hint = $e->getPayload()['hint'] ?? '';
|
|
|
|
if (preg_match('/the `(\w+)` scope/', $hint, $matches)) {
|
|
|
|
$parameter = $matches[1];
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
$response = [
|
|
|
|
'success' => false,
|
2019-08-23 13:58:04 +05:30
|
|
|
'error' => $e->getErrorType(),
|
2019-09-13 03:49:03 +05:30
|
|
|
'parameter' => $parameter ?? null,
|
2019-08-23 13:58:04 +05:30
|
|
|
'statusCode' => $e->getHttpStatusCode(),
|
2017-06-12 17:06:20 +05:30
|
|
|
];
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
if ($e->hasRedirect()) {
|
2017-06-12 17:06:20 +05:30
|
|
|
$response['redirectUri'] = $e->getRedirectUri();
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
if ($e->getHttpStatusCode() !== 200) {
|
|
|
|
Yii::$app->response->setStatusCode($e->getHttpStatusCode());
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
/**
|
|
|
|
* Raw error messages aren't very informative for the end user, as they don't contain
|
|
|
|
* information about the parameter that caused the error.
|
|
|
|
* This method is intended to build a more understandable description.
|
|
|
|
*
|
2019-09-22 02:47:21 +05:30
|
|
|
* Part of the existing texts are the legacy from the previous implementation.
|
2019-09-18 04:44:05 +05:30
|
|
|
*
|
|
|
|
* @param OAuthServerException $e
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function buildIssueErrorResponse(OAuthServerException $e): array {
|
|
|
|
$errorType = $e->getErrorType();
|
|
|
|
$message = $e->getMessage();
|
|
|
|
$hint = $e->getHint();
|
|
|
|
switch ($hint) {
|
|
|
|
case 'Invalid redirect URI':
|
|
|
|
$errorType = 'invalid_client';
|
|
|
|
$message = 'Client authentication failed.';
|
|
|
|
break;
|
|
|
|
case 'Cannot decrypt the authorization code':
|
|
|
|
$message .= ' Check the "code" parameter.';
|
2019-09-22 02:47:21 +05:30
|
|
|
break;
|
2019-09-18 04:44:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'error' => $errorType,
|
|
|
|
'message' => $message,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
private function createAcceptRequiredException(): OAuthServerException {
|
2019-12-11 01:21:11 +05:30
|
|
|
return new OAuthServerException('Client must accept authentication request.', 0, 'accept_required', 401);
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2019-09-18 04:44:05 +05:30
|
|
|
private function getScopesList(AuthorizationRequest $request): array {
|
2019-12-15 19:50:29 +05:30
|
|
|
return array_values(array_map(function(ScopeEntityInterface $scope): string {
|
2019-09-18 04:44:05 +05:30
|
|
|
return $scope->getIdentifier();
|
2019-12-15 19:50:29 +05:30
|
|
|
}, $request->getScopes()));
|
2019-09-18 04:44:05 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private function findOauthSession(Account $account, OauthClient $client): ?OauthSession {
|
|
|
|
return $account->getOauthSessions()->andWhere(['client_id' => $client->id])->one();
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|