From 5b4f077432565c96fc3a83478029f6fec10b7708 Mon Sep 17 00:00:00 2001 From: Daniel <386404@qq.com> Date: Sun, 19 Jan 2025 16:15:17 +0800 Subject: [PATCH] new provider for linux.do forum --- src/LinuxDo/LinuxDoExtendSocialite.php | 19 +++++++ src/LinuxDo/Provider.php | 65 ++++++++++++++++++++++++ src/LinuxDo/README.md | 70 ++++++++++++++++++++++++++ src/LinuxDo/composer.json | 33 ++++++++++++ 4 files changed, 187 insertions(+) create mode 100644 src/LinuxDo/LinuxDoExtendSocialite.php create mode 100644 src/LinuxDo/Provider.php create mode 100644 src/LinuxDo/README.md create mode 100644 src/LinuxDo/composer.json diff --git a/src/LinuxDo/LinuxDoExtendSocialite.php b/src/LinuxDo/LinuxDoExtendSocialite.php new file mode 100644 index 000000000..9e35728fa --- /dev/null +++ b/src/LinuxDo/LinuxDoExtendSocialite.php @@ -0,0 +1,19 @@ +extendSocialite('linux_do', Provider::class); + } +} diff --git a/src/LinuxDo/Provider.php b/src/LinuxDo/Provider.php new file mode 100644 index 000000000..fb58a5414 --- /dev/null +++ b/src/LinuxDo/Provider.php @@ -0,0 +1,65 @@ +buildAuthUrlFromBase('https://connect.linux.do/oauth2/authorize', $state); + } + + /** + * {@inheritdoc} + */ + protected function getTokenUrl() + { + return 'https://connect.linux.do/oauth2/token'; + } + + /** + * {@inheritdoc} + */ + protected function getUserByToken($token) + { + $response = $this->getHttpClient()->get('https://connect.linux.do/api/user', [ + RequestOptions::HEADERS => [ + 'Authorization' => 'Bearer '.$token, + ], + ]); + +// Log::warning("LinuxDO Hint getUserResponse:", ["response"=>$response->getBody()]); + + return json_decode((string) $response->getBody(), true); + } + + /** + * {@inheritdoc} + */ + protected function mapUserToObject(array $user) + { +// Log::warning("LinuxDO Hint UserMap:", $user); + return (new User)->setRaw($user)->map([ + 'id' => $user['id'], + 'nickname' => $user['username'], + 'name' => $user['name'], + 'email' => $user['email'], + 'avatar' => $user['avatar_url'], + ]); + } +} diff --git a/src/LinuxDo/README.md b/src/LinuxDo/README.md new file mode 100644 index 000000000..d1256f2b1 --- /dev/null +++ b/src/LinuxDo/README.md @@ -0,0 +1,70 @@ +# LinuxDo + +```bash +composer require socialiteproviders/linuxdo +``` + +## Installation & Basic Usage + +Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below. + +### Add configuration to `config/services.php` + +```php +'linux_do' => [ + 'client_id' => env('LINUXDO_CLIENT_ID'), + 'client_secret' => env('LINUXDO_CLIENT_SECRET'), + 'redirect' => env('LINUXDO_REDIRECT_URI'), +], +``` + +### Add provider event listener + +#### Laravel 11+ + +In Laravel 11, the default `EventServiceProvider` provider was removed. Instead, add the listener using the `listen` method on the `Event` facade, in your `AppServiceProvider` `boot` method. + +* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers. + +```php +Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) { + $event->extendSocialite('linux_do', \SocialiteProviders\LinuxDo\Provider::class); +}); +``` +
+ +Laravel 10 or below + +Configure the package's listener to listen for `SocialiteWasCalled` events. + +Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions. + +```php +protected $listen = [ + \SocialiteProviders\Manager\SocialiteWasCalled::class => [ + // ... other providers + \SocialiteProviders\LinuxDo\LinuxDoExtendSocialite::class.'@handle', + ], +]; +``` +
+ +### Usage + +You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed): + +```php +return Socialite::driver('linux_do')->redirect(); +``` + +### Returned User fields + +- ``id`` +- ``nickname`` +- ``name`` +- ``email`` +- ``avatar`` + +### Reference + +- [Create Linux.do OAuth2 Application](https://connect.linux.do/) diff --git a/src/LinuxDo/composer.json b/src/LinuxDo/composer.json new file mode 100644 index 000000000..4ea1d0ba5 --- /dev/null +++ b/src/LinuxDo/composer.json @@ -0,0 +1,33 @@ +{ + "name": "socialiteproviders/linuxdo", + "description": "LinuxDo OAuth2 Provider for Laravel Socialite", + "license": "MIT", + "keywords": [ + "linuxdo", + "linux.do", + "oauth", + "socialite", + "provider" + ], + "authors": [ + { + "name": "Travis Tang", + "email": "me@investravis.com" + } + ], + "support": { + "issues": "https://github.com/Travisun/oauthProviders/issues", + "source": "https://github.com/Travisun/oauthProviders", + "docs": "https://github.com/Travisun/oauthProviders" + }, + "require": { + "php": "^8.0", + "ext-json": "*", + "socialiteproviders/manager": "^4.4" + }, + "autoload": { + "psr-4": { + "SocialiteProviders\\LinuxDo\\": "" + } + } +}