Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit 15f162a

Browse files
author
Mohammad Shahbaz Alam
authored
Merge pull request #2 from magiclabs/fix-test-n-header
Added log tests and auth header options
2 parents b144cc3 + a7bccb2 commit 15f162a

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

assets/.DS_Store

-6 KB
Binary file not shown.

includes/class-login.php

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function register_routes(){
162162
* Return log-in link, if user not exist - create new user
163163
* Fired by `rest_api_init` action hook.
164164
* @return string
165-
* @todo fix signature
165+
* @return string|false
166166
*
167167
* @since 0.0.0
168168
* @access public
@@ -180,7 +180,7 @@ public function get_auth_link()
180180
$result = wp_create_user($name, wp_generate_password(), $this->user_email); // Create wp user
181181
if(is_wp_error($result)){ // If get error to create the user - log message
182182
$error = $result->get_error_message();
183-
error_log( $error );
183+
$this->log( $error );
184184
}else{ // If user was successfully created - receive and return login url
185185
$user_id_role = new WP_User($result);
186186
$user_id_role->set_role($this->user_role);
@@ -230,8 +230,15 @@ public function get_login_url($user_data)
230230
public function validate_token()
231231
{
232232
$headers = apache_request_headers();
233-
if(!empty($headers['Authorization'])){ // Check exist authorization field in header
234-
$did_token = \MagicAdmin\Util\Http::parse_authorization_header_value($headers['Authorization']);
233+
234+
if(!empty($headers['Authorization'])){
235+
$token = $headers['Authorization'];
236+
}elseif (!empty($headers['authorization'])){
237+
$token = $headers['authorization'];
238+
}
239+
240+
if(!empty($token)){ // Check exist authorization field in header
241+
$did_token = \MagicAdmin\Util\Http::parse_authorization_header_value($token);
235242

236243
// Deny access if token not exist
237244
if ($did_token == null) {
@@ -253,15 +260,15 @@ public function validate_token()
253260
}
254261
}
255262
} catch (\MagicAdmin\Exception\DIDTokenException $e) {
256-
error_log( print_r($e->getMessage(), true) );
263+
$this->log( $e->getMessage() );
257264
return false;
258265
} catch (\MagicAdmin\Exception\RequestException $e) {
259-
error_log( print_r($e->getMessage(), true) );
266+
$this->log( $e->getMessage() );
260267
return false;
261268
}
262269
}else{
263-
error_log( 'Failed to receive authorization header' );
264-
error_log( print_r($headers, true) );
270+
$this->log( 'Failed to receive authorization header' );
271+
$this->log( $headers );
265272
return false;
266273
}
267274
}
@@ -358,5 +365,18 @@ protected function exit()
358365
{
359366
exit;
360367
}
368+
369+
/**
370+
* Logging
371+
*
372+
* @param $message
373+
*/
374+
protected function log($message): void
375+
{
376+
if (!is_string($message)) {
377+
$message = print_r($message, true);
378+
}
379+
error_log($message);
380+
}
361381
}
362382

0 commit comments

Comments
 (0)