5
5
Description: Official <a href="https://www.intercom.io">Intercom</a> support for WordPress.
6
6
Author: Intercom
7
7
Author URI: https://www.intercom.io
8
- Version: 2.6.6
8
+ Version: 3.0.0
9
9
*/
10
10
11
- class IdentityVerificationCalculator
11
+ require_once __DIR__ . '/vendor/autoload.php ' ;
12
+ use Firebase \JWT \JWT ;
13
+
14
+ class TimeProvider
15
+ {
16
+ private static $ mockTime = null ;
17
+
18
+ public static function setMockTime ($ timestamp )
19
+ {
20
+ self ::$ mockTime = $ timestamp ;
21
+ }
22
+
23
+ public static function resetMockTime ()
24
+ {
25
+ self ::$ mockTime = null ;
26
+ }
27
+
28
+ public static function getCurrentTime ()
29
+ {
30
+ return self ::$ mockTime !== null ? self ::$ mockTime : time ();
31
+ }
32
+ }
33
+
34
+ class MessengerSecurityCalculator
12
35
{
13
36
private $ raw_data = array ();
14
37
private $ secret_key = "" ;
@@ -19,39 +42,59 @@ public function __construct($data, $secret_key)
19
42
$ this ->secret_key = $ secret_key ;
20
43
}
21
44
22
- public function identityVerificationComponent ()
45
+ public function messengerSecurityComponent ()
23
46
{
24
47
$ secret_key = $ this ->getSecretKey ();
48
+
25
49
if (empty ($ secret_key ))
26
50
{
27
- return $ this ->emptyIdentityVerificationHashComponent ();
28
- }
29
- if (array_key_exists ("user_id " , $ this ->getRawData ()))
30
- {
31
- return $ this ->identityVerificationHashComponent ("user_id " );
51
+ return $ this ->getRawData ();
32
52
}
33
- if (array_key_exists ("email " , $ this ->getRawData ()))
53
+ if (array_key_exists ("user_id " , $ this -> getRawData ()) || array_key_exists ( " email " , $ this ->getRawData ()))
34
54
{
35
- return $ this ->identityVerificationHashComponent ( " email " );
55
+ return $ this ->messengerSecurityJWTComponent ( );
36
56
}
37
- return $ this ->emptyIdentityVerificationHashComponent ();
38
- }
39
57
40
- private function emptyIdentityVerificationHashComponent ()
41
- {
42
- return array ();
58
+ return $ this ->getRawData ();
43
59
}
44
60
45
- private function identityVerificationHashComponent ( $ key )
61
+ private function messengerSecurityJWTComponent ( )
46
62
{
47
63
$ raw_data = $ this ->getRawData ();
48
- return array ("user_hash " => hash_hmac ("sha256 " , $ raw_data [$ key ], $ this ->getSecretKey ()));
64
+
65
+ $ filtered_data = $ raw_data ;
66
+ $ payload = array ();
67
+
68
+ if (array_key_exists ("email " , $ filtered_data )) {
69
+ unset($ filtered_data ["email " ]);
70
+ $ payload ["user_id " ] = $ raw_data ["email " ];
71
+ $ payload ["email " ] = $ raw_data ["email " ];
72
+ }
73
+ if (array_key_exists ("user_id " , $ filtered_data )) {
74
+ unset($ filtered_data ["user_id " ]);
75
+ $ payload ["user_id " ] = $ raw_data ["user_id " ];
76
+ }
77
+ if (array_key_exists ("name " , $ filtered_data )) {
78
+ unset($ filtered_data ["name " ]);
79
+ $ payload ["name " ] = $ raw_data ["name " ];
80
+ }
81
+
82
+ $ payload = array_merge ($ payload , apply_filters ("intercom_sensitive_attributes " , array ()));
83
+ $ payload ["exp " ] = TimeProvider::getCurrentTime () + 3600 ;
84
+
85
+ $ filtered_data ["intercom_user_jwt " ] = JWT ::encode (
86
+ $ payload , $ this ->getSecretKey (),
87
+ 'HS256 '
88
+ );
89
+
90
+ return $ filtered_data ;
49
91
}
50
92
51
93
private function getSecretKey ()
52
94
{
53
95
return $ this ->secret_key ;
54
96
}
97
+
55
98
private function getRawData ()
56
99
{
57
100
return $ this ->raw_data ;
@@ -357,10 +400,9 @@ public function appId()
357
400
private function getRawData ()
358
401
{
359
402
$ user = new IntercomUser ($ this ->wordpress_user , $ this ->raw_data );
360
- $ settings = apply_filters ("intercom_settings " , $ user ->buildSettings ());
361
- $ identityVerificationCalculator = new IdentityVerificationCalculator ($ settings , $ this ->secret );
362
- $ result = array_merge ($ settings , $ identityVerificationCalculator ->identityVerificationComponent ());
363
- $ result = $ this ->mergeConstants ($ result );
403
+ $ messengerSecurityCalculator = new MessengerSecurityCalculator ($ user ->buildSettings (), $ this ->secret );
404
+ $ settings = $ messengerSecurityCalculator ->messengerSecurityComponent ();
405
+ $ result = $ this ->mergeConstants (apply_filters ("intercom_settings " , $ settings ));
364
406
$ result ['installation_type ' ] = 'wordpress ' ;
365
407
return $ result ;
366
408
}
@@ -385,7 +427,19 @@ private function validateRawData($raw_data)
385
427
}
386
428
387
429
if (getenv ('INTERCOM_PLUGIN_TEST ' ) == '1 ' && !function_exists ('apply_filters ' )) {
388
- function apply_filters ($ _ , $ value ) {
430
+ function apply_filters ($ key , $ value ) {
431
+ if ($ key == "intercom_sensitive_attributes " ) {
432
+ $ extra_data_key = 'INTERCOM_PLUGIN_TEST_JWT_DATA ' ;
433
+ } elseif ($ key == "intercom_settings " ) {
434
+ $ extra_data_key = 'INTERCOM_PLUGIN_TEST_SETTINGS ' ;
435
+ }
436
+
437
+ $ extra_data = getenv ($ extra_data_key );
438
+ if ($ extra_data ) {
439
+ $ extra_data = json_decode ($ extra_data , true );
440
+ return array_merge ($ value , $ extra_data );
441
+ }
442
+
389
443
return $ value ;
390
444
}
391
445
}
@@ -436,6 +490,10 @@ public function buildSettings()
436
490
{
437
491
$ this ->settings ["email " ] = WordPressEscaper::escJS ($ this ->wordpress_user ->user_email );
438
492
}
493
+ if (!empty ($ this ->wordpress_user ->ID ))
494
+ {
495
+ $ this ->settings ["user_id " ] = WordPressEscaper::escJS ($ this ->wordpress_user ->ID );
496
+ }
439
497
if (!empty ($ this ->wordpress_user ->display_name ))
440
498
{
441
499
$ this ->settings ["name " ] = WordPressEscaper::escJS ($ this ->wordpress_user ->display_name );
0 commit comments