]> git.mxchange.org Git - friendica.git/commitdiff
Refactor OAuth Token
authorArt4 <art4@wlabs.de>
Mon, 18 Nov 2024 08:02:22 +0000 (08:02 +0000)
committerArt4 <art4@wlabs.de>
Mon, 18 Nov 2024 08:02:22 +0000 (08:02 +0000)
src/BaseModule.php
src/Module/BaseApi.php
src/Module/OAuth/Token.php

index 5f72a0f79b0c003e79f7cacb7dfe2a57fa3fe7a9..e0c97afce2d6278c0c7d43023241941f76ec154d 100644 (file)
@@ -456,7 +456,7 @@ abstract class BaseModule implements ICanHandleRequests
         * @param string      $content
         * @param string      $type
         * @param string|null $content_type
-        * @return void
+        * @return never
         * @throws HTTPException\InternalServerErrorException
         */
        public function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null)
@@ -493,7 +493,7 @@ abstract class BaseModule implements ICanHandleRequests
         * @param mixed  $content
         * @param string $content_type
         * @param int    $options A combination of json_encode() binary flags
-        * @return void
+        * @return never
         * @throws HTTPException\InternalServerErrorException
         * @see json_encode()
         */
@@ -508,7 +508,7 @@ abstract class BaseModule implements ICanHandleRequests
         * @param int    $httpCode
         * @param mixed  $content
         * @param string $content_type
-        * @return void
+        * @return never
         * @throws HTTPException\InternalServerErrorException
         */
        public function jsonError(int $httpCode, $content, string $content_type = 'application/json')
index 4b3b817816d0ecd6c8460a5e95471b2ead366e10..f457d64ec95473f86987bfd29063d185a9b10e51 100644 (file)
@@ -509,7 +509,7 @@ class BaseApi extends BaseModule
        /**
         * @param int   $errorno
         * @param Error $error
-        * @return void
+        * @return never
         * @throws HTTPException\InternalServerErrorException
         */
        protected function logAndJsonError(int $errorno, Error $error)
index 47efa266e6d4bc72c7a48b121374ed7886d414ea..9ce760790bf3a82a4843b7889587db9e2460e30f 100644 (file)
@@ -66,32 +66,52 @@ class Token extends BaseApi
                        $this->logAndJsonError(401, $this->errorFactory->Unauthorized('invalid_client', $this->t('Invalid data or unknown client')));
                }
 
-               if ($request['grant_type'] == 'client_credentials') {
+               $grant_type = (string) $request['grant_type'];
+
+               if (!in_array($grant_type, ['client_credentials', 'authorization_code'])) {
+                       Logger::warning('Unsupported or missing grant type', ['request' => $_REQUEST]);
+                       $this->logAndJsonError(422, $this->errorFactory->UnprocessableEntity($this->t('Unsupported or missing grant type')));
+               }
+
+               if ($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
-                       $redirect_uri = strtok($request['redirect_uri'],'?');
-                       $condition = [
-                               "`redirect_uri` LIKE ? AND `id` = ? AND `code` = ? AND `created_at` > ?",
-                               $redirect_uri, $application['id'], $request['code'], DateTimeFormat::utc('now - 5 minutes')
-                       ];
-
-                       $token = DBA::selectFirst('application-view', ['access_token', 'created_at', 'uid'], $condition);
-                       if (!DBA::isResult($token)) {
-                               $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]);
-                       $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'],
+                               null
+                       );
+
+                       $this->jsonExit($object->toArray());
                }
 
-               $object = new \Friendica\Object\Api\Mastodon\Token($token['access_token'], 'Bearer', $application['scopes'], $token['created_at'], $me);
+               // now check for $grant_type === 'authorization_code'
+               // For security reasons only allow freshly created tokens
+               $redirect_uri = strtok($request['redirect_uri'],'?');
+               $condition = [
+                       "`redirect_uri` LIKE ? AND `id` = ? AND `code` = ? AND `created_at` > ?",
+                       $redirect_uri, $application['id'], $request['code'], DateTimeFormat::utc('now - 5 minutes')
+               ];
+
+               $token = DBA::selectFirst('application-view', ['access_token', 'created_at', 'uid'], $condition);
+               if (!DBA::isResult($token)) {
+                       $this->logger->notice('Token not found or outdated', $condition);
+                       $this->logAndJsonError(401, $this->errorFactory->Unauthorized());
+               }
+
+               $owner = User::getOwnerDataById($token['uid']);
+
+               $object = new \Friendica\Object\Api\Mastodon\Token(
+                       $token['access_token'],
+                       'Bearer',
+                       $application['scopes'],
+                       $token['created_at'],
+                       $owner['url']
+               );
 
                $this->jsonExit($object->toArray());
        }