Skip to content

Commit a465d40

Browse files
committed
Add pocket id SSO support
1 parent 83c0d00 commit a465d40

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

app/Providers/AppServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function boot()
3939
$event->extendSocialite('authentik', \SocialiteProviders\Authentik\Provider::class);
4040
$event->extendSocialite('authelia', \SocialiteProviders\Authelia\Provider::class);
4141
$event->extendSocialite('keycloak', \SocialiteProviders\Keycloak\Provider::class);
42+
$event->extendSocialite('pocketid', \Kami\Cocktail\Services\Auth\PocketIdProvider::class);
4243
});
4344

4445
if (DB::getDriverName() === 'sqlite') {

app/Services/Auth/OauthProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum OauthProvider: string
1818
case Authentik = 'authentik';
1919
case Authelia = 'authelia';
2020
case Keycloak = 'keycloak';
21+
case PocketId = 'pocketid';
2122

2223
public function getPrettyName(): string
2324
{
@@ -28,6 +29,7 @@ public function getPrettyName(): string
2829
self::Authentik => 'Authentik',
2930
self::Authelia => 'Authelia',
3031
self::Keycloak => 'Keycloak',
32+
self::PocketId => 'PocketId',
3133
};
3234
}
3335
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kami\Cocktail\Services\Auth;
6+
7+
use Laravel\Socialite\Contracts\Provider;
8+
use SocialiteProviders\Manager\SocialiteWasCalled;
9+
10+
class PocketIdExtendSocialite
11+
{
12+
public function handle(SocialiteWasCalled $socialiteWasCalled): void
13+
{
14+
$socialiteWasCalled->extendSocialite('pocketid', Provider::class);
15+
}
16+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Kami\Cocktail\Services\Auth;
6+
7+
use GuzzleHttp\RequestOptions;
8+
use Illuminate\Support\Arr;
9+
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
10+
use SocialiteProviders\Manager\OAuth2\User;
11+
12+
class PocketIdProvider extends AbstractProvider
13+
{
14+
public const IDENTIFIER = 'POCKETID';
15+
16+
/**
17+
* @var array<string>
18+
*/
19+
protected $scopes = ['openid profile email'];
20+
21+
/**
22+
* @return array<string>
23+
*/
24+
public static function additionalConfigKeys(): array
25+
{
26+
return ['base_url'];
27+
}
28+
29+
protected function getBaseUrl(): string
30+
{
31+
return rtrim($this->getConfig('base_url'), '/');
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
protected function getAuthUrl($state)
38+
{
39+
return $this->buildAuthUrlFromBase($this->getBaseUrl() . '/authorize', $state);
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
protected function getTokenUrl()
46+
{
47+
return $this->getBaseUrl() . '/api/oidc/token';
48+
}
49+
50+
/**
51+
* @return array<mixed>
52+
*/
53+
protected function getUserByToken($token): array
54+
{
55+
$response = $this->getHttpClient()->get($this->getBaseUrl() . '/api/oidc/userinfo', [
56+
RequestOptions::HEADERS => [
57+
'Authorization' => 'Bearer '.$token,
58+
],
59+
]);
60+
61+
return json_decode((string) $response->getBody(), true);
62+
}
63+
64+
/**
65+
* @param array<mixed> $user
66+
*/
67+
protected function mapUserToObject(array $user): User
68+
{
69+
return (new User())->setRaw($user)->map([
70+
'id' => Arr::get($user, 'sub'),
71+
'nickname' => Arr::get($user, 'preferred_username'),
72+
'name' => Arr::get($user, 'name'),
73+
'email' => Arr::get($user, 'email'),
74+
]);
75+
}
76+
}

config/services.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,11 @@
7070
'base_url' => env('KEYCLOAK_BASE_URL'),
7171
'realms' => env('KEYCLOAK_REALM'),
7272
],
73+
74+
'pocketid' => [
75+
'base_url' => env('POCKETID_BASE_URL'),
76+
'client_id' => env('POCKETID_CLIENT_ID'),
77+
'client_secret' => env('POCKETID_CLIENT_SECRET'),
78+
'redirect' => env('POCKETID_REDIRECT_URI')
79+
],
7380
];

0 commit comments

Comments
 (0)