]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/OAuth/Token.php
Update copyright
[friendica.git] / src / Module / OAuth / Token.php
index 1d0b43cd67fecebe8552875cdf8fd3a2b1f33bb1..d41c056dfab00af847433934b4f760c34601f63d 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -26,42 +26,71 @@ use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Module\BaseApi;
+use Friendica\Security\OAuth;
 
 /**
- * Dummy class for all currently unimplemented endpoints
+ * @see https://docs.joinmastodon.org/spec/oauth/
+ * @see https://aaronparecki.com/oauth-2-simplified/
  */
 class Token extends BaseApi
 {
-       public static function post(array $parameters = [])
+       protected function post(array $request = [])
        {
-               $client_secret = !isset($_REQUEST['client_secret']) ? '' : $_REQUEST['client_secret'];
-               $code          = !isset($_REQUEST['code']) ? '' : $_REQUEST['code'];
-               $grant_type    = !isset($_REQUEST['grant_type']) ? '' : $_REQUEST['grant_type'];
+               $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);
 
-               if ($grant_type != 'authorization_code') {
-                       Logger::warning('Wrong or missing grant type', ['grant_type' => $grant_type]);
-                       DI::mstdnError()->RecordNotFound();
+               // AndStatus transmits the client data in the AUTHORIZATION header field, see https://github.com/andstatus/andstatus/issues/530
+               $authorization = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
+               if (empty($authorization)) {
+                       // workaround for HTTP-auth in CGI mode
+                       $authorization = $_SERVER['REDIRECT_REMOTE_USER'] ?? '';
                }
 
-               $application = self::getApplication();
-               if (empty($application)) {
-                       DI::mstdnError()->RecordNotFound();
+               if (empty($request['client_id']) && substr($authorization, 0, 6) == 'Basic ') {
+                       $datapair = explode(':', base64_decode(trim(substr($authorization, 6))));
+                       if (count($datapair) == 2) {
+                               $request['client_id']     = $datapair[0];
+                               $request['client_secret'] = $datapair[1];
+                       }
                }
 
-               if ($application['client_secret'] != $client_secret) {
-                       Logger::warning('Wrong client secret', $client_secret);
-                       DI::mstdnError()->RecordNotFound();
+               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'));
                }
 
-               $condition = ['application-id' => $application['id'], 'code' => $code];
+               $application = OAuth::getApplication($request['client_id'], $request['client_secret'], $request['redirect_uri']);
+               if (empty($application)) {
+                       DI::mstdnError()->UnprocessableEntity();
+               }
 
-               $token = DBA::selectFirst('application-token', ['access_token', 'created_at'], $condition);
-               if (!DBA::isResult($token)) {
-                       Logger::warning('Token not found', $condition);
-                       DI::mstdnError()->RecordNotFound();
+               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, '');
+               } 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];
+
+                       $token = DBA::selectFirst('application-view', ['access_token', 'created_at'], $condition);
+                       if (!DBA::isResult($token)) {
+                               Logger::warning('Token not found or outdated', $condition);
+                               DI::mstdnError()->Unauthorized();
+                       }
+               } else {
+                       Logger::warning('Unsupported or missing grant type', ['request' => $_REQUEST]);
+                       DI::mstdnError()->UnprocessableEntity(DI::l10n()->t('Unsupported or missing grant type'));
                }
 
-               // @todo Use entity class
-               System::jsonExit(['access_token' => $token['access_token'], 'token_type' => 'Bearer', 'scope' => $application['scopes'], 'created_at' => $token['created_at']]);
+               $object = new \Friendica\Object\Api\Mastodon\Token($token['access_token'], 'Bearer', $application['scopes'], $token['created_at']);
+
+               System::jsonExit($object->toArray());
        }
 }