-
Notifications
You must be signed in to change notification settings - Fork 1
/
restful.class.php
198 lines (167 loc) · 4.78 KB
/
restful.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* Description
*
* @class WPSecurePassRestful
* @author =undo= <[email protected]>
* @copyright Copyright (C) 2014 wpXtreme Inc. All Rights Reserved.
* @date 2014-10-15
* @version 1.0.0
*
*/
final class WPSecurePassRestful {
// Default end point api address.
const API_ENDPOINT = 'https://beta.secure-pass.net/';
// Entry and version
const API_VERSION = 'api/v1/';
// Timeout connection request
const CONNECTION_TIMEOUT = 45;
// The User Agent request
const USER_AGENT = 'wpSecurePass/';
/**
* An instance of WPSecurePassOptions class.
*
* @var WPSecurePassOptions $_options
*/
private $_options;
/**
* Return a singleton instance of WPSecurePassRestful class
*
* @return WPSecurePassRestful
*/
public static function init()
{
static $instance = null;
if( is_null( $instance ) ) {
$instance = new self();
}
return $instance;
}
/**
* Create an instance of WPSecurePassRestful class
*
* @return WPSecurePassRestful
*/
public function __construct()
{
// Get options instance
$this->_options = WPSecurePassOptions::init();
}
/**
* Do a request to the wpXtreme Server.
*
* @param string $route Optional. Route. Example `/`
* @param array $raw_body Optional. Params will be convert in jSON
* @param string $verb Optional. Verb of request. Default is 'GET'
*
* @return array|bool
*/
public function request( $route = '', $raw_body = array(), $verb = 'POST' )
{
// Stability
$continue = $this->isRESTFulAvailable();
// Exit if RESTFul is not properly set.
if( empty( $continue ) ) {
return false;
}
// Prepare array for request
$args = array(
'method' => $verb,
'timeout' => self::CONNECTION_TIMEOUT,
'redirection' => 5,
'httpversion' => '1.0',
'user-agent' => self::USER_AGENT . WP_SECUREPASS_VERSION,
'blocking' => true,
'headers' => array(
'X-SecurePass-App-ID' => $this->_options->restfulApplicationID(),
'X-SecurePass-App-Secret' => $this->_options->restfulSecret(),
),
'cookies' => array(),
'body' => $raw_body,
'compress' => false,
'decompress' => false,
'sslverify' => true,
);
// Build the endpoint API
$endpoint = trailingslashit( sprintf( '%s%s', trailingslashit( $this->_options->restfulEndPoint() ) . self::API_VERSION, $route ) );
// Do request
$response = wp_remote_request( $endpoint, $args );
// Dead connection
if( 200 != wp_remote_retrieve_response_code( $response ) ) {
return false;
}
// Get body
$body = wp_remote_retrieve_body( $response );
return json_decode( $body );
}
/**
* Return TRUE if all options RESTFul are setting, otherwise FALSE.
*
* @return bool
*/
public function isRESTFulAvailable()
{
// Stability
$endpoint = $this->_options->restfulEndPoint();
$app_id = $this->_options->restfulApplicationID();
$secret = $this->_options->restfulSecret();
$domain = $this->_options->restfulDomain();
if( empty( $endpoint ) || empty( $app_id ) || empty( $secret ) || empty( $domain ) ) {
return false;
}
return true;
}
// -------------------------------------------------------------------------------------------------------------------
// API (map)
// -------------------------------------------------------------------------------------------------------------------
/**
* Return the 'ping' to API or FALSE on error.
*
* eg:
*
* object(stdClass)#307 (4) {
* ["ip_version"]=> int(4)
* ["ip"]=> string(13) "198.211.98.85"
* ["errorMsg"]=> string(0) ""
* ["rc"]=> int(0)
* }
*
* @return array|bool
*/
public function ping()
{
return $this->request( 'ping' );
}
/**
* Authenticate a given user and return a WP_User if successfully, otherwise FALSE.
*
* @param string $username SercurePass user id
* @param string $password SecurePass OTP + Password
*
* @return bool|WP_User
*/
public function auth( $username, $password )
{
// Prepare result
$user = false;
// Prepare additional arguments with USERNAME@domain
$args = array(
'USERNAME' => sprintf( '%s@%s', $username, $this->_options->restfulDomain() ),
'SECRET' => $password
);
// Authenticate
$result = $this->request( 'users/auth', $args );
if( false === $result ) {
return $user;
}
// Check jSON
if( is_object( $result ) && empty( $result->errorMsg ) ) {
// Check authenticate
if( false !== $result->authenticated ) {
// Get user
$user = get_user_by( 'login', $username );
}
}
return $user;
}
}