3
3
namespace Portier \Client ;
4
4
5
5
use Lcobucci \JWT \Configuration as JwtConfig ;
6
- use Lcobucci \JWT \Validation \Constraint as JwtConstraint ;
7
6
use Lcobucci \JWT \Signer as JwtSigner ;
7
+ use Lcobucci \JWT \Validation \Constraint as JwtConstraint ;
8
8
9
9
/**
10
10
* Client for a Portier broker.
@@ -13,6 +13,7 @@ class Client
13
13
{
14
14
/**
15
15
* Default Portier broker origin.
16
+ *
16
17
* @var string
17
18
*/
18
19
public const DEFAULT_BROKER = 'https://broker.portier.io ' ;
@@ -25,20 +26,23 @@ class Client
25
26
26
27
/**
27
28
* The origin of the Portier broker.
29
+ *
28
30
* @var string
29
31
*/
30
32
public $ broker = self ::DEFAULT_BROKER ;
31
33
32
34
/**
33
35
* The number of seconds of clock drift to allow.
36
+ *
34
37
* @var int
35
38
*/
36
39
public $ leeway = 3 * 60 ;
37
40
38
41
/**
39
- * Constructor
40
- * @param StoreInterface $store Store implementation to use.
41
- * @param string $redirectUri URL that Portier will redirect to.
42
+ * Constructor.
43
+ *
44
+ * @param StoreInterface $store store implementation to use
45
+ * @param string $redirectUri URL that Portier will redirect to
42
46
*/
43
47
public function __construct (StoreInterface $ store , string $ redirectUri )
44
48
{
@@ -62,7 +66,7 @@ public static function normalize(string $email): string
62
66
assert (defined ('MB_CASE_FOLD ' ) && function_exists ('idn_to_ascii ' ));
63
67
64
68
$ localEnd = strrpos ($ email , '@ ' );
65
- if ($ localEnd === false ) {
69
+ if (false === $ localEnd ) {
66
70
return '' ;
67
71
}
68
72
@@ -79,8 +83,8 @@ public static function normalize(string $email): string
79
83
IDNA_USE_STD3_RULES | IDNA_CHECK_BIDI ,
80
84
INTL_IDNA_VARIANT_UTS46
81
85
);
82
- if (empty ($ host ) || $ host [ 0 ] === ' [ ' ||
83
- filter_var ($ host , FILTER_VALIDATE_IP ) !== false ) {
86
+ if (empty ($ host ) || ' [ ' === $ host [ 0 ] ||
87
+ false !== filter_var ($ host , FILTER_VALIDATE_IP )) {
84
88
return '' ;
85
89
}
86
90
@@ -89,8 +93,10 @@ public static function normalize(string $email): string
89
93
90
94
/**
91
95
* Start authentication of an email address.
92
- * @param string $email Email address to authenticate.
93
- * @return string URL to redirect the browser to.
96
+ *
97
+ * @param string $email email address to authenticate
98
+ *
99
+ * @return string URL to redirect the browser to
94
100
*/
95
101
public function authenticate (string $ email ): string
96
102
{
@@ -109,13 +115,16 @@ public function authenticate(string $email): string
109
115
'client_id ' => $ this ->clientId ,
110
116
'redirect_uri ' => $ this ->redirectUri ,
111
117
]);
112
- return $ authEndpoint . '? ' . $ query ;
118
+
119
+ return $ authEndpoint .'? ' .$ query ;
113
120
}
114
121
115
122
/**
116
123
* Verify a token received on our `redirect_uri`.
117
- * @param string $token The received `id_token` parameter value.
118
- * @return string The verified email address.
124
+ *
125
+ * @param string $token the received `id_token` parameter value
126
+ *
127
+ * @return string the verified email address
119
128
*/
120
129
public function verify (string $ token ): string
121
130
{
@@ -145,20 +154,20 @@ public function verify(string $token): string
145
154
$ publicKey = null ;
146
155
foreach ($ keysDoc ->keys as $ key ) {
147
156
if ($ key instanceof \stdClass &&
148
- isset ($ key ->alg ) && $ key -> alg === ' RS256 ' &&
157
+ isset ($ key ->alg ) && ' RS256 ' === $ key -> alg &&
149
158
isset ($ key ->kid ) && $ key ->kid === $ kid &&
150
159
isset ($ key ->n ) && isset ($ key ->e )) {
151
160
$ publicKey = self ::parseJwk ($ key );
152
161
break ;
153
162
}
154
163
}
155
- if ($ publicKey === null ) {
164
+ if (null === $ publicKey ) {
156
165
throw new \Exception ('Cannot find the public key used to sign the token ' );
157
166
}
158
167
159
168
// Validate the token claims.
160
169
$ clock = \Lcobucci \Clock \SystemClock::fromUTC ();
161
- $ leeway = new \DateInterval ('PT ' . $ this ->leeway . 'S ' );
170
+ $ leeway = new \DateInterval ('PT ' . $ this ->leeway . 'S ' );
162
171
$ constraints = [
163
172
new JwtConstraint \SignedWith (new JwtSigner \Rsa \Sha256 (), $ publicKey ),
164
173
new JwtConstraint \IssuedBy ($ this ->broker ),
@@ -201,7 +210,8 @@ public function verify(string $token): string
201
210
*/
202
211
private function fetchDiscovery (): \stdClass
203
212
{
204
- $ discoveryUrl = $ this ->broker . '/.well-known/openid-configuration ' ;
213
+ $ discoveryUrl = $ this ->broker .'/.well-known/openid-configuration ' ;
214
+
205
215
return $ this ->store ->fetchCached ('discovery ' , $ discoveryUrl );
206
216
}
207
217
@@ -221,19 +231,19 @@ private static function parseJwk(\stdClass $jwk): JwtSigner\Key
221
231
$ encoded = base64_encode ($ pkey ->getBinary ());
222
232
223
233
return JwtSigner \Key \InMemory::plainText (
224
- "-----BEGIN PUBLIC KEY----- \n" .
225
- chunk_split ($ encoded , 64 , "\n" ) .
234
+ "-----BEGIN PUBLIC KEY----- \n" .
235
+ chunk_split ($ encoded , 64 , "\n" ).
226
236
"-----END PUBLIC KEY----- \n"
227
237
);
228
238
}
229
239
230
240
/**
231
- * Get the origin for a URL
241
+ * Get the origin for a URL.
232
242
*/
233
243
private static function getOrigin (string $ url ): string
234
244
{
235
245
$ components = parse_url ($ url );
236
- if ($ components === false ) {
246
+ if (false === $ components ) {
237
247
throw new \Exception ('Could not parse the redirect URI ' );
238
248
}
239
249
@@ -247,12 +257,12 @@ private static function getOrigin(string $url): string
247
257
}
248
258
$ host = $ components ['host ' ];
249
259
250
- $ res = $ scheme . ':// ' . $ host ;
260
+ $ res = $ scheme. ':// ' . $ host ;
251
261
if (isset ($ components ['port ' ])) {
252
262
$ port = $ components ['port ' ];
253
- if (($ scheme === ' http ' && $ port !== 80 ) ||
254
- ($ scheme === ' https ' && $ port !== 443 )) {
255
- $ res .= ': ' . $ port ;
263
+ if ((' http ' === $ scheme && 80 !== $ port ) ||
264
+ (' https ' === $ scheme && 443 !== $ port )) {
265
+ $ res .= ': ' . $ port ;
256
266
}
257
267
}
258
268
@@ -262,8 +272,8 @@ private static function getOrigin(string $url): string
262
272
private static function decodeBase64Url (string $ input ): string
263
273
{
264
274
$ output = base64_decode (strtr ($ input , '-_ ' , '+/ ' ), true );
265
- if ($ output === false ) {
266
- throw new \Exception (" Invalid base64 " );
275
+ if (false === $ output ) {
276
+ throw new \Exception (' Invalid base64 ' );
267
277
}
268
278
269
279
return $ output ;
0 commit comments