|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lifeboat; |
|
4
|
|
|
|
|
5
|
|
|
use Lifeboat\Utils\Curl; |
|
6
|
|
|
use Lifeboat\Utils\URL; |
|
7
|
|
|
use Lifeboat\Utils\Utils; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class App |
|
11
|
|
|
* @package Lifeboat |
|
12
|
|
|
* |
|
13
|
|
|
* @property string $_app_id |
|
14
|
|
|
* @property string $_app_secret |
|
15
|
|
|
* @property string $_app_challenge |
|
16
|
|
|
* @property string $_code |
|
17
|
|
|
*/ |
|
18
|
|
|
class App extends Connector { |
|
19
|
|
|
|
|
20
|
|
|
const CODE_URL = '/oauth/code'; |
|
21
|
|
|
|
|
22
|
|
|
private $_app_id = ''; |
|
23
|
|
|
private $_app_secret = ''; |
|
24
|
|
|
private $_api_challenge = ''; |
|
25
|
|
|
private $_code = ''; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(string $_app_id, string $_app_secret, $_auth_domain = self::AUTH_DOMAIN) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->_app_id = $_app_id; |
|
30
|
|
|
$this->_app_secret = $_app_secret; |
|
31
|
|
|
$this->_auth_domain = rtrim($_auth_domain, '/'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string $challenge |
|
36
|
|
|
* @return $this |
|
37
|
|
|
*/ |
|
38
|
|
|
public function setAPIChallenge(string $challenge): App |
|
39
|
|
|
{ |
|
40
|
|
|
$this->_api_challenge = $challenge; |
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getAPIChallenge(): string |
|
48
|
|
|
{ |
|
49
|
|
|
if (!$this->_api_challenge) $this->_api_challenge = Utils::create_random_string(128); |
|
50
|
|
|
return $this->_api_challenge; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $process_url |
|
55
|
|
|
* @param string $error_url |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getAuthURL(string $process_url, string $error_url): string |
|
59
|
|
|
{ |
|
60
|
|
|
$url = URL::setGetVar('app_id', $this->getAppID(), $this->auth_url(self::CODE_URL)); |
|
61
|
|
|
$url = URL::setGetVar('process_url', urlencode($process_url), $url); |
|
62
|
|
|
$url = URL::setGetVar('error_url', urlencode($error_url), $url); |
|
63
|
|
|
|
|
64
|
|
|
return URL::setGetVar('challenge', Utils::pack($this->getAPIChallenge()), $url); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $secret |
|
69
|
|
|
* @param string $code |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function fetchAccessToken(string $secret, string $code): string |
|
73
|
|
|
{ |
|
74
|
|
|
$curl = new Curl($this->auth_url(self::TOKEN_URL), [ |
|
75
|
|
|
'challenge' => $this->getAPIChallenge(), |
|
76
|
|
|
'code' => $code, |
|
77
|
|
|
'app_secret' => $secret |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
|
|
$curl->setMethod('POST'); |
|
81
|
|
|
$response = $curl->curl(); |
|
82
|
|
|
$json = $response->getJSON(); |
|
83
|
|
|
|
|
84
|
|
|
if (!$response->isValid() || !$json || !array_key_exists('access_token', $json)) { |
|
85
|
|
|
return $json['access_token']; |
|
86
|
|
|
} else { |
|
87
|
|
|
return ''; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getAppID(): string |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->_app_id; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getAccessToken(): string |
|
103
|
|
|
{ |
|
104
|
|
|
if (!$this->_access_token) { |
|
105
|
|
|
$this->_access_token = $this->fetchAccessToken($this->_app_secret, $this->_code); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $this->_access_token; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|