-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKCETrait.php
104 lines (84 loc) · 2.95 KB
/
PKCETrait.php
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
<?php
/**
* Trait PKCETrait
*
* @created 19.09.2024
* @author smiley <smiley@chillerlan.net>
* @copyright 2024 smiley
* @license MIT
*/
declare(strict_types=1);
namespace chillerlan\OAuth\Core;
use chillerlan\OAuth\Providers\ProviderException;
use chillerlan\Utilities\{Crypto, Str};
use const SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING;
/**
* Implements PKCE (Proof Key for Code Exchange) functionality
*
* @see \chillerlan\OAuth\Core\PKCE
*/
trait PKCETrait{
/**
* implements PKCE::setCodeChallenge()
*
* @see \chillerlan\OAuth\Core\PKCE::setCodeChallenge()
* @see \chillerlan\OAuth\Core\OAuth2Provider::getAuthorizationURLRequestParams()
*
* @param array<string, string> $params
* @return array<string, string>
*/
final public function setCodeChallenge(array $params, string $challengeMethod):array{
if(!isset($params['response_type']) || $params['response_type'] !== 'code'){
throw new ProviderException('invalid authorization request params');
}
$verifier = $this->generateVerifier($this->options->pkceVerifierLength);
$params['code_challenge'] = $this->generateChallenge($verifier, $challengeMethod);
$params['code_challenge_method'] = $challengeMethod;
$this->storage->storeCodeVerifier($verifier, $this->name);
return $params;
}
/**
* implements PKCE::setCodeVerifier()
*
* @see \chillerlan\OAuth\Core\PKCE::setCodeVerifier()
* @see \chillerlan\OAuth\Core\OAuth2Provider::getAccessTokenRequestBodyParams()
*
* @param array<string, string> $params
* @return array<string, string>
*/
final public function setCodeVerifier(array $params):array{
if(!isset($params['grant_type'], $params['code']) || $params['grant_type'] !== 'authorization_code'){
throw new ProviderException('invalid authorization request body');
}
$params['code_verifier'] = $this->storage->getCodeVerifier($this->name);
// delete verifier after use
$this->storage->clearCodeVerifier($this->name);
return $params;
}
/**
* implements PKCE::generateVerifier()
*
* @see \chillerlan\OAuth\Core\PKCE::generateVerifier()
* @see \chillerlan\OAuth\Core\OAuth2Provider::setCodeChallenge()
*/
final public function generateVerifier(int $length):string{
return Crypto::randomString($length, PKCE::VERIFIER_CHARSET);
}
/**
* implements PKCE::generateChallenge()
*
* @see \chillerlan\OAuth\Core\PKCE::generateChallenge()
* @see \chillerlan\OAuth\Core\OAuth2Provider::setCodeChallenge()
*/
final public function generateChallenge(string $verifier, string $challengeMethod):string{
if($challengeMethod === PKCE::CHALLENGE_METHOD_PLAIN){
return $verifier;
}
$verifier = match($challengeMethod){
PKCE::CHALLENGE_METHOD_S256 => Crypto::sha256($verifier, true),
// no other hash methods yet
default => throw new ProviderException('invalid PKCE challenge method'), // @codeCoverageIgnore
};
return Str::base64encode($verifier, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING);
}
}