1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lifeboat\SDK; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Promise\Promise; |
6
|
|
|
use Lifeboat\SDK\Exceptions\OAuthException; |
7
|
|
|
use Lifeboat\SDK\Services\Curl; |
8
|
|
|
use Lifeboat\SDK\Utils\URL; |
9
|
|
|
use Lifeboat\SDK\Utils\Utils; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class App |
13
|
|
|
* @package Lifeboat\SDK |
14
|
|
|
*/ |
15
|
|
|
class App { |
16
|
|
|
|
17
|
|
|
const AUTH_DOMAIN = 'https://accounts.lifeboat.app'; |
18
|
|
|
const CODE_URL = '/oauth/code'; |
19
|
|
|
const TOKEN_URL = '/oauth/token'; |
20
|
|
|
const SITES_URL = '/oauth/sites'; |
21
|
|
|
|
22
|
|
|
private $_auth_domain = ''; |
23
|
|
|
private $_app_id = ''; |
24
|
|
|
private $_api_challenge = ''; |
25
|
|
|
|
26
|
|
|
public function __construct(string $_app_id, $_auth_domain = self::AUTH_DOMAIN) |
27
|
|
|
{ |
28
|
|
|
$this->_app_id = $_app_id; |
29
|
|
|
$this->_auth_domain = rtrim($_auth_domain, '/'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $challenge |
34
|
|
|
* @return $this |
35
|
|
|
*/ |
36
|
|
|
public function setAPIChallenge(string $challenge): App |
37
|
|
|
{ |
38
|
|
|
$this->_api_challenge = $challenge; |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getAPIChallenge(): string |
46
|
|
|
{ |
47
|
|
|
if (!$this->_api_challenge) $this->_api_challenge = Utils::create_random_string(128); |
48
|
|
|
return $this->_api_challenge; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $process_url |
53
|
|
|
* @param string $error_url |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getAuthURL(string $process_url, string $error_url): string |
57
|
|
|
{ |
58
|
|
|
$url = URL::setGetVar('app_id', $this->getAppID(), $this->auth_url(self::CODE_URL)); |
59
|
|
|
$url = URL::setGetVar('process_url', urlencode($process_url), $url); |
60
|
|
|
$url = URL::setGetVar('error_url', urlencode($error_url), $url); |
61
|
|
|
|
62
|
|
|
return URL::setGetVar('challenge', Utils::pack($this->getAPIChallenge()), $url); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $secret |
67
|
|
|
* @param string $code |
68
|
|
|
* @param Promise $promise |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function getAccessToken(string $secret, string $code, Promise $promise): App |
72
|
|
|
{ |
73
|
|
|
$curl = new Curl($this->auth_url(self::TOKEN_URL), [ |
74
|
|
|
'challenge' => $this->getAPIChallenge(), |
75
|
|
|
'code' => $code, |
76
|
|
|
'app_secret' => $secret |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$curl->setMethod('POST'); |
80
|
|
|
$response = $curl->curl(); |
81
|
|
|
$json = $response->getJSON(); |
82
|
|
|
|
83
|
|
|
if (!$response->isValid() || !$json || !array_key_exists('access_token', $json)) { |
84
|
|
|
$promise->reject($response->getRaw()); |
85
|
|
|
} else { |
86
|
|
|
$promise->resolve($json['access_token']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $access_token |
94
|
|
|
* @return array |
95
|
|
|
* @throws OAuthException |
96
|
|
|
*/ |
97
|
|
|
public function getSites(string $access_token): array |
98
|
|
|
{ |
99
|
|
|
$curl = new Curl($this->auth_url(self::SITES_URL), [ |
100
|
|
|
'access_token' => $access_token |
101
|
|
|
]); |
102
|
|
|
|
103
|
|
|
$curl->setMethod('POST'); |
104
|
|
|
$response = $curl->curl(); |
105
|
|
|
|
106
|
|
|
if (!$response->isValid()) { |
107
|
|
|
throw new OAuthException($response->getRaw()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $response->getJSON(); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getAppID(): string |
117
|
|
|
{ |
118
|
|
|
return $this->_app_id; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $path |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
private function auth_url(string $path): string |
126
|
|
|
{ |
127
|
|
|
return $this->_auth_domain . '/' . ltrim($path, '/'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|