-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByPKCE.class.php
More file actions
executable file
·62 lines (52 loc) · 1.79 KB
/
ByPKCE.class.php
File metadata and controls
executable file
·62 lines (52 loc) · 1.79 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
<?php namespace web\auth\oauth;
use lang\IllegalArgumentException;
/** @test web.auth.unittest.ByPKCETest */
class ByPKCE extends Credentials {
const SUPPORTED= ['S256', 'plain'];
private $challenge, $method;
/**
* Creates credentials with a client ID and method.
* Support the `S256` and `plain` methods.
*
* @param string $clientId
* @param string $method
* @throws lang.IllegalArgumentException
*/
public function __construct($clientId, $method) {
parent::__construct($clientId);
if ('S256' === $method) {
$this->challenge= fn($verifier) => JWT::encode(hash('sha256', $verifier, true));
} else if ('plain' === $method) {
$this->challenge= fn($verifier) => $verifier;
} else {
throw new IllegalArgumentException('Unsupported method '.$method.', expected one of ['.implode(', ', self::SUPPORTED).']');
}
$this->method= $method;
}
/** @return string */
public function method() { return $this->method; }
/** Returns authorization seed */
public function seed(): array {
static $UNRESERVED= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~';
$random= random_bytes(64);
$verifier= '';
for ($i= 0; $i < 64; $i++) {
$verifier.= $UNRESERVED[ord($random[$i]) % 66];
}
return ['verifier' => $verifier];
}
/** Returns parameters to be passed on to authorization */
public function pass(array $seed): array {
return [
'code_challenge' => ($this->challenge)($seed['verifier']),
'code_challenge_method' => $this->method,
];
}
/** Returns parameters to be used in authentication process */
public function params(string $endpoint, array $seed= []): array {
return [
'client_id' => $this->key,
'code_verifier' => $seed['verifier'],
];
}
}