|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* A php library for using the Authy API. |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/ck-developer/authy |
|
7
|
|
|
* @package authy |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
* @copyright Copyright (c) 2016 Claude Khedhiri <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Authy; |
|
13
|
|
|
|
|
14
|
|
|
use Authy\Message\AbstractRequest; |
|
15
|
|
|
use Authy\Message\AbstractResponse; |
|
16
|
|
|
use GuzzleHttp\Client; |
|
17
|
|
|
|
|
18
|
|
|
class Authy |
|
19
|
|
|
{ |
|
20
|
|
|
protected $liveEndpoint = 'https://api.authy.com/protected/json/'; |
|
21
|
|
|
protected $testEndpoint = 'https://sandbox-api.authy.com/protected/json/'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Client |
|
25
|
|
|
*/ |
|
26
|
|
|
private $httpClient; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $parameters; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Authy constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $parameters |
|
37
|
|
|
* @param Client|null $httpClient |
|
38
|
|
|
*/ |
|
39
|
7 |
|
public function __construct(array $parameters = array(), Client $httpClient = null) |
|
40
|
|
|
{ |
|
41
|
7 |
|
$this->initialize($parameters); |
|
42
|
7 |
|
$this->httpClient = $httpClient; |
|
43
|
7 |
|
} |
|
44
|
|
|
|
|
45
|
7 |
|
public function initialize(array $parameters = array()) |
|
46
|
|
|
{ |
|
47
|
7 |
|
$this->parameters = array_merge(array( |
|
48
|
7 |
|
'apiKey' => '', |
|
49
|
|
|
'sandbox' => true, |
|
50
|
|
|
), $parameters); |
|
51
|
7 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @throws \Exception |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
4 |
|
public function getApiKey() |
|
59
|
|
|
{ |
|
60
|
4 |
|
return $this->parameters['apiKey']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $apiKey |
|
65
|
|
|
* |
|
66
|
|
|
* @return $this |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function setApiKey($apiKey) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$this->parameters['apiKey'] = $apiKey; |
|
71
|
|
|
|
|
72
|
1 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
5 |
|
public function isSandbox() |
|
79
|
|
|
{ |
|
80
|
5 |
|
return $this->parameters['sandbox']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param bool $sandbox |
|
85
|
|
|
* |
|
86
|
|
|
* @return $this |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public function setSandbox($sandbox) |
|
89
|
|
|
{ |
|
90
|
2 |
|
$this->parameters['sandbox'] = $sandbox; |
|
91
|
2 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function getEndpoint() |
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->isSandbox() ? $this->testEndpoint : $this->liveEndpoint; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param $httpClient |
|
104
|
|
|
* |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function setHttpClient($httpClient) |
|
108
|
|
|
{ |
|
109
|
2 |
|
$this->httpClient = $httpClient; |
|
110
|
|
|
|
|
111
|
2 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
protected function getDefaultHttpClient() |
|
115
|
|
|
{ |
|
116
|
1 |
|
return new Client(array( |
|
117
|
1 |
|
'base_uri' => $this->getEndpoint(), |
|
118
|
1 |
|
'query' => array('api_key' => $this->getApiKey()), |
|
119
|
1 |
|
'timeout' => 2.0, |
|
120
|
|
|
)); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return Client |
|
125
|
|
|
*/ |
|
126
|
3 |
|
public function getHttpClient() |
|
127
|
|
|
{ |
|
128
|
3 |
|
if ($this->httpClient) { |
|
129
|
2 |
|
return $this->httpClient; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
return $this->httpClient = $this->getDefaultHttpClient(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $class |
|
137
|
|
|
* |
|
138
|
|
|
* @return AbstractResponse |
|
139
|
|
|
*/ |
|
140
|
2 |
|
private function createRequest($class) |
|
141
|
|
|
{ |
|
142
|
|
|
/** @var AbstractRequest $request */ |
|
143
|
2 |
|
$request = new $class($this->getHttpClient()); |
|
144
|
|
|
|
|
145
|
2 |
|
return $request->send(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return \Authy\Message\AuthyDetailsResponse |
|
150
|
|
|
*/ |
|
151
|
1 |
|
public function details() |
|
152
|
|
|
{ |
|
153
|
1 |
|
return $this->createRequest('\Authy\Message\AuthyDetailsRequest'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return \Authy\Message\AuthyStatsResponse |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function stats() |
|
160
|
|
|
{ |
|
161
|
1 |
|
return $this->createRequest('\Authy\Message\AuthyStatsRequest'); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|