]> git.mxchange.org Git - friendica.git/commitdiff
Preparation for Oauth client registration
authorMichael <heluecht@pirati.ca>
Sun, 9 May 2021 22:23:21 +0000 (22:23 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 9 May 2021 22:23:21 +0000 (22:23 +0000)
database.sql
src/DI.php
src/Factory/Api/Mastodon/Application.php [new file with mode: 0644]
src/Module/Api/Mastodon/Apps.php [new file with mode: 0644]
src/Object/Api/Mastodon/Application.php
static/dbstructure.config.php
static/routes.config.php

index 7a18fad5cbb397878765c14520aa5de7228c58b2..666a48f66786089cdd74531e57886ae886410025 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2021.06-dev (Siberian Iris)
--- DB_UPDATE_VERSION 1414
+-- DB_UPDATE_VERSION 1415
 -- ------------------------------------------
 
 
@@ -364,6 +364,21 @@ CREATE TABLE IF NOT EXISTS `apcontact` (
        FOREIGN KEY (`gsid`) REFERENCES `gserver` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT
 ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='ActivityPub compatible contacts - used in the ActivityPub implementation';
 
+--
+-- TABLE application
+--
+CREATE TABLE IF NOT EXISTS `application` (
+       `id` int unsigned NOT NULL auto_increment COMMENT 'generated index',
+       `client_id` varchar(64) NOT NULL COMMENT '',
+       `client_secret` varchar(64) NOT NULL COMMENT '',
+       `name` varchar(255) NOT NULL COMMENT '',
+       `redirect_uri` varchar(255) NOT NULL COMMENT '',
+       `website` varchar(255) COMMENT '',
+       `scopes` varchar(255) COMMENT '',
+        PRIMARY KEY(`id`),
+        UNIQUE INDEX `client_id` (`client_id`)
+) DEFAULT COLLATE utf8mb4_general_ci COMMENT='OAuth application';
+
 --
 -- TABLE attach
 --
index 987cc4fbed4efe8d24da6af9af9bcedb09acab64..37091a5ab82d5d439f525279975ef054fa508860 100644 (file)
@@ -239,6 +239,14 @@ abstract class DI
                return self::$dice->create(Factory\Api\Mastodon\Account::class);
        }
 
+       /**
+        * @return Factory\Api\Mastodon\Application
+        */
+       public static function mstdnApplication()
+       {
+               return self::$dice->create(Factory\Api\Mastodon\Application::class);
+       }
+
        /**
         * @return Factory\Api\Mastodon\Attachment
         */
diff --git a/src/Factory/Api/Mastodon/Application.php b/src/Factory/Api/Mastodon/Application.php
new file mode 100644 (file)
index 0000000..b45f4e9
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Factory\Api\Mastodon;
+
+use Friendica\BaseFactory;
+use Friendica\Database\DBA;
+
+class Application extends BaseFactory
+{
+       /**
+        * @param int $id Application ID
+        */
+       public function createFromApplicationId(int $id)
+       {
+               $application = DBA::selectFirst('application', ['client_id', 'client_secret', 'id', 'name', 'redirect_uri', 'website'], ['id' => $id]);
+               if (!DBA::isResult($application)) {
+                       return [];
+               }
+
+               $object = new \Friendica\Object\Api\Mastodon\Application(
+                       $application['name'], 
+                       $application['client_id'], 
+                       $application['client_secret'], 
+                       $application['id'], 
+                       $application['redirect_uri'], 
+                       $application['website']);
+
+               return $object->toArray();
+       }
+}
diff --git a/src/Module/Api/Mastodon/Apps.php b/src/Module/Api/Mastodon/Apps.php
new file mode 100644 (file)
index 0000000..36bf4ba
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Module\Api\Mastodon;
+
+use Friendica\Core\System;
+use Friendica\Database\DBA;
+use Friendica\DI;
+use Friendica\Module\BaseApi;
+
+/**
+ * Apps class to register new OAuth clients
+ */
+class Apps extends BaseApi
+{
+       /**
+        * @param array $parameters
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        */
+       public static function post(array $parameters = [])
+       {
+               $name = !isset($_REQUEST['client_name']) ? '' : $_REQUEST['client_name'];
+               $redirect = !isset($_REQUEST['redirect_uris']) ? '' : $_REQUEST['redirect_uris'];
+               $scopes = !isset($_REQUEST['scopes']) ? '' : $_REQUEST['scopes'];
+               $website = !isset($_REQUEST['website']) ? '' : $_REQUEST['website'];
+
+               if (empty($name) || empty($redirect)) {
+                       DI::mstdnError()->RecordNotFound();
+               }
+
+               $client_id = base64_encode(openssl_random_pseudo_bytes(32));
+               $client_secret = bin2hex(random_bytes(32));
+
+               $fields = ['client_id' => $client_id, 'client_secret' => $client_secret, 'name' => $name, 'redirect_uri' => $redirect];
+
+               if (!empty($scopes)) {
+                       $fields['scopes'] = $scopes;
+               }
+
+               if (!empty($website)) {
+                       $fields['website'] = $website;
+               }
+
+               if (!DBA::insert('application', $fields)) {
+                       DI::mstdnError()->RecordNotFound();
+               }
+
+               System::jsonExit(DI::mstdnApplication()->createFromApplicationId(DBA::lastInsertId()));
+       }
+}
index c8bb03a30dc834a7bcf0472e79eea4f084d4e3fc..1f31ca8ec7cd46f25b65782601fab4cd9a569593 100644 (file)
@@ -30,8 +30,18 @@ use Friendica\BaseDataTransferObject;
  */
 class Application extends BaseDataTransferObject
 {
+       /** @var string */
+       protected $client_id;
+       /** @var string */
+       protected $client_secret;
+       /** @var int */
+       protected $id;
        /** @var string */
        protected $name;
+       /** @var string */
+       protected $redirect_uri;
+       /** @var string */
+       protected $website;
 
        /**
         * Creates an application entry
@@ -39,8 +49,36 @@ class Application extends BaseDataTransferObject
         * @param array   $item
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public function __construct(string $name)
+       public function __construct(string $name, string $client_id = null, string $client_secret = null, int $id = null, string $redirect_uri = null, string $website = null)
        {
-               $this->name = $name;
+               $this->client_id     = $client_id;
+               $this->client_secret = $client_secret;
+               $this->id            = $id;
+               $this->name          = $name;
+               $this->redirect_uri  = $redirect_uri;
+               $this->website       = $website;
+       }
+
+       /**
+        * Returns the current entity as an array
+        *
+        * @return array
+        */
+       public function toArray(): array
+       {
+               $application = parent::toArray();
+
+               if (empty($application['id'])) {
+                       unset($application['client_id']);
+                       unset($application['client_secret']);
+                       unset($application['id']);
+                       unset($application['redirect_uri']);
+               }
+
+               if (empty($application['website'])) {
+                       unset($application['website']);
+               }
+
+               return $application;
        }
 }
index 247552761f49b30c3133d9da3d67b705651657fd..052f73b9cf94e38d5d50026947f6dc4a771a3c33 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1414);
+       define('DB_UPDATE_VERSION', 1415);
 }
 
 return [
@@ -426,6 +426,22 @@ return [
                        "gsid" => ["gsid"]
                ]
        ],
