1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the PHP SDK library for the Superdesk Content API. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú. |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Superdesk\ContentApiSdk\API\Authentication; |
16
|
|
|
|
17
|
|
|
use Superdesk\ContentApiSdk\API\Request\RequestInterface; |
18
|
|
|
use Superdesk\ContentApiSdk\ContentApiSdk; |
19
|
|
|
use Superdesk\ContentApiSdk\Exception\AuthenticationException; |
20
|
|
|
use Superdesk\ContentApiSdk\Exception\ClientException; |
21
|
|
|
use Superdesk\ContentApiSdk\Exception\InvalidDataException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* OAuth password authentication class. |
25
|
|
|
*/ |
26
|
|
|
class OAuthPasswordAuthentication extends AbstractAuthentication |
27
|
|
|
{ |
28
|
|
|
const AUTHENTICATION_GRANT_TYPE = 'password'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Username for OAuth password authentication. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $username; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Password for OAuth password authentication. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $password; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Gets the value of username. |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getUsername() |
50
|
|
|
{ |
51
|
|
|
return $this->username; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sets the value of username. |
56
|
|
|
* |
57
|
|
|
* @param string $username Value to set |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
|
|
public function setUsername($username) |
62
|
|
|
{ |
63
|
|
|
$this->username = $username; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the value of password. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getPassword() |
74
|
|
|
{ |
75
|
|
|
return $this->password; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets the value of password. |
80
|
|
|
* |
81
|
|
|
* @param string $password Value to set |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
|
|
public function setPassword($password) |
86
|
|
|
{ |
87
|
|
|
$this->password = $password; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function refreshAccessToken() |
96
|
|
|
{ |
97
|
|
|
return $this->makeTokenCall(array( |
98
|
|
|
'client_id' => $this->getClientId(), |
99
|
|
|
'grant_type' => self::REFRESH_GRANT_TYPE, |
100
|
|
|
'username' => $this->getUsername(), |
101
|
|
|
'refresh_token' => $this->refreshToken |
102
|
|
|
)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
|
|
public function getAuthenticationTokens() |
109
|
|
|
{ |
110
|
|
|
return $this->makeTokenCall(array( |
111
|
|
|
'client_id' => $this->getClientId(), |
112
|
|
|
'grant_type' => self::AUTHENTICATION_GRANT_TYPE, |
113
|
|
|
'username' => $this->getUsername(), |
114
|
|
|
'password' => $this->getPassword() |
115
|
|
|
)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Makes an authentication call to the server. |
120
|
|
|
* |
121
|
|
|
* @param array $parameters Array with parameters for the request |
122
|
|
|
* |
123
|
|
|
* @return boolean |
124
|
|
|
* |
125
|
|
|
* @throws AuthenticationException Thrown on failures during request and |
126
|
|
|
* invalid responses. |
127
|
|
|
*/ |
128
|
|
|
private function makeTokenCall(array $parameters) |
129
|
|
|
{ |
130
|
|
|
try { |
131
|
|
|
$response = $this->client->makeCall( |
132
|
|
|
$this->getAuthenticationUrl(), |
133
|
|
|
array(), |
134
|
|
|
array(), |
135
|
|
|
'POST', |
136
|
|
|
$parameters |
137
|
|
|
); |
138
|
|
|
} catch (ClientException $e) { |
139
|
|
|
throw new AuthenticationException('Could not request access token.', $e->getCode(), $e); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($response['status'] === 200) { |
143
|
|
|
try { |
144
|
|
|
$responseObj = ContentApiSdk::getValidJsonObj($response['body']); |
145
|
|
|
} catch (InvalidDataException $e) { |
146
|
|
|
throw new AuthenticationException('Authentication response body is not (valid) json.', $e->getCode(), $e); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if (property_exists($responseObj, 'access_token') && property_exists($responseObj, 'refresh_token')) { |
150
|
|
|
$this->accessToken = $responseObj->access_token; |
151
|
|
|
$this->refreshToken = $responseObj->refresh_token; |
152
|
|
|
|
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
throw new AuthenticationException('The server returned an unexpected response body.'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
throw new AuthenticationException(sprintf('The server returned an error with status %s.', $response['status']), $response['status']); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|