1313use OpenTok \Exception \InvalidArgumentException ;
1414use JohnStevenson \JsonWorks \Document ;
1515use JohnStevenson \JsonWorks \Utils as JsonUtils ;
16+ use RuntimeException ;
1617
1718/**
1819* @internal
@@ -25,13 +26,44 @@ class Validators
2526
2627 public const STREAM_MODES = ['auto ' , 'manual ' ];
2728
28- public static function validateApiKey ($ apiKey)
29+ public static function isVonageKeypair ($ apiKey, $ apiSecret ): bool
2930 {
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 ;
3440 }
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 ));
3567 }
3668
3769 public static function validateForceMuteAllOptions (array $ options )
@@ -56,13 +88,6 @@ public static function validateForceMuteAllOptions(array $options)
5688 }
5789 }
5890
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-
6691 public static function validateApiUrl ($ apiUrl )
6792 {
6893 if (!(is_string ($ apiUrl ) && filter_var ($ apiUrl , FILTER_VALIDATE_URL ))) {
0 commit comments