+       "application" => [
+               "comment" => "OAuth application",
+               "fields" => [
+                       "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "generated index"],
+                       "client_id" => ["type" => "varchar(64)", "not null" => "1", "comment" => ""],
+                       "client_secret" => ["type" => "varchar(64)", "not null" => "1", "comment" => ""],
+                       "name" => ["type" => "varchar(255)", "not null" => "1", "comment" => ""],
+                       "redirect_uri" => ["type" => "varchar(255)", "not null" => "1", "comment" => ""],
+                       "website" => ["type" => "varchar(255)", "comment" => ""],
+                       "scopes" => ["type" => "varchar(255)", "comment" => ""],
+               ],
+               "indexes" => [
+                       "PRIMARY" => ["id"],
+                       "client_id" => ["UNIQUE", "client_id"]
+               ]
+       ],
        "attach" => [
                "comment" => "file attachments",
                "fields" => [
index a21ff336da81f61d87c181ecfcebcc1183ec9275..5fbcffbd7cdbd5770adef37da16fde7a4901c2e4 100644 (file)
@@ -86,7 +86,7 @@ return [
                        '/announcements'                     => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]], // not implemented
                        '/announcements/{id:\d+}/dismiss'    => [Module\Api\Mastodon\Unimplemented::class,            [        R::POST]], // not implemented
                        '/announcements/{id:\d+}/reactions/{name}' => [Module\Api\Mastodon\Unimplemented::class,      [R::PUT, R::DELETE]], // not implemented
-                       '/apps'                              => [Module\Api\Mastodon\Unimplemented::class,            [        R::POST]],
+                       '/apps'                              => [Module\Api\Mastodon\Apps::class,                     [        R::POST]],
                        '/apps/verify_credentials'           => [Module\Api\Mastodon\Unimplemented::class,            [R::GET         ]],
                        '/blocks'                            => [Module\Api\Mastodon\Blocks::class,                   [R::GET         ]],
                        '/bookmarks'                         => [Module\Api\Mastodon\Bookmarks::class,                [R::GET         ]],