|
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
|
|
|
$production = self::productionEnvironment(); |
|
14
|
|
|
if (!isset($options['urlAuthorize']) || |
|
15
|
|
|
!isset($options['urlAccessToken']) || |
|
16
|
|
|
!isset($options['urlResourceOwnerDetails']) |
|
17
|
|
|
) { |
|
18
|
|
|
$options['urlAuthorize'] = $production['urlAuthorize']; |
|
19
|
|
|
$options['urlAccessToken'] = $production['urlAccessToken']; |
|
20
|
|
|
$options['urlResourceOwnerDetails'] = $production['urlResourceOwnerDetails']; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
parent::__construct($options, $collaborators); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Cria uma instância para ambiente de homologação |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $options |
|
30
|
|
|
* @param array $collaborators |
|
31
|
|
|
* @return static |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function staging(array $options, array $collaborators = []): self |
|
34
|
|
|
{ |
|
35
|
|
|
return new self(array_merge($options, self::stagingEnvironment()), $collaborators); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Cria uma instância para ambiente de produção |
|
40
|
|
|
* Pode ser criado diretamente via construtor |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $options |
|
43
|
|
|
* @param array $collaborators |
|
44
|
|
|
* @return static |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function production(array $options, array $collaborators = []): self |
|
47
|
|
|
{ |
|
48
|
|
|
return new self(array_merge($options, self::productionEnvironment()), $collaborators); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritDoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getAuthorizationUrl(array $options = []): string |
|
55
|
|
|
{ |
|
56
|
|
|
if (!isset($options['nonce'])) { |
|
57
|
|
|
$options['nonce'] = md5(uniqid('govbr', true)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return parent::getAuthorizationUrl($options); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getDefaultScopes(): array |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
'openid', |
|
67
|
|
|
'email', |
|
68
|
|
|
'phone', |
|
69
|
|
|
'profile', |
|
70
|
|
|
'govbr_confiabilidades' |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getScopeSeparator(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return '+'; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritDoc |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
83
|
|
|
{ |
|
84
|
|
|
return new GovBrUser($response, $token); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getAvatar(GovBrUser $govBrUser): ?Avatar |
|
88
|
|
|
{ |
|
89
|
|
|
$request = $this->getAuthenticatedRequest( |
|
90
|
|
|
self::METHOD_GET, |
|
91
|
|
|
$govBrUser->getAvatarUrl(), |
|
92
|
|
|
$govBrUser->token() |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$response = $this->getResponse($request); |
|
96
|
|
|
|
|
97
|
|
|
return |
|
98
|
|
|
new Avatar( |
|
99
|
|
|
(string) $response->getBody(), |
|
100
|
|
|
$response->getHeaderLine('Content-type') |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private static function productionEnvironment(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return [ |
|
107
|
|
|
'urlAuthorize' => 'https://sso.acesso.gov.br/authorize', |
|
108
|
|
|
'urlAccessToken' => 'https://sso.acesso.gov.br/token', |
|
109
|
|
|
'urlResourceOwnerDetails' => 'https://sso.acesso.gov.br/userinfo', |
|
110
|
|
|
]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private static function stagingEnvironment(): array |
|
114
|
|
|
{ |
|
115
|
|
|
return [ |
|
116
|
|
|
'urlAuthorize' => 'https://sso.staging.acesso.gov.br/authorize', |
|
117
|
|
|
'urlAccessToken' => 'https://sso.staging.acesso.gov.br/token', |
|
118
|
|
|
'urlResourceOwnerDetails' => 'https://sso.staging.acesso.gov.br/userinfo', |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|