1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Inktale\Api; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Inktale\Api\Exceptions\InktaleApiException; |
8
|
|
|
use Inktale\Api\Exceptions\InktaleOAuthException; |
9
|
|
|
use Inktale\Api\Interfaces\OAuthStateStorageInterface; |
10
|
|
|
|
11
|
|
|
class InktaleOAuth |
12
|
|
|
{ |
13
|
|
|
/** @var OAuthStateStorageInterface */ |
14
|
|
|
private $stateStorage; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $clientId; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $clientSecret; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $redirectUri; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var InktaleOAuthApiClient |
33
|
|
|
*/ |
34
|
|
|
private $apiClient; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* InktaleOAuth constructor. |
38
|
|
|
* @param OAuthStateStorageInterface $stateStorage |
39
|
|
|
* @param InktaleOAuthApiClient $apiClient |
40
|
|
|
* @param string $clientId Inktale app id |
41
|
|
|
* @param string $clientSecret App's client secret |
42
|
|
|
* @param string $redirectUri Redirect after permissions granted |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
OAuthStateStorageInterface $stateStorage, |
46
|
|
|
InktaleOAuthApiClient $apiClient, |
47
|
|
|
$clientId, |
48
|
|
|
$clientSecret, |
49
|
|
|
$redirectUri |
50
|
|
|
) { |
51
|
|
|
$this->stateStorage = $stateStorage; |
52
|
|
|
$this->clientId = $clientId; |
53
|
|
|
$this->clientSecret = $clientSecret; |
54
|
|
|
$this->redirectUri = $redirectUri; |
55
|
|
|
$this->apiClient = $apiClient; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Retrieve a URL where user should be redirected to authorize app |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function getAuthUrl() |
64
|
|
|
{ |
65
|
|
|
$state = uniqid('oauthState', true); |
66
|
|
|
$this->stateStorage->put($state); |
67
|
|
|
|
68
|
|
|
$query = http_build_query([ |
69
|
|
|
'state' => $state, |
70
|
|
|
'client_id' => $this->clientId, |
71
|
|
|
'redirect_uri' => $this->redirectUri, |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
return $this->apiClient->url . '?' . $query; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $code |
79
|
|
|
* @param $state |
80
|
|
|
* @throws InktaleOAuthException |
81
|
|
|
*/ |
82
|
|
|
public function getApiKey($code, $state) |
83
|
|
|
{ |
84
|
|
|
$storedState = $this->stateStorage->get(); |
85
|
|
|
$this->stateStorage->forget(); |
86
|
|
|
|
87
|
|
|
if (!$state || $storedState !== $state) { |
88
|
|
|
throw new InktaleOAuthException('State is not valid'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!$code) { |
92
|
|
|
throw new InktaleOAuthException('Code is missing'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$response = $this->apiClient->post('access-token', [ |
97
|
|
|
'client_id' => $this->clientId, |
98
|
|
|
'client_secret' => $this->clientSecret, |
99
|
|
|
'code' => $code, |
100
|
|
|
'redirect_uri' => $this->redirectUri, |
101
|
|
|
]); |
102
|
|
|
} catch (InktaleApiException $e) { |
103
|
|
|
$exception = new InktaleOAuthException; |
104
|
|
|
|
105
|
|
|
$exception->setErrorDetails($e->getErrorDetails()); |
106
|
|
|
$exception->setErrorTitle($e->getErrorTitle()); |
107
|
|
|
$exception->setRawResponse($e->getRawResponse()); |
108
|
|
|
|
109
|
|
|
throw $exception; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $response['access_token']; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|