]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/OAuth/Token.php
API: Show different ids on reshares / don't check for client secret
[friendica.git] / src / Module / OAuth / Token.php
index 6aef63f302940487450698d8bde4264f5e29b966..2752c69a6d70b0d16ca8e66f82f1f63b64a42a6d 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
  *
@@ -26,24 +26,32 @@ use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Module\BaseApi;
+use Friendica\Module\Special\HTTPException;
 use Friendica\Security\OAuth;
+use Friendica\Util\DateTimeFormat;
+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
 {
-       public function 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.
                        'scope'         => 'read', // List of requested OAuth scopes, separated by spaces. Must be a subset of scopes declared during app registration. If not provided, defaults to "read".
                        'code'          => '', // A user authorization code, obtained via /oauth/authorize
                        'grant_type'    => '', // Set equal to "authorization_code" if code is provided in order to gain user-level access. Otherwise, set equal to "client_credentials" to obtain app-level access only.
-               ]);
+               ], $request);
 
                // AndStatus transmits the client data in the AUTHORIZATION header field, see https://github.com/andstatus/andstatus/issues/530
                $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
@@ -60,14 +68,15 @@ 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'));
+               // "client_secret" is required for "client_credentials": https://www.oauth.com/oauth2-servers/access-tokens/client-credentials/
+               if (empty($request['client_id']) || (($request['grant_type'] == 'client_credentials') && empty($request['client_secret']))) {
+                       Logger::warning('Incomplete request data', ['request' => $request]);
+                       DI::mstdnError()->Unauthorized('invalid_client', DI::l10n()->t('Incomplete request data'));
                }
 
                $application = OAuth::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']);
                if (empty($application)) {
-                       DI::mstdnError()->UnprocessableEntity();
+                       DI::mstdnError()->Unauthorized('invalid_client', DI::l10n()->t('Invalid data or unknown client'));
                }
 
                if ($request['grant_type'] == 'client_credentials') {
@@ -76,12 +85,12 @@ class Token extends BaseApi
                        $token = OAuth::createTokenForUser($application, 0, '');
                } 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];
+                       $condition = ["`redirect_uri` = ? AND `id` = ? AND `code` = ? AND `created_at` > ?",
+                               $request['redirect_uri'], $application['id'], $request['code'], DateTimeFormat::utc('now - 5 minutes')];
 
                        $token = DBA::selectFirst('application-view', ['access_token', 'created_at'], $condition);
                        if (!DBA::isResult($token)) {
-                               Logger::warning('Token not found or outdated', $condition);
+                               Logger::notice('Token not found or outdated', $condition);
                                DI::mstdnError()->Unauthorized();
                        }
                } else {