]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/OAuth/Token.php
Rename BaseApi->logErrorAndJsonExit to logAndJsonError to better match the functionality
[friendica.git] / src / Module / OAuth / Token.php
index 8bbb272c31d45f8063998b3c021a084332b37c5e..7e22a88dabff6c588533c7a504d2309caa71e96e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -25,18 +25,28 @@ use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\DI;
+use Friendica\Model\User;
 use Friendica\Module\BaseApi;
+use Friendica\Module\Special\HTTPException;
 use Friendica\Security\OAuth;
+use Friendica\Util\DateTimeFormat;
+use GuzzleHttp\Psr7\Uri;
+use Psr\Http\Message\ResponseInterface;
 
 /**
- * @see https://docs.joinmastodon.org/spec/oauth/
+ * @see https://docs.joinmastodon.org/methods/oauth/#token
  * @see https://aaronparecki.com/oauth-2-simplified/
  */
 class Token extends BaseApi
 {
-       protected function post(array $request = [], array $post = [])
+       public function run(HTTPException $httpException, array $request = [], bool $scopecheck = true): ResponseInterface
        {
-               $request = self::getRequest([
+               return parent::run($httpException, $request, false);
+       }
+
+       protected function post(array $request = [])
+       {
+               $request = $this->getRequest([
                        'client_id'     => '', // Client ID, obtained during app registration
                        'client_secret' => '', // Client secret, obtained during app registration
                        'redirect_uri'  => '', // Set a URI to redirect the user to. If this parameter is set to "urn:ietf:wg:oauth:2.0:oob" then the token will be shown instead. Must match one of the redirect URIs declared during app registration.
@@ -52,8 +62,11 @@ class Token extends BaseApi
                        $authorization = $_SERVER['REDIRECT_REMOTE_USER'] ?? '';
                }
 
-               if (empty($request['client_id']) && substr($authorization, 0, 6) == 'Basic ') {
-                       $datapair = explode(':', base64_decode(trim(substr($authorization, 6))));
+               if ((empty($request['client_id']) || empty($request['client_secret'])) && substr($authorization, 0, 6) == 'Basic ') {
+                       // Per RFC2617, usernames can't contain a colon but password can,
+                       // so we cut on the first colon to obtain the username and the password
+                       // @see https://www.rfc-editor.org/rfc/rfc2617#section-2
+                       $datapair = explode(':', base64_decode(trim(substr($authorization, 6))), 2);
                        if (count($datapair) == 2) {
                                $request['client_id']     = $datapair[0];
                                $request['client_secret'] = $datapair[1];
@@ -61,36 +74,42 @@ class Token extends BaseApi
                }
 
                if (empty($request['client_id']) || empty($request['client_secret'])) {
-                       Logger::warning('Incomplete request data', ['request' => $_REQUEST]);
-                       DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Incomplete request data'));
+                       $this->logger->warning('Incomplete request data', ['request' => $request]);
+                       $this->logAndJsonError(401, $this->errorFactory->Unauthorized('invalid_client', $this->t('Incomplete request data')));;
                }
 
                $application = OAuth::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']);
                if (empty($application)) {
-                       DI::mstdnError()->UnprocessableEntity();
+                       $this->logAndJsonError(401, $this->errorFactory->Unauthorized('invalid_client', $this->t('Invalid data or unknown client')));
                }
 
                if ($request['grant_type'] == 'client_credentials') {
                        // the "client_credentials" are used as a token for the application itself.
                        // see https://aaronparecki.com/oauth-2-simplified/#client-credentials
                        $token = OAuth::createTokenForUser($application, 0, '');
+                       $me = null;
                } elseif ($request['grant_type'] == 'authorization_code') {
                        // For security reasons only allow freshly created tokens
-                       $condition = ["`redirect_uri` = ? AND `id` = ? AND `code` = ? AND `created_at` > UTC_TIMESTAMP() - INTERVAL ? MINUTE",
-                               $request['redirect_uri'], $application['id'], $request['code'], 5];
+                       $uri = new Uri($request['redirect_uri']);
+                       $condition = [
+                               "`redirect_uri` LIKE ? AND `id` = ? AND `code` = ? AND `created_at` > ?",
+                               '%' . $uri->getScheme() . '://' . $uri->getHost() . $uri->getPath() . '%', $application['id'], $request['code'], DateTimeFormat::utc('now - 5 minutes')
+                       ];
 
-                       $token = DBA::selectFirst('application-view', ['access_token', 'created_at'], $condition);
+                       $token = DBA::selectFirst('application-view', ['access_token', 'created_at', 'uid'], $condition);
                        if (!DBA::isResult($token)) {
-                               Logger::warning('Token not found or outdated', $condition);
-                               DI::mstdnError()->Unauthorized();
+                               $this->logger->notice('Token not found or outdated', $condition);
+                               $this->logAndJsonError(401, $this->errorFactory->Unauthorized());
                        }
+                       $owner = User::getOwnerDataById($token['uid']);
+                       $me = $owner['url'];
                } else {
                        Logger::warning('Unsupported or missing grant type', ['request' => $_REQUEST]);
-                       DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing grant type'));
+                       $this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity($this->t('Unsupported or missing grant type')));
                }
 
-               $object = new \Friendica\Object\Api\Mastodon\Token($token['access_token'], 'Bearer', $application['scopes'], $token['created_at']);
+               $object = new \Friendica\Object\Api\Mastodon\Token($token['access_token'], 'Bearer', $application['scopes'], $token['created_at'], $me);
 
-               System::jsonExit($object->toArray());
+               $this->jsonExit($object->toArray());
        }
 }