|
1
|
|
|
<?php namespace nyx\auth\id; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Identity Provider |
|
5
|
|
|
* |
|
6
|
|
|
* @package Nyx\Auth |
|
7
|
|
|
* @version 0.1.0 |
|
8
|
|
|
* @author Michal Chojnacki <[email protected]> |
|
9
|
|
|
* @copyright 2012-2017 Nyx Dev Team |
|
10
|
|
|
* @link https://github.com/unyx/nyx |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class Provider implements interfaces\Provider |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var credentials\Client The credentials used to identify this consumer with the provider. |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $consumer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var bool Whether the Provider should attempt to retrieve the email address of an entity when performing |
|
21
|
|
|
* identify calls. |
|
22
|
|
|
* |
|
23
|
|
|
* This is kept as a separate, publicly settable behavioural flag because many Providers make |
|
24
|
|
|
* the entity's email address(es) available only with special permission scopes and/or at different |
|
25
|
|
|
* endpoints, meaning simple identify() calls which do not rely on the email being available |
|
26
|
|
|
* can in those cases be simplified by setting this flag to false. |
|
27
|
|
|
* |
|
28
|
|
|
* Some Identity Providers do not provide the entity's email address(es) under any circumstances, |
|
29
|
|
|
* in which case this flag will have no effect. |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $shouldProvideEmailAddress = true; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \GuzzleHttp\ClientInterface The underlying HTTP Client used for communicating with the provider. |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $httpClient; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructs a new Identity Provider instance tied to the given client/consumer/application Credentials. |
|
40
|
|
|
* |
|
41
|
|
|
* @param credentials\Client $consumer |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(credentials\Client $consumer) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->consumer = $consumer; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getAuthorizeUrl(array $parameters = []) : string |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->buildUrl(static::URL_AUTHORIZE, $parameters); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getExchangeUrl() : string |
|
60
|
|
|
{ |
|
61
|
|
|
return static::URL_EXCHANGE; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritDoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getIdentifyUrl() : string |
|
68
|
|
|
{ |
|
69
|
|
|
return static::URL_IDENTIFY; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Checks whether the Provider should attempt to retrieve the email address of an entity when performing |
|
74
|
|
|
* identify calls. |
|
75
|
|
|
* |
|
76
|
|
|
* @see $shouldProvideEmailAddress |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function shouldProvideEmailAddress() : bool |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->shouldProvideEmailAddress; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets whether the Provider should attempt to retrieve the email address of an entity when performing |
|
86
|
|
|
* identify calls. |
|
87
|
|
|
* |
|
88
|
|
|
* @param bool $bool |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
|
|
public function setShouldProvideEmailAddress(bool $bool) : Provider |
|
92
|
|
|
{ |
|
93
|
|
|
$this->shouldProvideEmailAddress = $bool; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Builds an URL string from the given base URL and optional additional query parameters. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $base The base URL. |
|
102
|
|
|
* @param array $query Additional query parameters. |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function buildUrl(string $base, array $query = []) : string |
|
106
|
|
|
{ |
|
107
|
|
|
return empty($query) ? $base : $base.'?'.http_build_query($query, null, '&'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Returns the underlying HTTP Client used for communicating with the provider. |
|
112
|
|
|
* |
|
113
|
|
|
* @return \GuzzleHttp\ClientInterface |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getHttpClient() : \GuzzleHttp\ClientInterface |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->httpClient ?: $this->httpClient = new \GuzzleHttp\Client(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Sets the underlying HTTP Client used for communicating with the provider. |
|
122
|
|
|
* |
|
123
|
|
|
* @param \GuzzleHttp\ClientInterface $client |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setHttpClient(\GuzzleHttp\ClientInterface $client) : Provider |
|
127
|
|
|
{ |
|
128
|
|
|
$this->httpClient = $client; |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|