Repository navigation
Expand file tree
/
Copy pathOpenIDProvider.php
More file actions
73 lines (56 loc) · 1.82 KB
/
Copy pathOpenIDProvider.php
File metadata and controls
73 lines (56 loc) · 1.82 KB
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
<?php
// Copyright (c) Tribufu. All Rights Reserved.
// SPDX-License-Identifier: MIT
namespace Paymenter\Extensions\Others\OpenID;
use GuzzleHttp\RequestOptions;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\User;
/**
* Authorization code flow of an OpenID Connect provider, with the endpoints of its discovery document.
*/
class OpenIDProvider extends AbstractProvider
{
protected $scopes = ['openid', 'profile', 'email'];
protected $scopeSeparator = ' ';
protected array $endpoints = [];
protected ?string $idToken = null;
public function setEndpoints(array $endpoints): static
{
$this->endpoints = $endpoints;
return $this;
}
public function getIdToken(): ?string
{
return $this->idToken;
}
public function getAccessTokenResponse($code)
{
$response = parent::getAccessTokenResponse($code);
$this->idToken = $response['id_token'] ?? null;
return $response;
}
protected function getAuthUrl($state)
{
return $this->buildAuthUrlFromBase($this->endpoints['authorization_endpoint'], $state);
}
protected function getTokenUrl()
{
return $this->endpoints['token_endpoint'];
}
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get($this->endpoints['userinfo_endpoint'], [
RequestOptions::HEADERS => ['Authorization' => 'Bearer ' . $token],
]);
return json_decode($response->getBody(), true);
}
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['sub'],
'nickname' => $user['preferred_username'] ?? null,
'name' => $user['name'] ?? null,
'email' => $user['email'] ?? null,
]);
}
}