13
13
use OpenTok \Exception \InvalidArgumentException ;
14
14
use JohnStevenson \JsonWorks \Document ;
15
15
use JohnStevenson \JsonWorks \Utils as JsonUtils ;
16
+ use RuntimeException ;
16
17
17
18
/**
18
19
* @internal
@@ -25,13 +26,44 @@ class Validators
25
26
26
27
public const STREAM_MODES = ['auto ' , 'manual ' ];
27
28
28
- public static function validateApiKey ($ apiKey)
29
+ public static function isVonageKeypair ($ apiKey, $ apiSecret ): bool
29
30
{
30
- if (!(is_string ($ apiKey ) || is_int ($ apiKey ))) {
31
- throw new InvalidArgumentException (
32
- 'The apiKey was not a string nor an integer: ' . print_r ($ apiKey , true )
33
- );
31
+ if (!is_string ($ apiKey ) || !is_string ($ apiSecret )) {
32
+ throw new InvalidArgumentException ("API Key and API Secret must be strings. " );
33
+ }
34
+
35
+ $ isOpenTokKey = preg_match ('/^\d+$/ ' , $ apiKey );
36
+ $ isOpenTokSecret = preg_match ('/^[a-f0-9]{40}$/i ' , $ apiSecret );
37
+
38
+ if ($ isOpenTokKey && $ isOpenTokSecret ) {
39
+ return false ;
34
40
}
41
+
42
+ $ isVonageApplicationId = preg_match ('/^[a-f0-9\-]{36}$/i ' , $ apiKey );
43
+ $ isVonagePrivateKey = self ::isValidPrivateKey ($ apiSecret );
44
+
45
+ if ($ isVonageApplicationId && $ isVonagePrivateKey ) {
46
+ return true ;
47
+ }
48
+
49
+ // Mixed formats or invalid formats - throw an exception
50
+ throw new InvalidArgumentException ("Invalid Vonage Keypair credentials provided. " );
51
+ }
52
+
53
+ private static function isValidPrivateKey (string $ filePath ): bool
54
+ {
55
+ if (!file_exists ($ filePath ) || !is_readable ($ filePath )) {
56
+ throw new InvalidArgumentException ("Private key file does not exist or is not readable. " );
57
+ }
58
+
59
+ $ keyContents = file_get_contents ($ filePath );
60
+
61
+ if ($ keyContents === false ) {
62
+ throw new RuntimeException ("Failed to read private key file. " );
63
+ }
64
+
65
+ // Check if it contains a valid private RSA key header
66
+ return (bool ) preg_match ('/^-----BEGIN PRIVATE KEY-----[\s\S]+-----END PRIVATE KEY-----$/m ' , trim ($ keyContents ));
35
67
}
36
68
37
69
public static function validateForceMuteAllOptions (array $ options )
@@ -56,13 +88,6 @@ public static function validateForceMuteAllOptions(array $options)
56
88
}
57
89
}
58
90
59
- public static function validateApiSecret ($ apiSecret )
60
- {
61
- if (!(is_string ($ apiSecret ))) {
62
- throw new InvalidArgumentException ('The apiSecret was not a string: ' . print_r ($ apiSecret , true ));
63
- }
64
- }
65
-
66
91
public static function validateApiUrl ($ apiUrl )
67
92
{
68
93
if (!(is_string ($ apiUrl ) && filter_var ($ apiUrl , FILTER_VALIDATE_URL ))) {
0 commit comments