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-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-08-23 13:58:04 +05:30
|
|
|
use GuzzleHttp\Psr7\Response;
|
|
|
|
use GuzzleHttp\Psr7\ServerRequest;
|
2017-06-12 17:06:20 +05:30
|
|
|
use League\OAuth2\Server\AuthorizationServer;
|
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;
|
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',
|
|
|
|
];
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
/**
|
|
|
|
* @var AuthorizationServer
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function validate(): array {
|
|
|
|
try {
|
2019-08-23 13:58:04 +05:30
|
|
|
$request = $this->getRequest();
|
|
|
|
$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) {
|
2017-06-12 17:06:20 +05:30
|
|
|
$response = $this->buildErrorResponse($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function complete(): array {
|
|
|
|
try {
|
2017-11-21 22:28:55 +05:30
|
|
|
Yii::$app->statsd->inc('oauth.complete.attempt');
|
2019-08-23 13:58:04 +05:30
|
|
|
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$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-08-23 13:58:04 +05:30
|
|
|
/** @var OauthClient $clientModel */
|
|
|
|
$clientModel = $this->findClient($authRequest->getClient()->getIdentifier());
|
2017-06-12 17:06:20 +05:30
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
if (!$this->canAutoApprove($account, $clientModel, $authRequest)) {
|
2017-11-21 22:28:55 +05:30
|
|
|
Yii::$app->statsd->inc('oauth.complete.approve_required');
|
2019-08-23 13:58:04 +05:30
|
|
|
|
|
|
|
$accept = ((array)$request->getParsedBody())['accept'] ?? null;
|
|
|
|
if ($accept === null) {
|
|
|
|
throw $this->createAcceptRequiredException();
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
if (!in_array($accept, [1, '1', true, 'true'], true)) {
|
|
|
|
throw OAuthServerException::accessDenied(null, $authRequest->getRedirectUri());
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 05:02:57 +05:30
|
|
|
$authRequest->setUser(new UserEntity($account->id));
|
2019-08-23 13:58:04 +05:30
|
|
|
$responseObj = $this->server->completeAuthorizationRequest($authRequest, new Response(200));
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
$response = [
|
|
|
|
'success' => true,
|
2019-08-23 13:58:04 +05:30
|
|
|
'redirectUri' => $responseObj->getHeader('Location'), // TODO: ensure that this is correct type and behavior
|
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') {
|
2017-11-21 22:28:55 +05:30
|
|
|
Yii::$app->statsd->inc('oauth.complete.fail');
|
|
|
|
}
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
$response = $this->buildErrorResponse($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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,
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getToken(): array {
|
2019-08-23 13:58:04 +05:30
|
|
|
$request = $this->getRequest();
|
|
|
|
$params = (array)$request->getParsedBody();
|
|
|
|
$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
|
|
|
|
|
|
|
$responseObj = new Response(200);
|
|
|
|
$this->server->respondToAccessTokenRequest($request, $responseObj);
|
|
|
|
$clientId = $params['client_id'];
|
|
|
|
|
|
|
|
// TODO: build response from the responseObj
|
|
|
|
$response = [];
|
|
|
|
|
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();
|
|
|
|
|
2017-06-12 17:06:20 +05:30
|
|
|
$response = [
|
2019-08-23 13:58:04 +05:30
|
|
|
'error' => $e->getErrorType(),
|
|
|
|
'message' => $e->getMessage(), // TODO: use hint field?
|
2017-06-12 17:06:20 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2019-08-23 13:58:04 +05:30
|
|
|
private function canAutoApprove(Account $account, OauthClient $client, AuthorizationRequest $request): bool {
|
2017-06-12 17:06:20 +05:30
|
|
|
if ($client->is_trusted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \common\models\OauthSession|null $session */
|
|
|
|
$session = $account->getOauthSessions()->andWhere(['client_id' => $client->id])->one();
|
|
|
|
if ($session !== null) {
|
|
|
|
$existScopes = $session->getScopes()->members();
|
2019-08-23 13:58:04 +05:30
|
|
|
if (empty(array_diff(array_keys($request->getScopes()), $existScopes))) {
|
2017-06-12 17:06:20 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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-08-23 13:58:04 +05:30
|
|
|
* @param \League\OAuth2\Server\Entities\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
|
|
|
/**
|
|
|
|
* @param \League\OAuth2\Server\Entities\ScopeEntityInterface[] $scopes
|
|
|
|
* @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-08-23 13:58:04 +05:30
|
|
|
private function buildErrorResponse(OAuthServerException $e): array {
|
2017-06-12 17:06:20 +05:30
|
|
|
$response = [
|
|
|
|
'success' => false,
|
2019-08-23 13:58:04 +05:30
|
|
|
'error' => $e->getErrorType(),
|
|
|
|
// 'parameter' => $e->parameter, // TODO: if this is necessary, the parameter can be extracted from the hint
|
|
|
|
'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-08-23 13:58:04 +05:30
|
|
|
private function getRequest(): ServerRequestInterface {
|
|
|
|
return ServerRequest::fromGlobals();
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
2019-08-23 13:58:04 +05:30
|
|
|
private function createAcceptRequiredException(): OAuthServerException {
|
|
|
|
return new OAuthServerException(
|
|
|
|
'Client must accept authentication request.',
|
|
|
|
0,
|
|
|
|
'accept_required',
|
|
|
|
401
|
|
|
|
);
|
2017-06-12 17:06:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|