|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WrapIt\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use WrapIt\WrapIt; |
|
6
|
|
|
use WrapIt\Exceptions\WrapItParameterException; |
|
7
|
|
|
use WrapIt\Exceptions\WrapItResponseException; |
|
8
|
|
|
use WrapIt\Http\WrapItApiRequester; |
|
9
|
|
|
use WrapIt\Helpers\Helper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class WrapItLoginHelper |
|
13
|
|
|
* |
|
14
|
|
|
* @package WrapIt |
|
15
|
|
|
*/ |
|
16
|
|
|
class WrapItLoginHelper extends Helper { |
|
17
|
|
|
|
|
18
|
12 |
|
public function __construct($wi) { |
|
19
|
12 |
|
parent::__construct($wi); |
|
20
|
12 |
|
} |
|
21
|
|
|
|
|
22
|
6 |
|
public function generateLoginUrl($opt) { |
|
23
|
6 |
|
$opt = array_merge(array( |
|
24
|
6 |
|
"redirect_uri" => null, |
|
25
|
3 |
|
"scope" => array("profile"), |
|
26
|
3 |
|
"response_type" => "code", |
|
27
|
|
|
"state" => null |
|
28
|
6 |
|
), $opt); |
|
29
|
|
|
|
|
30
|
6 |
|
if ($opt["redirect_uri"] == null) { |
|
|
|
|
|
|
31
|
|
|
throw new WrapItParameterException("redirect_uri is missing"); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$parameters = array( |
|
35
|
6 |
|
"client_id" => $this->client_id, |
|
36
|
6 |
|
"redirect_uri" => $opt["redirect_uri"], |
|
37
|
6 |
|
"scope" => implode(" ", $opt["scope"]), |
|
38
|
6 |
|
"response_type" => $opt["response_type"] |
|
39
|
3 |
|
); |
|
40
|
|
|
|
|
41
|
6 |
|
if ($opt["state"] != null) { |
|
42
|
|
|
$parameters["state"] = $opt["state"]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
6 |
|
return "https://" . $this->api_requester->getDomain() . "/auth?" . http_build_query($parameters); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
6 |
|
public function exchangeAccessToken($opt) { |
|
49
|
6 |
|
$opt = array_merge(array( |
|
50
|
6 |
|
"redirect_uri" => null, |
|
51
|
3 |
|
"code" => null, |
|
52
|
|
|
"expire" => null |
|
53
|
6 |
|
), $opt); |
|
54
|
|
|
|
|
55
|
6 |
|
if ($opt["redirect_uri"] == null) { |
|
56
|
|
|
throw new WrapItParameterException("redirect_uri is missing"); |
|
57
|
|
|
} |
|
58
|
6 |
|
if ($opt["code"] == null) { |
|
59
|
|
|
throw new WrapItParameterException("code is missing"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$post = array( |
|
63
|
6 |
|
"client_id" => $this->client_id, |
|
64
|
6 |
|
"client_secret" => $this->client_secret, |
|
65
|
6 |
|
"grant_type" => "client_credentials", |
|
66
|
6 |
|
"redirect_uri" => $opt["redirect_uri"], |
|
67
|
6 |
|
"code" => $opt["code"] |
|
68
|
3 |
|
); |
|
69
|
|
|
|
|
70
|
6 |
|
if ($opt["expire"] != null) { |
|
71
|
|
|
$post["expire"] = $opt["expire"]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
6 |
|
$data = $this->api_requester->post("access_token", $post); |
|
75
|
|
|
|
|
76
|
6 |
|
if (!isset($data["error"]) && isset($data["access_token"])) { |
|
77
|
|
|
return $data["access_token"]; |
|
78
|
6 |
|
} else if (isset($data["error"])) { |
|
79
|
6 |
|
throw new WrapItResponseException($data["error"]["message"]); |
|
80
|
|
|
} else { |
|
81
|
|
|
throw new WrapItResponseException("Unknown error"); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|