|
| 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 | +} |
0 commit comments