1313use OpenTok \Exception \InvalidArgumentException ;
1414use JohnStevenson \JsonWorks \Document ;
1515use JohnStevenson \JsonWorks \Utils as JsonUtils ;
16+ use RuntimeException ;
1617
1718/**
1819* @internal
@@ -25,31 +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 ;
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 ;
3447 }
48+
49+ // Mixed formats or invalid formats - throw an exception
50+ throw new InvalidArgumentException ("Invalid Vonage Keypair credentials provided. " );
3551 }
3652
37- public static function validateVonageJwtArguments ( array $ options )
53+ private static function isValidPrivateKey ( string $ filePath ): bool
3854 {
39- if (!isset ( $ data [ ' application_id ' ]) && ! isset ( $ data [ ' private_key_path ' ] )) {
40- return ;
55+ if (!file_exists ( $ filePath ) || ! is_readable ( $ filePath )) {
56+ throw new InvalidArgumentException ( " Private key file does not exist or is not readable. " ) ;
4157 }
4258
43- if ( isset ( $ data [ ' application_id ' ]) && isset ( $ data [ ' private_key_path ' ])) {
44- if ( is_string ( $ data [ ' application_id ' ]) && is_string ( $ data [ ' private_key_path ' ])) {
45- return ;
46- } ;
59+ $ keyContents = file_get_contents ( $ filePath );
60+
61+ if ( $ keyContents === false ) {
62+ throw new RuntimeException ( " Failed to read private key file. " ) ;
4763 }
4864
49- // If one key is present but not the other, validation fails
50- throw new InvalidArgumentException (
51- 'You are attempting to use the Vonage Video API. Both application_id and private key paths must be in options and both strings. '
52- );
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 ));
5367 }
5468
5569 public static function validateForceMuteAllOptions (array $ options )
@@ -74,13 +88,6 @@ public static function validateForceMuteAllOptions(array $options)
7488 }
7589 }
7690
77- public static function validateApiSecret ($ apiSecret )
78- {
79- if (!(is_string ($ apiSecret ))) {
80- throw new InvalidArgumentException ('The apiSecret was not a string: ' . print_r ($ apiSecret , true ));
81- }
82- }
83-
8491 public static function validateApiUrl ($ apiUrl )
8592 {
8693 if (!(is_string ($ apiUrl ) && filter_var ($ apiUrl , FILTER_VALIDATE_URL ))) {
0 commit comments