1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
<?php
/**
* Copyright 2014 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
namespace Facebook\Tests;
use Facebook\FacebookApp;
use Facebook\SignedRequest;
class SignedRequestTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FacebookApp
*/
protected $app;
protected $rawSignature = 'U0_O1MqqNKUt32633zAkdd2Ce-jGVgRgJeRauyx_zC8=';
protected $rawPayload = 'eyJvYXV0aF90b2tlbiI6ImZvb190b2tlbiIsImFsZ29yaXRobSI6IkhNQUMtU0hBMjU2IiwiaXNzdWVkX2F0IjozMjEsImNvZGUiOiJmb29fY29kZSIsInN0YXRlIjoiZm9vX3N0YXRlIiwidXNlcl9pZCI6MTIzLCJmb28iOiJiYXIifQ==';
protected $payloadData = [
'oauth_token' => 'foo_token',
'algorithm' => 'HMAC-SHA256',
'issued_at' => 321,
'code' => 'foo_code',
'state' => 'foo_state',
'user_id' => 123,
'foo' => 'bar',
];
public function setUp()
{
$this->app = new FacebookApp('123', 'foo_app_secret');
}
public function testAValidSignedRequestCanBeCreated()
{
$sr = new SignedRequest($this->app);
$rawSignedRequest = $sr->make($this->payloadData);
$srTwo = new SignedRequest($this->app, $rawSignedRequest);
$payload = $srTwo->getPayload();
$expectedRawSignedRequest = $this->rawSignature . '.' . $this->rawPayload;
$this->assertEquals($expectedRawSignedRequest, $rawSignedRequest);
$this->assertEquals($this->payloadData, $payload);
}
/**
* @expectedException \Facebook\Exceptions\FacebookSDKException
*/
public function testInvalidSignedRequestsWillFailFormattingValidation()
{
new SignedRequest($this->app, 'invalid_signed_request');
}
public function testBase64EncodingIsUrlSafe()
{
$sr = new SignedRequest($this->app);
$encodedData = $sr->base64UrlEncode('aijkoprstADIJKLOPQTUVX1256!)]-:;"<>?.|~');
$this->assertEquals('YWlqa29wcnN0QURJSktMT1BRVFVWWDEyNTYhKV0tOjsiPD4_Lnx-', $encodedData);
}
public function testAUrlSafeBase64EncodedStringCanBeDecoded()
{
$sr = new SignedRequest($this->app);
$decodedData = $sr->base64UrlDecode('YWlqa29wcnN0QURJSktMT1BRVFVWWDEyNTYhKV0tOjsiPD4/Lnx+');
$this->assertEquals('aijkoprstADIJKLOPQTUVX1256!)]-:;"<>?.|~', $decodedData);
}
/**
* @expectedException \Facebook\Exceptions\FacebookSDKException
*/
public function testAnImproperlyEncodedSignatureWillThrowAnException()
{
new SignedRequest($this->app, 'foo_sig.' . $this->rawPayload);
}
/**
* @expectedException \Facebook\Exceptions\FacebookSDKException
*/
public function testAnImproperlyEncodedPayloadWillThrowAnException()
{
new SignedRequest($this->app, $this->rawSignature . '.foo_payload');
}
/**
* @expectedException \Facebook\Exceptions\FacebookSDKException
*/
public function testNonApprovedAlgorithmsWillThrowAnException()
{
$signedRequestData = $this->payloadData;
$signedRequestData['algorithm'] = 'FOO-ALGORITHM';
$sr = new SignedRequest($this->app);
$rawSignedRequest = $sr->make($signedRequestData);
new SignedRequest($this->app, $rawSignedRequest);
}
public function testAsRawSignedRequestCanBeValidatedAndDecoded()
{
$rawSignedRequest = $this->rawSignature . '.' . $this->rawPayload;
$sr = new SignedRequest($this->app, $rawSignedRequest);
$this->assertEquals($this->payloadData, $sr->getPayload());
}
public function testARawSignedRequestCanBeValidatedAndDecoded()
{
$rawSignedRequest = $this->rawSignature . '.' . $this->rawPayload;
$sr = new SignedRequest($this->app, $rawSignedRequest);
$this->assertEquals($sr->getPayload(), $this->payloadData);
$this->assertEquals($sr->getRawSignedRequest(), $rawSignedRequest);
$this->assertEquals(123, $sr->getUserId());
$this->assertTrue($sr->hasOAuthData());
}
}
|