Bladeren bron

OAuth (part 12 draft) Add Google

Not tested. Google banned the registration of new applications for my country.
Visman 2 jaren geleden
bovenliggende
commit
06be297c6f

+ 1 - 1
app/Models/Pages/Admin/Install.php

@@ -1274,7 +1274,7 @@ class Install extends Admin
 
         // заполнение providers
         $providers = [
-            'github', 'yandex',
+            'github', 'yandex', 'google',
         ];
 
         $query = 'INSERT INTO ::providers (pr_name, pr_pos)

+ 1 - 1
app/Models/Pages/Admin/Providers.php

@@ -97,7 +97,7 @@ class Providers extends Admin
             $fields["name-{$provider->name}"] = [
                 'class'   => ['name', 'provider'],
                 'type'    => 'btn',
-                'value'   => $provider->name,
+                'value'   => __($provider->name),
                 'caption' => 'Provider label',
                 'link'    => $this->c->Router->link('AdminProvider', ['name' => $provider->name]),
             ];

+ 5 - 0
app/Models/Pages/Admin/Update.php

@@ -535,6 +535,7 @@ class Update extends Admin
     {
         $providers = [
             1 => 'yandex',
+            2 => 'google',
         ];
 
         $query = 'INSERT INTO ::providers (pr_name, pr_pos)
@@ -561,6 +562,10 @@ class Update extends Admin
             'shared=>providers=>drivers=>yandex',
             '\\ForkBB\\Models\\Provider\\Driver\\Yandex::class'
         );
+        $coreConfig->add(
+            'shared=>providers=>drivers=>google',
+            '\\ForkBB\\Models\\Provider\\Driver\\Google::class'
+        );
 
         $coreConfig->save();
 

+ 139 - 0
app/Models/Provider/Driver/Google.php

@@ -0,0 +1,139 @@
+<?php
+/**
+ * This file is part of the ForkBB <https://github.com/forkbb>.
+ *
+ * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
+ * @license   The MIT License (MIT)
+ */
+
+declare(strict_types=1);
+
+namespace ForkBB\Models\Provider\Driver;
+
+use ForkBB\Models\Provider\Driver;
+use RuntimeException;
+
+class Google extends Driver    // Not tested. Google banned the registration of new applications for my country.
+{
+    protected string $origName = 'google';
+    protected string $authURL  = 'https://accounts.google.com/o/oauth2/v2/auth';
+    protected string $tokenURL = 'https://oauth2.googleapis.com/token';
+    protected string $userURL  = 'https://www.googleapis.com/oauth2/v2/userinfo';
+    protected string $scope    = 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile';
+
+    /**
+     * Запрашивает информацию о пользователе
+     * Проверяет ответ
+     * Запоминает данные пользователя
+     */
+    public function reqUserInfo(): bool
+    {
+        $this->userInfo = [];
+
+        $options = [
+            'headers' => [
+                'Accept: application/json',
+                "Authorization: Bearer {$this->access_token}",
+            ],
+        ];
+
+        $response = $this->request('GET', $this->userURL, $options);
+
+        if (
+            ! empty($response['sub'])
+            || ! empty($response['id'])
+        ) {
+            $this->userInfo = $response;
+
+            return true;
+        } elseif (\is_array($response)) {
+            $this->error = 'User error';
+        }
+
+        return false;
+    }
+
+    /**
+     * Возвращает идентификатор пользователя (от провайдера)
+     */
+    protected function getuserId(): string
+    {
+        return (string) ($this->userInfo['sub'] ?? ($this->userInfo['id'] ?? '')); // ????
+    }
+
+    /**
+     * Возвращает логин пользователя (от провайдера)
+     */
+    protected function getuserLogin(): string
+    {
+        return (string) ($this->userInfo['given_name'] ?? '');
+    }
+
+    /**
+     * Возвращает имя пользователя (от провайдера)
+     */
+    protected function getuserName(): string
+    {
+        return (string) ($this->userInfo['name'] ?? '');
+    }
+
+    /**
+     * Возвращает email пользователя (от провайдера)
+     */
+    protected function getuserEmail(): string
+    {
+        return $this->c->Mail->valid($this->userInfo['email'] ?? null) ?: "{$this->origName}-{$this->userId}@localhost";
+    }
+
+    /**
+     * Возвращает флаг подлинности email пользователя (от провайдера)
+     */
+    protected function getuserEmailVerifed(): bool
+    {
+        return ! empty($this->userInfo['verified_email'])
+            && ! empty($this->userInfo['email'])
+            && $this->userEmail === $this->userInfo['email'];
+    }
+
+    /**
+     * Возвращает ссылку на аватарку пользователя (от провайдера)
+     */
+    protected function getuserAvatar(): string
+    {
+        return (string) ($this->userInfo['picture'] ?? '');
+    }
+
+    /**
+     * Возвращает ссылку на профиль пользователя (от провайдера)
+     */
+    protected function getuserURL(): string
+    {
+        return '';
+    }
+
+    /**
+     * Возвращает местоположение пользователя (от провайдера)
+     */
+    protected function getuserLocation(): string
+    {
+        return '';
+    }
+
+    /**
+     * Возвращает пол пользователя (от провайдера)
+     */
+    protected function getuserGender(): int
+    {
+        if (isset($this->userInfo['gender'])) {
+            if ('male' === $this->userInfo['gender']) {
+                return FORK_GEN_MAN;
+            }
+
+            if ('female' === $this->userInfo['gender']) {
+                return FORK_GEN_FEM;
+            }
+        }
+
+        return FORK_GEN_NOT;
+    }
+}

+ 1 - 0
app/config/main.dist.php

@@ -168,6 +168,7 @@ return [
             'drivers' => [
                 'github' => \ForkBB\Models\Provider\Driver\GitHub::class,
                 'yandex' => \ForkBB\Models\Provider\Driver\Yandex::class,
+                'google' => \ForkBB\Models\Provider\Driver\Google::class,
             ],
         ],
         'providerUser'  => \ForkBB\Models\ProviderUser\ProviderUser::class,

+ 3 - 0
app/lang/en/admin_providers.po

@@ -39,6 +39,9 @@ msgstr "GitHub"
 msgid "yandex"
 msgstr "Yandex"
 
+msgid "google"
+msgstr "Google"
+
 msgid "Callback label"
 msgstr "Callback URI"
 

+ 3 - 0
app/lang/ru/admin_providers.po

@@ -39,6 +39,9 @@ msgstr "GitHub"
 msgid "yandex"
 msgstr "Yandex"
 
+msgid "google"
+msgstr "Google"
+
 msgid "Callback label"
 msgstr "URI перенаправления"
 

+ 11 - 0
public/style/ForkBB/style.css

@@ -3017,3 +3017,14 @@ body,
   color: #ffffff;
   background-color: #fc3f1d;
 }
+
+#fork .f-btn[name="google"] {
+  color: #4285f4;
+  background-color: #ffffff;
+}
+
+#fork .f-btn[name="google"]:focus,
+#fork .f-btn[name="google"]:hover {
+  color: #ffffff;
+  background-color: #4285f4;
+}