-
Notifications
You must be signed in to change notification settings - Fork 519
feat: add provider Tailscale #1413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
df7d96d
4c06259
5f9f5f4
1180877
a01a518
29c3d1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
|
|
||
| namespace App\Helpers\SocialiteProviders; | ||
|
|
||
| use Illuminate\Support\Arr; | ||
| use GuzzleHttp\RequestOptions; | ||
| use SocialiteProviders\Manager\OAuth2\User; | ||
| use SocialiteProviders\Manager\OAuth2\AbstractProvider; | ||
|
|
||
| class Tailscale extends AbstractProvider | ||
| { | ||
| const IDENTIFIER = 'TAILSCALE'; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected $scopes = ['openid', 'profile', 'email']; | ||
| protected $scopeSeparator = ' '; | ||
|
|
||
| public static function additionalConfigKeys() | ||
| { | ||
| return ['base_url']; | ||
| } | ||
|
|
||
| protected function getBaseUrl() | ||
| { | ||
| $baseurl = $this->getConfig('base_url'); | ||
| if ($baseurl === null) { | ||
| throw new \InvalidArgumentException('Missing base_url'); | ||
| } | ||
|
|
||
| return rtrim($baseurl, '/'); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function getAuthUrl($state) | ||
| { | ||
| return $this->buildAuthUrlFromBase($this->getBaseUrl().'/authorize', $state); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function getTokenUrl() | ||
| { | ||
| return $this->getBaseUrl().'/token'; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function getUserByToken($token) | ||
| { | ||
| $response = $this->getHttpClient()->get($this->getBaseUrl().'/userinfo', [ | ||
| RequestOptions::HEADERS => [ | ||
| 'Authorization' => 'Bearer '.$token, | ||
| ], | ||
| ]); | ||
|
|
||
| return json_decode((string) $response->getBody(), true); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function mapUserToObject(array $user) | ||
| { | ||
| return (new User)->setRaw($user)->map([ | ||
| 'id' => Arr::get($user, 'sub'), | ||
| 'email' => Arr::get($user, 'email'), | ||
| 'name' => Arr::get($user, 'name'), | ||
| 'username' => Arr::get($user, 'username'), | ||
| ]); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Tailscale (tsidp) | ||
|
|
||
| ```bash | ||
| composer require socialiteproviders/tailscale | ||
| ``` | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Install [Tailscale OpenID Connect (OIDC) Identity Provider (tsidp)](https://github.com/tailscale/tsidp) and make it available on your Tailscale network. | ||
|
|
||
| ## 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 | ||
| 'tailscale' => [ | ||
| 'base_url' => env('TAILSCALE_BASE_URL'), | ||
| 'client_id' => env('TAILSCALE_CLIENT_ID'), | ||
| 'client_secret' => env('TAILSCALE_CLIENT_SECRET'), | ||
| 'redirect' => env('TAILSCALE_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('tailscale', \SocialiteProviders\Tailscale\Provider::class); | ||
| }); | ||
| ``` | ||
| <details> | ||
| <summary> | ||
| Laravel 10 or below | ||
| </summary> | ||
| 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\Tailscale\TailscaleExtendSocialite::class.'@handle', | ||
| ], | ||
| ]; | ||
| ``` | ||
| </details> | ||
|
|
||
| ### 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('tailscale')->redirect(); | ||
| ``` | ||
|
|
||
| ### Returned User fields | ||
|
|
||
| - ``id`` | ||
| - ``email`` | ||
| - ``name`` | ||
| - ``username`` (same as `name`) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
|
|
||
| <?php | ||
|
|
||
| namespace SocialiteProviders\Tailscale; | ||
|
|
||
| use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
|
||
| class TailscaleExtendSocialite | ||
| { | ||
| public function handle(SocialiteWasCalled $socialiteWasCalled): void | ||
| { | ||
| $socialiteWasCalled->extendSocialite('tailscale', Provider::class); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| { | ||
| "name": "socialiteproviders/tailscale", | ||
| "description": "Tailscale OAuth2 Provider for Laravel Socialite", | ||
| "license": "MIT", | ||
| "keywords": [ | ||
| "laravel", | ||
| "oauth", | ||
| "provider", | ||
| "socialite", | ||
| "tailscale", | ||
| "tsidp" | ||
| ], | ||
| "authors": [ | ||
| { | ||
| "name": "Winfred van Kuijk", | ||
| "email": "winfred@vankuijk.net" | ||
| } | ||
| ], | ||
| "support": { | ||
| "issues": "https://github.com/socialiteproviders/providers/issues", | ||
| "source": "https://github.com/socialiteproviders/providers", | ||
| "docs": "https://socialiteproviders.com/tailscale" | ||
| }, | ||
| "require": { | ||
| "php": "^8.0", | ||
| "ext-json": "*", | ||
| "socialiteproviders/manager": "^4.4" | ||
| }, | ||
| "autoload": { | ||
| "psr-4": { | ||
| "SocialiteProviders\\Tailscale\\": "" | ||
| } | ||
| } | ||
| } | ||
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8.4 +
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied composer.json from other providers, e.g. Okta. Most of them require 8.0+.
Is there anything 8.4 specific in the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, 8.3 is ok. we only support supported php vers