Skip to content

Commit db3d56a

Browse files
authored
Add PocketID provider (#1379)
1 parent c2819fa commit db3d56a

File tree

5 files changed

+199
-0
lines changed

5 files changed

+199
-0
lines changed

monorepo-builder.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ parameters:
159159
src/Pipedrive: 'git@github.com:SocialiteProviders/Pipedrive.git'
160160
src/Pixnet: 'git@github.com:SocialiteProviders/Pixnet.git'
161161
src/PlanningCenter: 'git@github.com/SocialiteProviders/PlanningCenter.git'
162+
src/PocketID: 'git@github.com:SocialiteProviders/PocketID.git'
162163
src/Podio: 'git@github.com:SocialiteProviders/Podio.git'
163164
src/Pr0gramm: 'git@github.com:SocialiteProviders/Pr0gramm.git'
164165
src/Procore: 'git@github.com:SocialiteProviders/Procore.git'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace SocialiteProviders\PocketID;
4+
5+
use SocialiteProviders\Manager\SocialiteWasCalled;
6+
7+
class PocketIDExtendSocialite
8+
{
9+
public function handle(SocialiteWasCalled $socialiteWasCalled): void
10+
{
11+
$socialiteWasCalled->extendSocialite('pocketid', Provider::class);
12+
}
13+
}

src/PocketID/Provider.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace SocialiteProviders\PocketID;
4+
5+
use GuzzleHttp\RequestOptions;
6+
use InvalidArgumentException;
7+
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
8+
use SocialiteProviders\Manager\OAuth2\User;
9+
10+
class Provider extends AbstractProvider
11+
{
12+
public const IDENTIFIER = 'POCKETID';
13+
14+
protected $scopes = ['openid', 'profile', 'email'];
15+
16+
protected $scopeSeparator = ' ';
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public static function additionalConfigKeys()
22+
{
23+
return ['base_url'];
24+
}
25+
26+
protected function getBaseUrl()
27+
{
28+
$baseurl = $this->getConfig('base_url');
29+
30+
if ($baseurl === null) {
31+
throw new InvalidArgumentException('Missing base_url');
32+
}
33+
34+
return rtrim($baseurl, '/');
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function getAuthUrl($state)
41+
{
42+
return $this->buildAuthUrlFromBase($this->getBaseUrl().'/authorize', $state);
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
protected function getTokenUrl()
49+
{
50+
return $this->getBaseUrl().'/api/oidc/token';
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
protected function getUserByToken($token)
57+
{
58+
$response = $this->getHttpClient()->get($this->getBaseUrl().'/api/oidc/userinfo', [
59+
RequestOptions::HEADERS => [
60+
'Authorization' => 'Bearer '.$token,
61+
],
62+
]);
63+
64+
return json_decode((string) $response->getBody(), true);
65+
}
66+
67+
/**
68+
* {@inheritdoc}
69+
*/
70+
protected function mapUserToObject(array $user)
71+
{
72+
return (new User())->setRaw($user)->map([
73+
'id' => $user['sub'],
74+
'name' => $user['name'] ?? null,
75+
'given_name' => $user['given_name'] ?? null,
76+
'family_name' => $user['family_name'] ?? null,
77+
'preferred_username' => $user['preferred_username'] ?? null,
78+
'email' => $user['email'] ?? null,
79+
'email_verified' => $user['email_verified'] ?? null,
80+
'picture' => $user['picture'] ?? null,
81+
]);
82+
}
83+
}

src/PocketID/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# PocketID
2+
3+
```bash
4+
composer require socialiteproviders/pocketid
5+
```
6+
7+
## Installation & Basic Usage
8+
9+
Please see the [Base Installation Guide](https://socialiteproviders.com/usage/), then follow the provider specific instructions below.
10+
11+
### Add configuration to `config/services.php`
12+
13+
```php
14+
'pocketid' => [
15+
'client_id' => env('POCKETID_CLIENT_ID'),
16+
'client_secret' => env('POCKETID_CLIENT_SECRET'),
17+
'redirect' => env('POCKETID_REDIRECT_URI'),
18+
],
19+
```
20+
21+
### Add provider event listener
22+
23+
#### Laravel 11+
24+
25+
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.
26+
27+
* Note: You do not need to add anything for the built-in socialite providers unless you override them with your own providers.
28+
29+
```php
30+
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
31+
$event->extendSocialite('pocketid', \SocialiteProviders\PocketID\Provider::class);
32+
});
33+
```
34+
<details>
35+
<summary>
36+
Laravel 10 or below
37+
</summary>
38+
Configure the package's listener to listen for `SocialiteWasCalled` events.
39+
40+
Add the event to your `listen[]` array in `app/Providers/EventServiceProvider`. See the [Base Installation Guide](https://socialiteproviders.com/usage/) for detailed instructions.
41+
42+
```php
43+
protected $listen = [
44+
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
45+
// ... other providers
46+
\SocialiteProviders\PocketID\PocketIDExtendSocialite::class.'@handle',
47+
],
48+
];
49+
```
50+
</details>
51+
52+
### Usage
53+
54+
You should now be able to use the provider like you would regularly use Socialite (assuming you have the facade installed):
55+
56+
```php
57+
return Socialite::driver('pocketid')->redirect();
58+
```
59+
60+
### Returned User fields
61+
62+
- ``id``
63+
- ``name``
64+
- ``given_name``
65+
- ``family_name``
66+
- ``preferred_username``
67+
- ``email``
68+
- ``email_verified``
69+
- ``picture``

src/PocketID/composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "socialiteproviders/pocketid",
3+
"description": "PocketID OAuth2 Provider for Laravel Socialite",
4+
"license": "MIT",
5+
"keywords": [
6+
"pocketid",
7+
"laravel",
8+
"oauth",
9+
"provider",
10+
"socialite"
11+
],
12+
"authors": [
13+
{
14+
"name": "brufdev",
15+
"email": "brufdev@proton.me"
16+
}
17+
],
18+
"support": {
19+
"issues": "https://github.com/socialiteproviders/providers/issues",
20+
"source": "https://github.com/socialiteproviders/providers",
21+
"docs": "https://socialiteproviders.com/pocketid"
22+
},
23+
"require": {
24+
"php": "^8.0",
25+
"ext-json": "*",
26+
"socialiteproviders/manager": "^4.4"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"SocialiteProviders\\PocketID\\": ""
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)