-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
235 lines (185 loc) · 6.84 KB
/
functions.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//------------------ append to previous data in functions.php---------------------
include "php-jwt.php";
define('JWT_SECRET_KEY', 'TEST');
define('RATE_LIMIT', 10); // 100 requests per hour
define('TIME_FRAME', 30); // Time frame in seconds (1 hour)
define('PRE_SHARED_KEY', 'adl'); // Change to your pre-shared key
function rate_limit_check() {
$ip = $_SERVER['REMOTE_ADDR'];
$request_count = get_transient("rate_limit_{$ip}_count");
$first_request_time = get_transient("rate_limit_{$ip}_time");
if ($request_count === false) {
set_transient("rate_limit_{$ip}_count", 1, TIME_FRAME);
set_transient("rate_limit_{$ip}_time", time(), TIME_FRAME);
} else {
if ($request_count >= RATE_LIMIT) {
// Too many requests
$wait_time = TIME_FRAME - (time() - $first_request_time);
return new WP_Error('rate_limit_exceeded', 'Rate limit exceeded. Try again in ' . gmdate("i:s", $wait_time) . ' minutes.', array('status' => 429));
} else {
// Increment the request count
set_transient("rate_limit_{$ip}_count", $request_count + 1, TIME_FRAME);
}
}
return true;
}
// Logging Function
function log_api_request($endpoint) {
$ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$timestamp = date("Y-m-d H:i:s");
$log_entry = "{$timestamp} - {$ip} accessed {$endpoint} using {$user_agent}\n";
// Save the log entry to a file or database
file_put_contents(__DIR__ . '/api_requests.log', $log_entry, FILE_APPEND);
}
function check_preshared_key() {
$provided_key = isset($_SERVER['HTTP_X_PRESHARED_KEY']) ? $_SERVER['HTTP_X_PRESHARED_KEY'] : '';
if ($provided_key !== PRE_SHARED_KEY) {
return new WP_Error('unauthorized_access', 'Unauthorized access. Invalid pre-shared key.', array('status' => 403));
}
return true;
}
// Generate JWT Token
function generate_jwt_token($user_id) {
$issuedAt = time();
$expirationTime = $issuedAt + (60 * 60); // jwt valid for 1 hour
$payload = array(
'iat' => $issuedAt,
'exp' => $expirationTime,
'data' => array(
'user_id' => $user_id
)
);
return JWT::encode($payload, JWT_SECRET_KEY);
}
function validate_jwt_token($token) {
try {
$decoded = JWT::decode($token, JWT_SECRET_KEY);
return $decoded['data']['user_id'];
} catch (Exception $e) {
return null;
}
}
// Register REST API Endpoints
add_action('rest_api_init', function () {
register_rest_route('custom/v1', '/login', array(
'methods' => 'POST',
'callback' => 'custom_login',
));
register_rest_route('custom/v1', '/register', array(
'methods' => 'POST',
'callback' => 'custom_register',
));
});
// Login Callback
function custom_login($request) {
// Rate Limit Check
$rate_limit_check = rate_limit_check();
if (is_wp_error($rate_limit_check)) {
return $rate_limit_check;
}
// Log the request
log_api_request('/login');
// Pre-shared Key Check
$preshared_key_check = check_preshared_key();
if (is_wp_error($preshared_key_check)) {
return $preshared_key_check;
}
$username = sanitize_text_field($request['username']);
$password = sanitize_text_field($request['password']);
$user = wp_authenticate($username, $password);
if (is_wp_error($user)) {
return new WP_Error('invalid_credentials', 'Invalid username or password', array('status' => 403));
}
$token = generate_jwt_token($user->ID);
return array(
'token' => $token,
'user_id' => $user->ID,
'username' => $user->user_login,
'email' => $user->user_email
);
}
// Register Callback
function custom_register($request) {
// Rate Limit Check
$rate_limit_check = rate_limit_check();
if (is_wp_error($rate_limit_check)) {
return $rate_limit_check;
}
// Log the request
log_api_request('/register');
// Pre-shared Key Check
$preshared_key_check = check_preshared_key();
if (is_wp_error($preshared_key_check)) {
return $preshared_key_check;
}
$username = sanitize_text_field($request['username']);
$password = sanitize_text_field($request['password']);
$email = sanitize_email($request['email']);
if (username_exists($username) || email_exists($email)) {
return new WP_Error('user_exists', 'Username or Email already exists', array('status' => 400));
}
$user_id = wp_create_user($username, $password, $email);
if (is_wp_error($user_id)) {
return new WP_Error('registration_failed', 'User registration failed', array('status' => 500));
}
// Optionally, send a welcome email to the user
wp_new_user_notification($user_id, null, 'user');
return array(
'user_id' => $user_id,
'username' => $username,
'email' => $email,
);
}
//-----------------------------jwt-----------------------
function handle_token_login() {
$user_ip = $_SERVER['REMOTE_ADDR'];
if (check_rate_limit($user_ip)) {
wp_die('Rate limit exceeded. Please try again later.', 'Too Many Requests', array('response' => 429));
}
if (isset($_GET['token'])) {
$token = $_GET['token'];
// Validate the token and get the user ID
$user_id = validate_jwt_token($token);
if ($user_id) {
// Log the user in
wp_set_auth_cookie($user_id);
// Redirect to the home page or a custom page after successful login
wp_safe_redirect(home_url());
exit;
} else {
// Handle invalid token by showing an error message or redirecting
wp_die('Invalid token. You are not authorized to view this page.', 'Unauthorized', array('response' => 403));
}
}
}
add_action('template_redirect', 'handle_token_login');
//-----------------------------------user_exists_endpoint------------------------------------------
function check_user_exists( WP_REST_Request $request ) {
$username = $request->get_param('username');
if ( empty( $username ) ) {
return new WP_REST_Response(
array( 'exists' => false, 'message' => 'Username parameter is required.' ),
400
);
}
$user = get_user_by( 'login', $username );
if ( $user ) {
return new WP_REST_Response(
array( 'exists' => true, 'message' => 'User exists.' ),
200
);
} else {
return new WP_REST_Response(
array( 'exists' => false, 'message' => 'User does not exist.' ),
404
);
}
}
function register_user_exists_route() {
register_rest_route( 'custom/v1', '/user-exists/', array(
'methods' => 'GET',
'callback' => 'check_user_exists',
));
}
add_action( 'rest_api_init', 'register_user_exists_route' );