1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SocialConnect\OpenIDConnect\Provider; |
6
|
|
|
|
7
|
|
|
use SocialConnect\Common\ArrayHydrator; |
8
|
|
|
use SocialConnect\Common\Entity\User; |
9
|
|
|
use SocialConnect\Common\Exception\InvalidArgumentException; |
10
|
|
|
use SocialConnect\OpenIDConnect\AccessToken; |
11
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
12
|
|
|
use SocialConnect\OpenIDConnect\AbstractProvider; |
13
|
|
|
use SocialConnect\Common\HttpStack; |
14
|
|
|
use SocialConnect\Provider\Session\SessionInterface; |
15
|
|
|
|
16
|
|
|
class AzureAD extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
const NAME = 'azure-ad'; |
19
|
|
|
const MS_GRAPH_API = 'https://graph.microsoft.com'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $baseUri; |
25
|
|
|
|
26
|
8 |
|
public function __construct(HttpStack $httpStack, SessionInterface $session, array $parameters) |
27
|
|
|
{ |
28
|
8 |
|
if (!isset($parameters['directoryId'])) { |
29
|
|
|
throw new \InvalidArgumentException('There is no "baseUri" given in the configuration'); |
30
|
|
|
} |
31
|
|
|
|
32
|
8 |
|
$this->baseUri = sprintf("https://login.microsoftonline.com/%s/", $parameters['directoryId']); |
33
|
|
|
|
34
|
8 |
|
parent::__construct($httpStack, $session, $parameters); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
public function getBaseUri() |
41
|
|
|
{ |
42
|
1 |
|
return $this->baseUri; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
1 |
|
public function getAuthorizeUri() |
49
|
|
|
{ |
50
|
1 |
|
return $this->baseUri . 'oauth2/v2.0/authorize'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
1 |
|
public function getRequestTokenUri() |
57
|
|
|
{ |
58
|
1 |
|
return $this->baseUri . 'oauth2/v2.0/token'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
1 |
|
public function getOpenIdUrl() |
65
|
|
|
{ |
66
|
1 |
|
return $this->baseUri . 'v2.0/.well-known/openid-configuration'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function getName() |
73
|
|
|
{ |
74
|
1 |
|
return self::NAME; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, ?AccessTokenInterface $accessToken = null): void |
78
|
|
|
{ |
79
|
|
|
if ($accessToken) { |
80
|
|
|
$headers['Authorization'] = 'Bearer ' . $accessToken->getToken(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1 |
|
public function extractIdentity(AccessTokenInterface $accessToken) |
88
|
|
|
{ |
89
|
1 |
|
if (!$accessToken instanceof AccessToken) { |
90
|
|
|
throw new InvalidArgumentException( |
91
|
|
|
'$accessToken must be instance AccessToken' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$jwt = $accessToken->getJwt(); |
96
|
|
|
|
97
|
1 |
|
$hydrator = new ArrayHydrator([ |
98
|
|
|
'sub' => 'id', |
99
|
|
|
'name' => 'username', |
100
|
|
|
'email' => 'email' |
101
|
|
|
]); |
102
|
|
|
|
103
|
1 |
|
$user = $hydrator->hydrate(new User(), $jwt->getPayload()); |
104
|
|
|
|
105
|
1 |
|
return $user; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
1 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
112
|
|
|
{ |
113
|
1 |
|
return $this->extractIdentity($accessToken); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function getScopeInline() |
120
|
|
|
{ |
121
|
|
|
return implode(' ', $this->scope); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|