Passed
Pull Request — master (#888)
by Tobias
01:50
created

SignedRequestFromInputHelper   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
A instantiateSignedRequest() 0 9 3
A __construct() 0 6 1
B getAccessToken() 0 16 5
A getRawSignedRequestFromPost() 0 7 2
A getRawSignedRequestFromCookie() 0 7 2
A getSignedRequest() 0 3 1
A getUserId() 0 3 2
1
<?php
2
/**
3
 * Copyright 2017 Facebook, Inc.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
namespace Facebook\Helper;
25
26
use Facebook\Facebook;
27
use Facebook\Application;
28
use Facebook\Client;
29
use Facebook\SignedRequest;
30
use Facebook\Authentication\AccessToken;
31
use Facebook\Authentication\OAuth2Client;
32
33
/**
34
 *
35
 * @package Facebook
36
 */
37
abstract class SignedRequestFromInputHelper
38
{
39
    /**
40
     * @var SignedRequest|null The SignedRequest entity.
41
     */
42
    protected $signedRequest;
43
44
    /**
45
     * @var Application The FacebookApp entity.
46
     */
47
    protected $app;
48
49
    /**
50
     * @var OAuth2Client The OAuth 2.0 client service.
51
     */
52
    protected $oAuth2Client;
53
54
    /**
55
     * Initialize the helper and process available signed request data.
56
     *
57
     * @param Application    $app          The FacebookApp entity.
58
     * @param Client $client       The client to make HTTP requests.
59
     * @param string         $graphVersion The version of Graph to use.
60
     */
61
    public function __construct(Application $app, Client $client, $graphVersion)
62
    {
63
        $this->app = $app;
64
        $this->oAuth2Client = new OAuth2Client($this->app, $client, $graphVersion);
65
66
        $this->instantiateSignedRequest();
67
    }
68
69
    /**
70
     * Instantiates a new SignedRequest entity.
71
     *
72
     * @param string|null
73
     */
74
    public function instantiateSignedRequest($rawSignedRequest = null)
75
    {
76
        $rawSignedRequest = $rawSignedRequest ?: $this->getRawSignedRequest();
77
78
        if (!$rawSignedRequest) {
79
            return;
80
        }
81
82
        $this->signedRequest = new SignedRequest($this->app, $rawSignedRequest);
83
    }
84
85
    /**
86
     * Returns an AccessToken entity from the signed request.
87
     *
88
     * @return AccessToken|null
89
     *
90
     * @throws \Facebook\Exception\SDKException
91
     */
92
    public function getAccessToken()
93
    {
94
        if ($this->signedRequest && $this->signedRequest->hasOAuthData()) {
95
            $code = $this->signedRequest->get('code');
96
            $accessToken = $this->signedRequest->get('oauth_token');
97
98
            if ($code && !$accessToken) {
99
                return $this->oAuth2Client->getAccessTokenFromCode($code);
100
            }
101
102
            $expiresAt = $this->signedRequest->get('expires', 0);
103
104
            return new AccessToken($accessToken, $expiresAt);
105
        }
106
107
        return null;
108
    }
109
110
    /**
111
     * Returns the SignedRequest entity.
112
     *
113
     * @return SignedRequest|null
114
     */
115
    public function getSignedRequest()
116
    {
117
        return $this->signedRequest;
118
    }
119
120
    /**
121
     * Returns the user_id if available.
122
     *
123
     * @return string|null
124
     */
125
    public function getUserId()
126
    {
127
        return $this->signedRequest ? $this->signedRequest->getUserId() : null;
128
    }
129
130
    /**
131
     * Get raw signed request from input.
132
     *
133
     * @return string|null
134
     */
135
    abstract public function getRawSignedRequest();
136
137
    /**
138
     * Get raw signed request from POST input.
139
     *
140
     * @return string|null
141
     */
142
    public function getRawSignedRequestFromPost()
143
    {
144
        if (isset($_POST['signed_request'])) {
145
            return $_POST['signed_request'];
146
        }
147
148
        return null;
149
    }
150
151
    /**
152
     * Get raw signed request from cookie set from the Javascript SDK.
153
     *
154
     * @return string|null
155
     */
156
    public function getRawSignedRequestFromCookie()
157
    {
158
        if (isset($_COOKIE['fbsr_' . $this->app->getId()])) {
159
            return $_COOKIE['fbsr_' . $this->app->getId()];
160
        }
161
162
        return null;
163
    }
164
}
165