1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace BrenoRoosevelt\OAuth2\Client; |
5
|
|
|
|
6
|
|
|
use League\OAuth2\Client\Provider\GenericProvider; |
7
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
8
|
|
|
|
9
|
|
|
final class GovBr extends GenericProvider |
10
|
|
|
{ |
11
|
|
|
public function __construct(array $options = [], array $collaborators = []) |
12
|
|
|
{ |
13
|
|
|
$options = array_merge($options, Environment::production()); |
14
|
|
|
parent::__construct($options, $collaborators); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function stagingEnvironment(array $options): self |
18
|
|
|
{ |
19
|
|
|
return new self(array_merge($options, Environment::staging())); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public static function productionEnvironment(array $options): self |
23
|
|
|
{ |
24
|
|
|
return new self(array_merge($options, Environment::production())); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritDoc |
29
|
|
|
*/ |
30
|
|
|
public function getAuthorizationUrl(array $options = []): string |
31
|
|
|
{ |
32
|
|
|
if (!isset($options['nonce'])) { |
33
|
|
|
$options['nonce'] = $this->getNonce(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return parent::getAuthorizationUrl($options); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function getNonce(): string |
40
|
|
|
{ |
41
|
|
|
return md5(uniqid('govbr', true)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getDefaultScopes(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
'openid', |
48
|
|
|
'email', |
49
|
|
|
'phone', |
50
|
|
|
'profile', |
51
|
|
|
'govbr_confiabilidades' |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getScopeSeparator(): string |
56
|
|
|
{ |
57
|
|
|
return '+'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
64
|
|
|
{ |
65
|
|
|
return new GovBrUser($response, $token); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getAvatar(GovBrUser $govBrUser): ?Avatar |
69
|
|
|
{ |
70
|
|
|
$request = $this->getAuthenticatedRequest( |
71
|
|
|
self::METHOD_GET, |
72
|
|
|
$govBrUser->getAvatarUrl(), |
73
|
|
|
$govBrUser->token() |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$response = $this->getResponse($request); |
77
|
|
|
|
78
|
|
|
return |
79
|
|
|
new Avatar( |
80
|
|
|
(string) $response->getBody(), |
81
|
|
|
$response->getHeaderLine('Content-type') |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|