-
Notifications
You must be signed in to change notification settings - Fork 0
/
Provider.php
109 lines (92 loc) · 3.24 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace SocialiteProviders\Zitadel;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'ZITADEL';
protected $scopeSeparator = ' ';
protected $scopes = ['openid', 'profile', 'email'];
/** {@inheritDoc} */
public function getScopes()
{
$additionalScopes = [];
if ($this->getConfig('organization_id')) {
$additionalScopes[] = 'urn:zitadel:iam:org:id:'.$this->getConfig('organization_id');
}
if ($this->getConfig('project_id')) {
$additionalScopes[] = 'urn:zitadel:iam:org:project:id:'.$this->getConfig('project_id').':aud';
}
return array_merge($this->scopes, $additionalScopes);
}
public static function additionalConfigKeys(): array
{
return [
'base_url',
'organization_id',
'project_id',
'post_logout_redirect_uri',
];
}
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase($this->getConfig('base_url').'/oauth/v2/authorize', $state);
}
protected function getTokenUrl(): string
{
return $this->getConfig('base_url').'/oauth/v2/token';
}
/** {@inheritDoc} */
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get($this->getConfig('base_url').'/oidc/v1/userinfo', [
RequestOptions::HEADERS => [
'Authorization' => 'Bearer '.$token,
],
]);
return json_decode((string) $response->getBody(), true);
}
/** {@inheritDoc} */
protected function parseApprovedScopes($body)
{
$scopes = parent::parseApprovedScopes($body);
return array_unique(array_merge($scopes, $this->getScopes()));
}
/** {@inheritDoc} */
public function mapUserToObject(array $user): User
{
return (new User)->setRaw($user)->map([
'id' => Arr::get($user, 'sub'),
'email' => Arr::get($user, 'email'),
'name' => Arr::get($user, 'name'),
'nickname' => Arr::get($user, 'preferred_username'),
'avatar' => Arr::get($user, 'picture'),
]);
}
/**
* Return logout endpoint.
*
* @link https://zitadel.com/docs/apis/openidoauth/endpoints#end_session_endpoint
*
* @param string|null $idToken ID token from the access token response
* @return string
*
* @throws Invalid
*/
public function getLogoutUrl($idToken)
{
if (($redirect = $this->getConfig('post_logout_redirect_uri')) === null) {
throw new InvalidArgumentException('services.zitadel.post_logout_redirect_uri configuration is missing');
}
$query = [
'id_token_hint' => $idToken,
'client_id' => $this->clientId,
'post_logout_redirect_uri' => $redirect,
'state' => $this->getState(),
];
return $this->getConfig('base_url').'/oidc/v1/end_session?'.http_build_query($query);
}
}