Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/Tailscale/Provider.php
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'),
]);
}
}
70 changes: 70 additions & 0 deletions src/Tailscale/README.md
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`)
14 changes: 14 additions & 0 deletions src/Tailscale/TailscaleExtendSocialite.php
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);
}
}
34 changes: 34 additions & 0 deletions src/Tailscale/composer.json
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",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8.4 +

Copy link
Copy Markdown
Contributor Author

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?

Copy link
Copy Markdown
Member

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

"ext-json": "*",
"socialiteproviders/manager": "^4.4"
},
"autoload": {
"psr-4": {
"SocialiteProviders\\Tailscale\\": ""
}
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline

Copy link
Copy Markdown
Contributor Author

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, which don't have newline. What's the policy?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have a newline

Loading