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