13
13
14
14
namespace chillerlan \OAuth \Core ;
15
15
16
+ use chillerlan \Utilities \File ;
16
17
use DirectoryIterator ;
17
18
use InvalidArgumentException ;
18
19
use ReflectionClass ;
19
- use RuntimeException ;
20
20
use function hash ;
21
- use function random_bytes ;
22
- use function realpath ;
23
- use function sodium_base642bin ;
24
- use function sodium_bin2base64 ;
25
- use function sodium_bin2hex ;
26
- use function sodium_crypto_secretbox ;
27
- use function sodium_crypto_secretbox_keygen ;
28
- use function sodium_crypto_secretbox_open ;
29
- use function sodium_hex2bin ;
30
- use function sodium_memzero ;
31
21
use function substr ;
32
22
use function trim ;
33
- use const SODIUM_BASE64_VARIANT_ORIGINAL ;
34
- use const SODIUM_CRYPTO_SECRETBOX_NONCEBYTES ;
35
23
36
24
/**
37
25
* Common utilities for use with the OAuth providers
38
26
*/
39
27
class Utilities{
40
28
41
- final public const ENCRYPT_FORMAT_BINARY = 0b00 ;
42
- final public const ENCRYPT_FORMAT_BASE64 = 0b01 ;
43
- final public const ENCRYPT_FORMAT_HEX = 0b10 ;
44
-
45
29
/**
46
30
* Fetches a list of provider classes in the given directory
47
31
*
48
32
* @return array<string, array<string, string>>
49
33
*/
50
34
public static function getProviders (string |null $ providerDir = null , string |null $ namespace = null ):array {
51
- $ providerDir = realpath (($ providerDir ?? __DIR__ .'/../Providers ' ));
35
+ $ providerDir = File:: realpath (($ providerDir ?? __DIR__ .'/../Providers ' ));
52
36
$ namespace = trim (($ namespace ?? 'chillerlan \\OAuth \\Providers ' ), '\\' );
53
37
$ providers = [];
54
38
55
- if ($ providerDir === false ){
56
- throw new InvalidArgumentException ('invalid $providerDir ' );
57
- }
58
-
59
39
foreach (new DirectoryIterator ($ providerDir ) as $ e ){
60
40
61
41
if ($ e ->getExtension () !== 'php ' ){
@@ -79,70 +59,4 @@ public static function getProviders(string|null $providerDir = null, string|null
79
59
return $ providers ;
80
60
}
81
61
82
- /**
83
- * Creates a new cryptographically secure random encryption key (in hexadecimal format)
84
- */
85
- public static function createEncryptionKey ():string {
86
- return sodium_bin2hex (sodium_crypto_secretbox_keygen ());
87
- }
88
-
89
- /**
90
- * encrypts the given $data with $key, $format output [binary, base64, hex]
91
- *
92
- * @see \sodium_crypto_secretbox()
93
- * @see \sodium_bin2base64()
94
- * @see \sodium_bin2hex()
95
- */
96
- public static function encrypt (string $ data , string $ keyHex , int $ format = self ::ENCRYPT_FORMAT_HEX ):string {
97
- $ nonce = random_bytes (SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
98
- $ box = sodium_crypto_secretbox ($ data , $ nonce , sodium_hex2bin ($ keyHex ));
99
-
100
- $ out = match ($ format ){
101
- self ::ENCRYPT_FORMAT_BINARY => $ nonce .$ box ,
102
- self ::ENCRYPT_FORMAT_BASE64 => sodium_bin2base64 ($ nonce .$ box , SODIUM_BASE64_VARIANT_ORIGINAL ),
103
- self ::ENCRYPT_FORMAT_HEX => sodium_bin2hex ($ nonce .$ box ),
104
- default => throw new InvalidArgumentException ('invalid format ' ), // @codeCoverageIgnore
105
- };
106
-
107
- sodium_memzero ($ data );
108
- sodium_memzero ($ keyHex );
109
- sodium_memzero ($ nonce );
110
- sodium_memzero ($ box );
111
-
112
- return $ out ;
113
- }
114
-
115
- /**
116
- * decrypts the given $encrypted data with $key from $format input [binary, base64, hex]
117
- *
118
- * @see \sodium_crypto_secretbox_open()
119
- * @see \sodium_base642bin()
120
- * @see \sodium_hex2bin()
121
- */
122
- public static function decrypt (string $ encrypted , string $ keyHex , int $ format = self ::ENCRYPT_FORMAT_HEX ):string {
123
-
124
- $ bin = match ($ format ){
125
- self ::ENCRYPT_FORMAT_BINARY => $ encrypted ,
126
- self ::ENCRYPT_FORMAT_BASE64 => sodium_base642bin ($ encrypted , SODIUM_BASE64_VARIANT_ORIGINAL ),
127
- self ::ENCRYPT_FORMAT_HEX => sodium_hex2bin ($ encrypted ),
128
- default => throw new InvalidArgumentException ('invalid format ' ), // @codeCoverageIgnore
129
- };
130
-
131
- $ nonce = substr ($ bin , 0 , SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
132
- $ box = substr ($ bin , SODIUM_CRYPTO_SECRETBOX_NONCEBYTES );
133
- $ data = sodium_crypto_secretbox_open ($ box , $ nonce , sodium_hex2bin ($ keyHex ));
134
-
135
- sodium_memzero ($ encrypted );
136
- sodium_memzero ($ keyHex );
137
- sodium_memzero ($ bin );
138
- sodium_memzero ($ nonce );
139
- sodium_memzero ($ box );
140
-
141
- if ($ data === false ){
142
- throw new RuntimeException ('decryption failed ' ); // @codeCoverageIgnore
143
- }
144
-
145
- return $ data ;
146
- }
147
-
148
62
}
0 commit comments