1717
1818class PHPSDKTestCase extends PHPUnit_Framework_TestCase {
1919 const APP_ID = '117743971608120 ' ;
20- const SECRET = '943716006e74d9b9283d4d5d8ab93204 ' ;
20+ const SECRET = '9c8ea2071859659bea1246d33a9207cf ' ;
2121
2222 const MIGRATED_APP_ID = '174236045938435 ' ;
2323 const MIGRATED_SECRET = '0073dce2d95c4a5c2922d1827ea0cca6 ' ;
2424
25- private static $ kExpiredAccessToken = '206492729383450|2.N4RKywNPuHAey7CK56_wmg__.3600.1304560800.1-214707|6Q14AfpYi_XJB26aRQumouzJiGA ' ;
26- private static $ kValidSignedRequest = '1sxR88U4SW9m6QnSxwCEw_CObqsllXhnpP5j2pxD97c.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyODEwNTI4MDAsIm9hdXRoX3Rva2VuIjoiMTE3NzQzOTcxNjA4MTIwfDIuVlNUUWpub3hYVVNYd1RzcDB1U2g5d19fLjg2NDAwLjEyODEwNTI4MDAtMTY3Nzg0NjM4NXx4NURORHBtcy1nMUM0dUJHQVYzSVdRX2pYV0kuIiwidXNlcl9pZCI6IjE2Nzc4NDYzODUifQ ' ;
27- private static $ kNonTosedSignedRequest = 'c0Ih6vYvauDwncv0n0pndr0hP0mvZaJPQDPt6Z43O0k.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiJ9 ' ;
28- private static $ kSignedRequestWithBogusSignature = '1sxR32U4SW9m6QnSxwCEw_CObqsllXhnpP5j2pxD97c.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImV4cGlyZXMiOjEyODEwNTI4MDAsIm9hdXRoX3Rva2VuIjoiMTE3NzQzOTcxNjA4MTIwfDIuVlNUUWpub3hYVVNYd1RzcDB1U2g5d19fLjg2NDAwLjEyODEwNTI4MDAtMTY3Nzg0NjM4NXx4NURORHBtcy1nMUM0dUJHQVYzSVdRX2pYV0kuIiwidXNlcl9pZCI6IjE2Nzc4NDYzODUifQ ' ;
29- private static $ kSignedRequestWithWrongAlgo = '2--BA2TJLbWV3uBHiB7ztrA4byNm9g0Sz8cv-x9-zi8.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NmEiLCJpc3N1ZWRfYXQiOjEzNDI0ODc0ODJ9 ' ;
25+ const TEST_USER = 499834690 ;
26+
27+ private static $ kExpiredAccessToken = 'AAABrFmeaJjgBAIshbq5ZBqZBICsmveZCZBi6O4w9HSTkFI73VMtmkL9jLuWsZBZC9QMHvJFtSulZAqonZBRIByzGooCZC8DWr0t1M4BL9FARdQwPWPnIqCiFQ ' ;
28+
29+ private static function kValidSignedRequest () {
30+ $ facebook = new FBPublic (array (
31+ 'appId ' => self ::APP_ID ,
32+ 'secret ' => self ::SECRET ,
33+ ));
34+ return $ facebook ->publicMakeSignedRequest (
35+ array (
36+ 'user_id ' => self ::TEST_USER ,
37+ )
38+ );
39+ }
40+
41+ private static function kNonTosedSignedRequest () {
42+ $ facebook = new FBPublic (array (
43+ 'appId ' => self ::APP_ID ,
44+ 'secret ' => self ::SECRET ,
45+ ));
46+ return $ facebook ->publicMakeSignedRequest (array ());
47+ }
48+
49+ private static function kSignedRequestWithBogusSignature () {
50+ $ facebook = new FBPublic (array (
51+ 'appId ' => self ::APP_ID ,
52+ 'secret ' => 'bogus ' ,
53+ ));
54+ return $ facebook ->publicMakeSignedRequest (
55+ array (
56+ 'algorithm ' => 'HMAC-SHA256 ' ,
57+ )
58+ );
59+ }
60+
61+ private static function kSignedRequestWithWrongAlgo () {
62+ $ facebook = new FBPublic (array (
63+ 'appId ' => self ::APP_ID ,
64+ 'secret ' => self ::SECRET ,
65+ ));
66+ $ data ['algorithm ' ] = 'foo ' ;
67+ $ json = json_encode ($ data );
68+ $ b64 = $ facebook ->publicBase64UrlEncode ($ json );
69+ $ raw_sig = hash_hmac ('sha256 ' , $ b64 , self ::SECRET , $ raw = true );
70+ $ sig = $ facebook ->publicBase64UrlEncode ($ raw_sig );
71+ return $ sig .'. ' .$ b64 ;
72+ }
3073
3174 public function testConstructor () {
3275 $ facebook = new TransientFacebook (array (
@@ -283,8 +326,8 @@ public function testGetUserFromSignedRequest() {
283326 'secret ' => self ::SECRET ,
284327 ));
285328
286- $ _REQUEST ['signed_request ' ] = self ::$ kValidSignedRequest ;
287- $ this ->assertEquals ('1677846385 ' , $ facebook ->getUser (),
329+ $ _REQUEST ['signed_request ' ] = self ::kValidSignedRequest () ;
330+ $ this ->assertEquals ('499834690 ' , $ facebook ->getUser (),
288331 'Failed to get user ID from a valid signed request. ' );
289332 }
290333
@@ -295,9 +338,9 @@ public function testGetSignedRequestFromCookie() {
295338 ));
296339
297340 $ _COOKIE [$ facebook ->publicGetSignedRequestCookieName ()] =
298- self ::$ kValidSignedRequest ;
341+ self ::kValidSignedRequest () ;
299342 $ this ->assertNotNull ($ facebook ->publicGetSignedRequest ());
300- $ this ->assertEquals ('1677846385 ' , $ facebook ->getUser (),
343+ $ this ->assertEquals ('499834690 ' , $ facebook ->getUser (),
301344 'Failed to get user ID from a valid signed request. ' );
302345 }
303346
@@ -308,7 +351,7 @@ public function testGetSignedRequestWithIncorrectSignature() {
308351 ));
309352
310353 $ _COOKIE [$ facebook ->publicGetSignedRequestCookieName ()] =
311- self ::$ kSignedRequestWithBogusSignature ;
354+ self ::kSignedRequestWithBogusSignature () ;
312355 $ this ->assertNull ($ facebook ->publicGetSignedRequest ());
313356 }
314357
@@ -453,26 +496,6 @@ public function testGraphAPIWithExpiredAccessToken() {
453496 }
454497 }
455498
456- public function testGraphAPIMethod () {
457- $ facebook = new TransientFacebook (array (
458- 'appId ' => self ::APP_ID ,
459- 'secret ' => self ::SECRET ,
460- ));
461-
462- try {
463- // naitik being bold about deleting his entire record....
464- // let's hope this never actually passes.
465- $ response = $ facebook ->api ('/naitik ' , $ method = 'DELETE ' );
466- $ this ->fail ('Should not get here. ' );
467- } catch (FacebookApiException $ e ) {
468- // ProfileDelete means the server understood the DELETE
469- $ msg =
470- 'OAuthException: (#200) User cannot access this application ' ;
471- $ this ->assertEquals ($ msg , (string ) $ e ,
472- 'Expect the invalid session message. ' );
473- }
474- }
475-
476499 public function testGraphAPIOAuthSpecError () {
477500 $ facebook = new TransientFacebook (array (
478501 'appId ' => self ::MIGRATED_APP_ID ,
@@ -722,26 +745,6 @@ public function testSecureCurrentUrlWithNonDefaultPort() {
722745 'Expect the current url to exist. ' );
723746 }
724747
725- public function testAppSecretCall () {
726- $ facebook = new TransientFacebook (array (
727- 'appId ' => self ::APP_ID ,
728- 'secret ' => self ::SECRET ,
729- ));
730-
731- try {
732- $ response = $ facebook ->api ('/ ' . self ::APP_ID . '/insights ' );
733- $ this ->fail ('Desktop applications need a user token for insights. ' );
734- } catch (FacebookApiException $ e ) {
735- // this test is failing as the graph call is returning the wrong
736- // error message
737- $ this ->assertEquals ($ e ->getMessage (),
738- 'An access token is required to request this resource. ' );
739- } catch (Exception $ e ) {
740- $ this ->fail ('Incorrect exception type thrown when trying to gain ' .
741- 'insights for desktop app without a user access token. ' );
742- }
743- }
744-
745748 public function testBase64UrlEncode () {
746749 $ input = 'Facebook rocks ' ;
747750 $ output = 'RmFjZWJvb2sgcm9ja3M ' ;
@@ -754,10 +757,10 @@ public function testSignedToken() {
754757 'appId ' => self ::APP_ID ,
755758 'secret ' => self ::SECRET
756759 ));
757- $ payload = $ facebook ->publicParseSignedRequest (self ::$ kValidSignedRequest );
760+ $ payload = $ facebook ->publicParseSignedRequest (self ::kValidSignedRequest () );
758761 $ this ->assertNotNull ($ payload , 'Expected token to parse ' );
759762 $ this ->assertEquals ($ facebook ->getSignedRequest (), null );
760- $ _REQUEST ['signed_request ' ] = self ::$ kValidSignedRequest ;
763+ $ _REQUEST ['signed_request ' ] = self ::kValidSignedRequest () ;
761764 $ this ->assertEquals ($ facebook ->getSignedRequest (), $ payload );
762765 }
763766
@@ -767,12 +770,12 @@ public function testNonTossedSignedtoken() {
767770 'secret ' => self ::SECRET
768771 ));
769772 $ payload = $ facebook ->publicParseSignedRequest (
770- self ::$ kNonTosedSignedRequest );
773+ self ::kNonTosedSignedRequest () );
771774 $ this ->assertNotNull ($ payload , 'Expected token to parse ' );
772775 $ this ->assertNull ($ facebook ->getSignedRequest ());
773- $ _REQUEST ['signed_request ' ] = self ::$ kNonTosedSignedRequest ;
774- $ this -> assertEquals ( $ facebook ->getSignedRequest (),
775- array ( 'algorithm ' => ' HMAC-SHA256 ' ));
776+ $ _REQUEST ['signed_request ' ] = self ::kNonTosedSignedRequest () ;
777+ $ sr = $ facebook ->getSignedRequest ();
778+ $ this -> assertTrue ( isset ( $ sr [ 'algorithm ' ] ));
776779 }
777780
778781 public function testSignedRequestWithWrongAlgo () {
@@ -781,7 +784,7 @@ public function testSignedRequestWithWrongAlgo() {
781784 'secret ' => self ::SECRET
782785 ));
783786 $ payload = $ fb ->publicParseSignedRequest (
784- self ::$ kSignedRequestWithWrongAlgo );
787+ self ::kSignedRequestWithWrongAlgo () );
785788 $ this ->assertNull ($ payload , 'Expected nothing back. ' );
786789 }
787790
@@ -868,13 +871,13 @@ public function testGetUserAndAccessTokenFromSignedRequestNotSession() {
868871 'secret ' => self ::SECRET
869872 ));
870873
871- $ _REQUEST ['signed_request ' ] = self ::$ kValidSignedRequest ;
874+ $ _REQUEST ['signed_request ' ] = self ::kValidSignedRequest () ;
872875 $ facebook ->publicSetPersistentData ('user_id ' , 41572 );
873876 $ facebook ->publicSetPersistentData ('access_token ' ,
874877 self ::$ kExpiredAccessToken );
875878 $ this ->assertNotEquals ('41572 ' , $ facebook ->getUser (),
876879 'Got user from session instead of signed request. ' );
877- $ this ->assertEquals ('1677846385 ' , $ facebook ->getUser (),
880+ $ this ->assertEquals ('499834690 ' , $ facebook ->getUser (),
878881 'Failed to get correct user ID from signed request. ' );
879882 $ this ->assertNotEquals (
880883 self ::$ kExpiredAccessToken ,
@@ -1849,6 +1852,9 @@ class FBPublic extends TransientFacebook {
18491852 public static function publicBase64UrlDecode ($ input ) {
18501853 return self ::base64UrlDecode ($ input );
18511854 }
1855+ public static function publicBase64UrlEncode ($ input ) {
1856+ return self ::base64UrlEncode ($ input );
1857+ }
18521858 public function publicParseSignedRequest ($ input ) {
18531859 return $ this ->parseSignedRequest ($ input );
18541860 }
0 commit comments