Repository navigation
Expand file tree
/
Copy pathClient.php
More file actions
710 lines (580 loc) · 23.6 KB
/
Copy pathClient.php
File metadata and controls
710 lines (580 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<?php
namespace IndieAuth;
class Client {
const VERSION = '1.2.0';
private static $_headers = array();
private static $_body = array();
private static $_parsed = null;
private static $_parsedHash = null;
private static $_metadata_body = array();
private static $_metadata = null;
// The profile URL whose discovery produced $_metadata, so it is never used
// for another site. Null when the metadata was set directly.
private static $_metadata_for = null;
public static $http;
public static $clientID;
public static $redirectURL;
public static $random_byte_count = 8;
// Handles everything you need to start the authorization process.
// Discovers the user's auth endpoints, generates and stores a state in the session.
// Returns an authorization URL or an error array.
// Note: the third parameter $authorizationEndpoint is a nonstandard parameter only used
// when this client is used with services like indielogin.com
public static function begin($url, $scope=false, $authorizationEndpoint=false) {
if(!isset(self::$clientID) || !isset(self::$redirectURL)) {
return self::_errorResponse('not_configured', 'Before you can begin, you need to configure the clientID and redirectURL of the IndieAuth client');
}
$errorCode = false;
$url = self::normalizeMeURL($url);
$_SESSION['indieauth_entered_url'] = $url;
if(!$url) {
return self::_errorResponse('invalid_url', 'The URL provided was invalid');
}
$metadataEndpoint = self::discoverMetadataEndpoint($url);
if ($metadataEndpoint) {
$response = self::discoverIssuer($metadataEndpoint);
if ($response instanceof ErrorResponse) {
return $response->getArray();
}
$_SESSION['indieauth_issuer'] = $response;
}
if(!$authorizationEndpoint) {
$authorizationEndpoint = static::discoverAuthorizationEndpoint($url);
}
if(!$authorizationEndpoint) {
return self::_errorResponse('missing_authorization_endpoint', 'Could not find your authorization endpoint');
}
$scopes = self::parseNonProfileScopes($scope);
if(count($scopes)) {
$tokenEndpoint = static::discoverTokenEndpoint($url);
if(!$tokenEndpoint) {
return self::_errorResponse('missing_token_endpoint', 'Could not find your token endpoint. The token endpoint is required when requesting non-profile scopes');
}
}
$state = self::generateStateParameter();
$codeVerifier = self::generatePKCECodeVerifier();
$_SESSION['indieauth_state'] = $state;
$_SESSION['indieauth_code_verifier'] = $codeVerifier;
$_SESSION['indieauth_authorization_endpoint'] = $authorizationEndpoint;
if(isset($tokenEndpoint))
$_SESSION['indieauth_token_endpoint'] = $tokenEndpoint;
$authorizationURL = self::buildAuthorizationURL($authorizationEndpoint, [
'me' => $url,
'redirect_uri' => self::$redirectURL,
'client_id' => self::$clientID,
'state' => $state,
'code_verifier' => $codeVerifier,
'scope' => $scope,
]);
return [$authorizationURL, false];
}
public static function complete($params) {
$requiredSessionKeys = ['indieauth_entered_url', 'indieauth_state', 'indieauth_authorization_endpoint'];
foreach($requiredSessionKeys as $key) {
if(!isset($_SESSION[$key])) {
error_log("IndieAuth\Client: missing $key from session");
return self::_errorResponse('invalid_session',
'The session was missing data. Ensure that you are initializing the session before using this library');
}
}
// Check the state first, even for an error response: otherwise anyone
// could send the user to the redirect URL with error text of their own.
$response = self::validateStateMatch($params, $_SESSION['indieauth_state']);
if ($response instanceof ErrorResponse) {
error_log("IndieAuth\Client: failed to validate that the state matched");
return $response->getArray();
}
if(isset($params['error'])) {
error_log("IndieAuth\Client: found error in params");
return self::_errorResponse($params['error'], isset($params['error_description']) ? $params['error_description'] : '');
}
if(!isset($params['code'])) {
error_log("IndieAuth\Client: missing code in params");
return self::_errorResponse('invalid_response',
'The response from the authorization server did not return an authorization code or error information');
}
if (isset($_SESSION['indieauth_issuer'])) {
$response = self::validateIssuerMatch($params, $_SESSION['indieauth_issuer']);
if ($response instanceof ErrorResponse) {
error_log("IndieAuth\Client: failed to validate that the issuer matched");
return $response->getArray();
}
}
if(isset($_SESSION['indieauth_token_endpoint'])) {
$data = self::exchangeAuthorizationCode($_SESSION['indieauth_token_endpoint'], [
'code' => $params['code'],
'redirect_uri' => self::$redirectURL,
'client_id' => self::$clientID,
'code_verifier' => $_SESSION['indieauth_code_verifier'],
]);
} else {
$data = self::exchangeAuthorizationCode($_SESSION['indieauth_authorization_endpoint'], [
'code' => $params['code'],
'redirect_uri' => self::$redirectURL,
'client_id' => self::$clientID,
'code_verifier' => $_SESSION['indieauth_code_verifier'],
]);
}
if(!isset($data['response']['me'])) {
$error = 'indieauth_error';
if(!empty($data['response_details']['error']))
$error = $data['response_details']['error'];
elseif(!empty($data['response']['error']))
$error = $data['response']['error'];
$error_description = 'The authorization server did not return a valid response';
if(!empty($data['response_details']['error_description']))
$error_description = $data['response_details']['error_description'];
elseif(!empty($data['response']['error_description']))
$error_description = $data['response']['error_description'];
return self::_errorResponse($error, $error_description, $data);
}
// If the returned "me" is not the same as the entered "me", it must
// declare the same authorization server that just answered: the same
// issuer, authorization endpoint and token endpoint. The authorization
// endpoint alone proves nothing, since any site can copy someone else's
// into its own metadata while redeeming the code at its own token endpoint.
if($_SESSION['indieauth_entered_url'] != $data['response']['me']) {
if(!self::_sameAuthorizationServer($data['response']['me'])) {
return self::_errorResponse('invalid_authorization_endpoint',
'The authorization server of the returned profile URL did not match the initial authorization server', $data);
}
}
$data['me'] = self::normalizeMeURL($data['response']['me']);
self::_clearSessionData();
return [$data, false];
}
// Whether a profile URL declares the authorization server recorded in the
// session when the flow began.
private static function _sameAuthorizationServer($me) {
if(!self::_urlIsValid($me))
return false;
$metadataEndpoint = self::discoverMetadataEndpoint($me);
$issuer = null;
if($metadataEndpoint) {
$issuer = self::discoverIssuer($metadataEndpoint);
if($issuer instanceof ErrorResponse)
return false;
}
$expectedIssuer = isset($_SESSION['indieauth_issuer']) ? $_SESSION['indieauth_issuer'] : null;
if(($issuer === null) !== ($expectedIssuer === null))
return false;
if($issuer !== null && self::normalizeMeURL($issuer) !== self::normalizeMeURL($expectedIssuer))
return false;
if(static::discoverAuthorizationEndpoint($me) != $_SESSION['indieauth_authorization_endpoint'])
return false;
// Where the code was redeemed: the token endpoint, or the authorization
// endpoint when there was none (checked just above).
if(isset($_SESSION['indieauth_token_endpoint'])
&& static::discoverTokenEndpoint($me) != $_SESSION['indieauth_token_endpoint'])
return false;
return true;
}
/**
* Wrapper to create an ErrorResponse and return the array
* @return array
*/
private static function _errorResponse($error_code, $description, $debug = null) {
self::_clearSessionData();
$error = new ErrorResponse($error_code, $description, $debug);
return $error->getArray();
}
private static function _clearSessionData() {
unset($_SESSION['indieauth_entered_url']);
unset($_SESSION['indieauth_state']);
unset($_SESSION['indieauth_code_verifier']);
unset($_SESSION['indieauth_authorization_endpoint']);
unset($_SESSION['indieauth_token_endpoint']);
unset($_SESSION['indieauth_issuer']);
}
public static function setUpHTTP() {
// Unfortunately I've seen a bunch of websites return different content when the user agent is set to something like curl or other server-side libraries, so we have to pretend to be a browser to successfully get the real HTML
if(!isset(self::$http)) {
self::$http = new \p3k\HTTP();
self::$http->set_user_agent('indieauth-client/' . self::VERSION);
self::$http->_timeout = 10;
// You can customize the user agent for your application by calling
// IndieAuth\Client::$http->set_user_agent('Your User Agent String');
}
}
private static function _urlIsValid($url) {
$url = parse_url($url);
if($url == false
|| !array_key_exists('scheme', $url)
|| !in_array($url['scheme'], array('http','https'))
|| !array_key_exists('host', $url)
) {
// Invalid url
return false;
}
return true;
}
/**
* @see https://indieauth.spec.indieweb.org/#indieauth-server-metadata
*/
private static function _isIssuerValid($issuer, $metadata_endpoint) {
$issuer = self::normalizeMeURL($issuer);
if (!$issuer) {
return false;
}
$parts = parse_url($issuer);
if (!array_key_exists('scheme', $parts) || $parts['scheme'] != 'https') {
error_log("IndieAuth\Client.php: failed scheme");
return false;
}
if (array_key_exists('query', $parts) || array_key_exists('fragment', $parts)) {
error_log("IndieAuth\Client.php: failed query or fragment");
return false;
}
$metadata_endpoint = self::normalizeMeURL($metadata_endpoint);
// The issuer is a prefix of the metadata URL (IndieAuth), or the metadata
// URL is the RFC 8414 well-known location for the issuer, which inserts
// /.well-known/oauth-authorization-server between the host and the
// issuer's path. The next IndieAuth draft uses the RFC 8414 form; servers
// such as IndieKey.id already do.
if (strpos($metadata_endpoint, $issuer) === 0) {
return true;
}
if (rtrim($metadata_endpoint, '/') === self::wellKnownMetadataURL($issuer)) {
return true;
}
error_log("IndieAuth\Client.php: failed metadata and issuer comparison");
return false;
}
/**
* The RFC 8414 (section 3) metadata location for an issuer identifier:
* https://host/.well-known/oauth-authorization-server followed by the
* issuer's path. Without a trailing slash, for comparison. False when the
* issuer is not a URL.
*
* @param string $issuer a normalized issuer URL
* @return string|false
*/
public static function wellKnownMetadataURL($issuer) {
$parts = parse_url($issuer);
if(!is_array($parts) || !isset($parts['scheme'], $parts['host'])) {
return false;
}
$base = strtolower($parts['scheme']) . '://' . strtolower($parts['host']) . (isset($parts['port']) ? ':' . $parts['port'] : '');
$path = rtrim($parts['path'] ?? '', '/');
return $base . '/.well-known/oauth-authorization-server' . $path;
}
private static function _fetchHead($url) {
self::setUpHTTP();
if(array_key_exists($url, self::$_headers)) {
return self::$_headers[$url];
} else {
$headers = self::$http->head($url);
self::$_headers[$url] = $headers['header'];
return self::$_headers[$url];
}
}
private static function _fetchBody($url) {
self::setUpHTTP();
if(array_key_exists($url, self::$_body)) {
return self::$_body[$url];
} else {
$response = self::$http->get($url);
self::$_body[$url] = $response['body'];
return self::$_body[$url];
}
}
private static function _fetchMetadata($url) {
self::setUpHTTP();
if(array_key_exists($url, self::$_metadata_body)) {
return self::$_metadata_body[$url];
} else {
$response = self::$http->get($url);
self::setMetadata($url, $response['body']);
return self::$_metadata_body[$url];
}
}
private static function resetMetadata() {
self::$_metadata_body = array();
self::$_metadata = null;
self::$_metadata_for = null;
}
/**
* Set metadata body
* $body is expected to be JSON and will attempt
* to decode to array in self::$_metadata
*/
public static function setMetadata($url, $body) {
self::resetMetadata();
self::$_metadata_body[$url] = $body;
$metadata_array = json_decode($body, true);
if (!is_null($metadata_array)) {
self::$_metadata = $metadata_array;
}
}
public static function getMetadata() {
return self::$_metadata;
}
private static function _discoverEndpoint($url, $name) {
if(!self::_urlIsValid($url)) {
return null;
}
// First check the parsed metadata for the endpoint, if it was
// discovered from this URL (or set directly)
if (self::$_metadata_for === null || self::$_metadata_for === $url) {
if ($endpoint = self::_discoverFromMetadata($name)) {
return self::_endpointIfValid($endpoint);
}
}
// If not found, check the HTTP headers for the endpoint
$headerString = self::_fetchHead($url);
if($endpoint = self::_extractEndpointFromHeaders($headerString, $url, $name)) {
return self::_endpointIfValid($endpoint);
}
// If not found, check the body for a rel value
$html = self::_fetchBody($url);
return self::_endpointIfValid(self::_extractEndpointFromHTML($html, $url, $name));
}
// Only http(s) URLs are endpoints: anything else (file:, gopher:, ...) a
// site declares is ignored rather than handed to the HTTP client.
private static function _endpointIfValid($endpoint) {
if(!$endpoint)
return $endpoint;
return is_string($endpoint) && self::_urlIsValid($endpoint) ? $endpoint : false;
}
private static function _discoverFromMetadata($name) {
if (!self::$_metadata) {
return null;
}
if (array_key_exists($name, self::$_metadata)) {
return self::$_metadata[$name];
}
return null;
}
private static function _extractEndpointFromHeaders($headerString, $url, $name) {
$headers = \IndieWeb\http_rels($headerString);
if(isset($headers[$name][0])) {
return \Mf2\resolveUrl($url, $headers[$name][0]);
}
return false;
}
private static function _extractEndpointFromHTML($html, $url, $name) {
if(self::$_parsedHash != ($h=md5($html))) {
$parser = new \Mf2\Parser($html);
$parser->enableAlternates = true;
self::$_parsed = $parser->parse();
self::$_parsedHash = $h;
}
if(isset(self::$_parsed['rels'][$name][0])) {
return \Mf2\resolveUrl($url, self::$_parsed['rels'][$name][0]);
}
return false;
}
public static function discoverMetadataEndpoint($url) {
// Start clean: a site without metadata must not inherit the previous one's.
self::resetMetadata();
if ($endpoint = self::_discoverEndpoint($url, 'indieauth-metadata')) {
self::_fetchMetadata($endpoint);
self::$_metadata_for = $url;
}
return $endpoint;
}
/**
* @param string $metadataEndpoint
* @return IndieAuth\ErrorResponse if error, string of `issuer` if valid
*/
public static function discoverIssuer($metadataEndpoint) {
$issuer = self::_discoverFromMetadata('issuer');
if (!$issuer) {
return new ErrorResponse('invalid_issuer', 'No issuer found in metadata endpoint');
}
if (!(self::_isIssuerValid($issuer, $metadataEndpoint))) {
return new ErrorResponse('invalid_issuer', 'Issuer in metadata endpoint is not valid');
}
return $issuer;
}
public static function discoverAuthorizationEndpoint($url) {
return self::_discoverEndpoint($url, 'authorization_endpoint');
}
public static function discoverTokenEndpoint($url) {
return self::_discoverEndpoint($url, 'token_endpoint');
}
public static function discoverRevocationEndpoint($url) {
return self::_discoverEndpoint($url, 'revocation_endpoint');
}
public static function discoverIntrospectionEndpoint($url) {
return self::_discoverEndpoint($url, 'introspection_endpoint');
}
public static function discoverUserinfoEndpoint($url) {
return self::_discoverEndpoint($url, 'userinfo_endpoint');
}
public static function discoverMicropubEndpoint($url) {
return self::_discoverEndpoint($url, 'micropub');
}
public static function discoverMicrosubEndpoint($url) {
return self::_discoverEndpoint($url, 'microsub');
}
// Build the authorization URL for the given url and endpoint
public static function buildAuthorizationURL($authorizationEndpoint, $params) {
$required = ['me', 'redirect_uri', 'client_id', 'state'];
foreach($required as $r) {
if(!isset($params[$r])) {
throw new \Exception('Missing parameter to buildAuthorizationURL: '.$r);
}
}
$url = parse_url($authorizationEndpoint);
$request = array();
if(array_key_exists('query', $url)) {
parse_str($url['query'], $request);
}
$request['response_type'] = 'code';
$request['me'] = $params['me'];
$request['redirect_uri'] = $params['redirect_uri'];
$request['client_id'] = $params['client_id'];
$request['state'] = $params['state'];
if(!empty($params['scope'])) {
$request['scope'] = $params['scope'];
}
if(isset($params['code_verifier'])) {
$request['code_challenge'] = self::generatePKCECodeChallenge($params['code_verifier']);
$request['code_challenge_method'] = 'S256';
}
$url['query'] = http_build_query($request);
return self::build_url($url);
}
// Input: Any URL or string like "aaronparecki.com"
// Output: Normlized URL (default to http if no scheme, default "/" path)
// or return false if not a valid IndieAuth URL (has a fragment)
public static function normalizeMeURL($url) {
$me = parse_url($url);
if(array_key_exists('path', $me) && $me['path'] == '')
return false;
// parse_url returns just "path" for naked domains, so
// move that into the "host" instead
if(count($me) == 1 && array_key_exists('path', $me)) {
if(preg_match('/([^\/]+)(\/.+)/', $me['path'], $match)) {
$me['host'] = $match[1];
$me['path'] = $match[2];
} else {
$me['host'] = $me['path'];
unset($me['path']);
}
}
if(!array_key_exists('scheme', $me))
$me['scheme'] = 'http';
if(!array_key_exists('path', $me))
$me['path'] = '/';
// Invalid scheme
if(!in_array($me['scheme'], array('http','https')))
return false;
// fragment not allowed
if(array_key_exists('fragment', $me))
return false;
return self::build_url($me);
}
public static function build_url($parsed_url) {
$scheme = isset($parsed_url['scheme']) ? strtolower($parsed_url['scheme']) . '://' : '';
$host = isset($parsed_url['host']) ? strtolower($parsed_url['host']) : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
public static function parseNonProfileScopes($scope) {
$scopes = explode(' ', $scope);
return array_filter(array_diff($scopes, ['profile','email']));
}
/**
* @param array $params
* @param string $expected_state
* @return IndieAuth\ErrorResponse if error, void (null) if valid
*/
public static function validateStateMatch($params, $expected_state = '') {
if (!isset($params['state'])) {
return new ErrorResponse('missing_state', 'The authorization server did not return the state parameter');
}
if ($params['state'] !== $expected_state) {
return new ErrorResponse('invalid_state', 'The authorization server returned an invalid state parameter');
}
}
/**
* @param array $params
* @param string $expected_issuer
* @return IndieAuth\ErrorResponse if error, void (null) if valid
*/
public static function validateIssuerMatch($params, $expected_issuer = '') {
if (!$expected_issuer) {
return;
}
if (!isset($params['iss'])) {
return new ErrorResponse('missing_iss', 'The authorization server did not return the iss parameter');
}
if (self::normalizeMeURL($params['iss']) !== self::normalizeMeURL($expected_issuer)) {
return new ErrorResponse('invalid_iss', 'The authorization server returned an invalid iss parameter');
}
}
public static function exchangeAuthorizationCode($endpoint, $params) {
$required = ['code', 'redirect_uri', 'client_id'];
foreach($required as $r) {
if(!isset($params[$r])) {
throw new \Exception('Missing parameter to exchangeAuthorizationCode: '.$r);
}
}
self::setUpHTTP();
$request = [
'grant_type' => 'authorization_code',
'code' => $params['code'],
'redirect_uri' => $params['redirect_uri'],
'client_id' => $params['client_id'],
];
if(isset($params['code_verifier'])) {
$request['code_verifier'] = $params['code_verifier'];
}
$response = self::$http->post($endpoint, http_build_query($request), [
'Accept: application/json, application/x-www-form-urlencoded;q=0.8'
]);
$data = json_decode($response['body'], true);
if(!$data) {
// Parse as form-encoded for legacy server support
$data = array();
parse_str($response['body'], $data);
}
return [
'response' => $data,
'raw_response' => $response['body'],
'response_code' => $response['code'],
'response_details' => $response,
];
}
public static function representativeHCard($url) {
$html = self::_fetchBody($url);
$parsed = \Mf2\parse($html, $url);
return \Mf2\HCard\representative($parsed, $url);
}
// Support legacy PHP random string generation methods
private static function generateRandomString($numBytes) {
if(function_exists('random_bytes')) {
$bytes = random_bytes($numBytes);
} elseif(function_exists('openssl_random_pseudo_bytes')){
$bytes = openssl_random_pseudo_bytes($numBytes);
} else {
$bytes = '';
for($i=0, $bytes=''; $i < $numBytes; $i++) {
$bytes .= chr(mt_rand(0, 255));
}
}
return bin2hex($bytes);
}
// Optional helper method to generate a state parameter. You can just as easily do this yourself some other way.
public static function generateStateParameter() {
return self::generateRandomString(self:: $random_byte_count);
}
/** PKCE Helpers **/
public static function generatePKCECodeVerifier() {
return self::generateRandomString(32);
}
private static function generatePKCECodeChallenge($plaintext) {
return self::base64_urlencode(hash('sha256', $plaintext, true));
}
private static function base64_urlencode($string) {
return rtrim(strtr(base64_encode($string), '+/', '-_'), '=');
}
}