From 46e6ed474d202fa3b7fb5bb8045b7be5978663e4 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Sun, 7 Jun 2020 23:37:36 +0800 Subject: [PATCH] add oauth client as plugin --- .editorconfig | 3 ++ oauth-github/bootstrap.php | 14 +++-- oauth-github/lang/en/general.yml | 2 + oauth-github/lang/zh_CN/general.yml | 2 + oauth-github/package.json | 9 ++-- oauth-littleskin/bootstrap.php | 14 +++-- oauth-littleskin/lang/en/general.yml | 2 + oauth-littleskin/lang/zh_CN/general.yml | 2 + oauth-littleskin/package.json | 9 ++-- oauth-microsoft-live/bootstrap.php | 14 +++-- oauth-microsoft-live/lang/en/general.yml | 2 + oauth-microsoft-live/lang/zh_CN/general.yml | 2 + oauth-microsoft-live/package.json | 9 ++-- oauth/bootstrap.php | 35 +++++++++++++ oauth/composer.json | 5 ++ oauth/lang/en/general.yml | 2 + oauth/lang/en/providers.yml | 1 + oauth/lang/zh_CN/general.yml | 2 + oauth/lang/zh_CN/providers.yml | 1 + oauth/package.json | 18 +++++++ oauth/src/AuthController.php | 58 +++++++++++++++++++++ oauth/views/providers.twig | 11 ++++ 22 files changed, 196 insertions(+), 21 deletions(-) create mode 100644 oauth-github/lang/en/general.yml create mode 100644 oauth-github/lang/zh_CN/general.yml create mode 100644 oauth-littleskin/lang/en/general.yml create mode 100644 oauth-littleskin/lang/zh_CN/general.yml create mode 100644 oauth-microsoft-live/lang/en/general.yml create mode 100644 oauth-microsoft-live/lang/zh_CN/general.yml create mode 100644 oauth/bootstrap.php create mode 100644 oauth/composer.json create mode 100644 oauth/lang/en/general.yml create mode 100644 oauth/lang/en/providers.yml create mode 100644 oauth/lang/zh_CN/general.yml create mode 100644 oauth/lang/zh_CN/providers.yml create mode 100644 oauth/package.json create mode 100644 oauth/src/AuthController.php create mode 100644 oauth/views/providers.twig diff --git a/.editorconfig b/.editorconfig index c2e2162..beee4c6 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,3 +10,6 @@ insert_final_newline = true [*.{php,ps1}] indent_size = 4 + +[composer.json] +indent_size = 4 diff --git a/oauth-github/bootstrap.php b/oauth-github/bootstrap.php index e571288..c6a56e1 100644 --- a/oauth-github/bootstrap.php +++ b/oauth-github/bootstrap.php @@ -1,8 +1,10 @@ listen( 'SocialiteProviders\Manager\SocialiteWasCalled', 'SocialiteProviders\GitHub\GitHubExtendSocialite@handle' @@ -14,6 +16,12 @@ return function (Dispatcher $events) { 'redirect' => env('GITHUB_REDIRECT_URI'), ]]); - resolve('oauth.providers') - ->put('github', ['icon' => 'github', 'displayName' => 'GitHub']); + $filter->add('oauth_providers', function (Collection $providers) { + $providers->put('github', [ + 'icon' => 'github', + 'displayName' => 'GitHub', + ]); + + return $providers; + }); }; diff --git a/oauth-github/lang/en/general.yml b/oauth-github/lang/en/general.yml new file mode 100644 index 0000000..40c556a --- /dev/null +++ b/oauth-github/lang/en/general.yml @@ -0,0 +1,2 @@ +title: Log In with GitHub +description: Log in skin server with GitHub. diff --git a/oauth-github/lang/zh_CN/general.yml b/oauth-github/lang/zh_CN/general.yml new file mode 100644 index 0000000..513cdc5 --- /dev/null +++ b/oauth-github/lang/zh_CN/general.yml @@ -0,0 +1,2 @@ +title: 使用 GitHub 登录 +description: 用 GitHub 账号来登录皮肤站 diff --git a/oauth-github/package.json b/oauth-github/package.json index ab0551e..30d32bc 100644 --- a/oauth-github/package.json +++ b/oauth-github/package.json @@ -1,13 +1,14 @@ { "name": "oauth-github", - "title": "使用 GitHub 登录", - "version": "0.1.4", - "description": "用 GitHub 账号来登录皮肤站", + "title": "Blessing\\OAuth\\GitHub::general.title", + "version": "0.2.0", + "description": "Blessing\\OAuth\\GitHub::general.description", "author": "GPlane", "url": "https://gplane.win/", "namespace": "Blessing\\OAuth\\GitHub", "require": { - "blessing-skin-server": "^5.0.0" + "blessing-skin-server": "^5.0.0", + "oauth": "^1.0.0" }, "enchants": { "icon": { diff --git a/oauth-littleskin/bootstrap.php b/oauth-littleskin/bootstrap.php index 09e9ff0..b2d707d 100644 --- a/oauth-littleskin/bootstrap.php +++ b/oauth-littleskin/bootstrap.php @@ -1,8 +1,10 @@ listen( 'SocialiteProviders\Manager\SocialiteWasCalled', 'BlessingSocialiteProviders\LittleSkin\LittleSkinExtendSocialite@handle' @@ -14,6 +16,12 @@ return function (Dispatcher $events) { 'redirect' => env('LITTLESKIN_REDIRECT_URI'), ]]); - resolve('oauth.providers') - ->put('littleskin', ['icon' => 'littleskin', 'displayName' => 'LittleSkin']); + $filter->add('oauth_providers', function (Collection $providers) { + $providers->put('littleskin', [ + 'icon' => 'littleskin', + 'displayName' => 'LittleSkin', + ]); + + return $providers; + }); }; diff --git a/oauth-littleskin/lang/en/general.yml b/oauth-littleskin/lang/en/general.yml new file mode 100644 index 0000000..d2f4a37 --- /dev/null +++ b/oauth-littleskin/lang/en/general.yml @@ -0,0 +1,2 @@ +title: Log In with LittleSkin +description: Log in skin server with LittleSkin account. diff --git a/oauth-littleskin/lang/zh_CN/general.yml b/oauth-littleskin/lang/zh_CN/general.yml new file mode 100644 index 0000000..0cd0659 --- /dev/null +++ b/oauth-littleskin/lang/zh_CN/general.yml @@ -0,0 +1,2 @@ +title: 使用 LittleSkin 登录 +description: 用 LittleSkin 账号来登录皮肤站 diff --git a/oauth-littleskin/package.json b/oauth-littleskin/package.json index 88f1e64..70d10b7 100644 --- a/oauth-littleskin/package.json +++ b/oauth-littleskin/package.json @@ -1,13 +1,14 @@ { "name": "oauth-littleskin", - "title": "使用 LittleSkin 登录", - "version": "0.1.5", - "description": "用 LittleSkin 账号来登录皮肤站", + "title": "Blessing\\OAuth\\LittleSkin::general.title", + "version": "0.2.0", + "description": "Blessing\\OAuth\\LittleSkin::general.description", "author": "Asnxthaony", "url": "https://github.com/Asnxthaony", "namespace": "Blessing\\OAuth\\LittleSkin", "require": { - "blessing-skin-server": "^5.0.0" + "blessing-skin-server": "^5.0.0", + "oauth": "^1.0.0" }, "enchants": { "icon": { diff --git a/oauth-microsoft-live/bootstrap.php b/oauth-microsoft-live/bootstrap.php index 22091a0..2abab40 100644 --- a/oauth-microsoft-live/bootstrap.php +++ b/oauth-microsoft-live/bootstrap.php @@ -1,8 +1,10 @@ listen( 'SocialiteProviders\Manager\SocialiteWasCalled', 'SocialiteProviders\Live\LiveExtendSocialite@handle' @@ -14,6 +16,12 @@ return function (Dispatcher $events) { 'redirect' => env('LIVE_REDIRECT_URI'), ]]); - resolve('oauth.providers') - ->put('live', ['icon' => 'microsoft', 'displayName' => 'Microsoft Live']); + $filter->add('oauth_providers', function (Collection $providers) { + $providers->put('littleskin', [ + 'icon' => 'microsoft', + 'displayName' => 'Microsoft Live', + ]); + + return $providers; + }); }; diff --git a/oauth-microsoft-live/lang/en/general.yml b/oauth-microsoft-live/lang/en/general.yml new file mode 100644 index 0000000..3006d5f --- /dev/null +++ b/oauth-microsoft-live/lang/en/general.yml @@ -0,0 +1,2 @@ +title: Log In with Microsoft Live +description: Log in skin server with Microsoft Live. diff --git a/oauth-microsoft-live/lang/zh_CN/general.yml b/oauth-microsoft-live/lang/zh_CN/general.yml new file mode 100644 index 0000000..9f122d2 --- /dev/null +++ b/oauth-microsoft-live/lang/zh_CN/general.yml @@ -0,0 +1,2 @@ +title: 使用 Microsoft Live 登录 +description: 通过 Microsoft Live 来登录皮肤站 diff --git a/oauth-microsoft-live/package.json b/oauth-microsoft-live/package.json index ec9a20a..1a63a1d 100644 --- a/oauth-microsoft-live/package.json +++ b/oauth-microsoft-live/package.json @@ -1,13 +1,14 @@ { "name": "oauth-microsoft-live", - "title": "使用 Microsoft Live 登录", - "version": "0.1.4", - "description": "通过 Microsoft Live 来登录皮肤站", + "title": "Blessing\\OAuth\\Live::general.title", + "version": "0.2.0", + "description": "Blessing\\OAuth\\Live::general.description", "author": "GPlane", "url": "", "namespace": "Blessing\\OAuth\\Live", "require": { - "blessing-skin-server": "^5.0.0" + "blessing-skin-server": "^5.0.0", + "oauth": "^1.0.0" }, "enchants": { "icon": { diff --git a/oauth/bootstrap.php b/oauth/bootstrap.php new file mode 100644 index 0000000..a7a4925 --- /dev/null +++ b/oauth/bootstrap.php @@ -0,0 +1,35 @@ +apply('oauth_providers', collect()); + + $view->with('providers', $providers); + }); + + $filter->add('auth_page_rows:login', function ($rows) { + $length = count($rows); + array_splice($rows, $length - 1, 0, ['Blessing\OAuthCore::providers']); + + return $rows; + }); + + $filter->add('auth_page_rows:register', function ($rows) { + $rows[] = 'Blessing\OAuthCore::providers'; + + return $rows; + }); + + Hook::addRoute(function () { + Route::prefix('auth/login') + ->namespace('Blessing\OAuthCore') + ->group(function () { + Route::get('{driver}', 'AuthController@login'); + Route::get('{driver}/callback', 'AuthController@callback'); + }); + }); +}; diff --git a/oauth/composer.json b/oauth/composer.json new file mode 100644 index 0000000..85807cf --- /dev/null +++ b/oauth/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "socialiteproviders/manager": "^3.5" + } +} diff --git a/oauth/lang/en/general.yml b/oauth/lang/en/general.yml new file mode 100644 index 0000000..5d29747 --- /dev/null +++ b/oauth/lang/en/general.yml @@ -0,0 +1,2 @@ +title: OAuth Client Core +description: Core plugin of OAuth client. diff --git a/oauth/lang/en/providers.yml b/oauth/lang/en/providers.yml new file mode 100644 index 0000000..4c2dedb --- /dev/null +++ b/oauth/lang/en/providers.yml @@ -0,0 +1 @@ +title: Log In with diff --git a/oauth/lang/zh_CN/general.yml b/oauth/lang/zh_CN/general.yml new file mode 100644 index 0000000..be607ab --- /dev/null +++ b/oauth/lang/zh_CN/general.yml @@ -0,0 +1,2 @@ +title: OAuth 客户端核心 +description: OAuth 客户端核心插件 diff --git a/oauth/lang/zh_CN/providers.yml b/oauth/lang/zh_CN/providers.yml new file mode 100644 index 0000000..5a4240a --- /dev/null +++ b/oauth/lang/zh_CN/providers.yml @@ -0,0 +1 @@ +title: 其它登录方式 diff --git a/oauth/package.json b/oauth/package.json new file mode 100644 index 0000000..27676a5 --- /dev/null +++ b/oauth/package.json @@ -0,0 +1,18 @@ +{ + "name": "oauth", + "title": "Blessing\\OAuthCore::general.title", + "version": "1.0.0", + "description": "Blessing\\OAuthCore::general.description", + "author": "GPlane", + "url": "https://gplane.win/", + "namespace": "Blessing\\OAuthCore", + "require": { + "blessing-skin-server": "^5.0.0" + }, + "enchants": { + "icon": { + "bg": "primary", + "fa": "box" + } + } +} diff --git a/oauth/src/AuthController.php b/oauth/src/AuthController.php new file mode 100644 index 0000000..b943047 --- /dev/null +++ b/oauth/src/AuthController.php @@ -0,0 +1,58 @@ +redirect(); + } + + public function callback(Dispatcher $dispatcher, Filter $filter, $driver) + { + $remoteUser = Socialite::driver($driver)->user(); + + $email = $remoteUser->email; + if (empty($email)) { + abort(500, 'Unsupported OAuth Server which does not provide email.'); + } + + $user = User::where('email', $email)->first(); + if (!$user) { + $whip = new Whip(); + $ip = $whip->getValidIpAddress(); + $ip = $filter->apply('client_ip', $ip); + + $user = new User(); + $user->email = $email; + $user->nickname = $remoteUser->nickname ?? $remoteUser->name ?? $email; + $user->score = option('user_initial_score'); + $user->avatar = 0; + $user->password = ''; + $user->ip = $ip; + $user->permission = User::NORMAL; + $user->register_at = Carbon::now(); + $user->last_sign_at = Carbon::now()->subDay(); + $user->verified = true; + + $user->save(); + $dispatcher->dispatch('auth.registration.completed', [$user]); + } + + $dispatcher->dispatch('auth.login.ready', [$user]); + Auth::login($user); + $dispatcher->dispatch('auth.login.succeeded', [$user]); + + return redirect('/user'); + } +} diff --git a/oauth/views/providers.twig b/oauth/views/providers.twig new file mode 100644 index 0000000..dc26542 --- /dev/null +++ b/oauth/views/providers.twig @@ -0,0 +1,11 @@ +{% if providers|length > 0 %} +
+
{{ trans('Blessing\\OAuthCore::providers.title') }}
+ {% for name, provider in providers %} + +   + {{ provider.displayName }} + + {% endfor %} +
+{% endif %}