Skip to content

Commit

Permalink
make services singletons
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Dec 10, 2023
1 parent 9398172 commit 05b423d
Show file tree
Hide file tree
Showing 26 changed files with 748 additions and 604 deletions.
1 change: 1 addition & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ composer.lock
hookdoc-conf.json
package.json
yarn.lock
php-scoper
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP with tools
uses: shivammathur/setup-php@v2
Expand All @@ -29,6 +29,7 @@ jobs:
YARN_ENABLE_IMMUTABLE_INSTALLS: false
run: |
yarn install
composer prefix-dependencies
composer install
- name: Linting the code
Expand Down
10 changes: 2 additions & 8 deletions includes/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,13 @@ class API {
*/
protected $wc_rest_api_handler;

/**
* @var
*/
protected $auth_service;

/**
* @var bool
*/
protected $is_auth_checked = false;


public function __construct() {
$this->auth_service = new Auth();

// Init and register routes for the WCPOS REST API
$this->controllers = array(
// woocommerce pos rest api controllers
Expand Down Expand Up @@ -324,7 +317,8 @@ private function authenticate( $user_id ) {
list($token) = sscanf( $auth_header, 'Bearer %s' );

if ( $token ) {
$decoded_token = $this->auth_service->validate_token( $token );
$auth_service = Auth::instance();
$decoded_token = $auth_service->validate_token( $token );

// Check if validate_token returned WP_Error and user_id is null
if ( is_wp_error( $decoded_token ) && $user_id === null ) {
Expand Down
44 changes: 0 additions & 44 deletions includes/API/Abstracts/Controller.php

This file was deleted.

67 changes: 0 additions & 67 deletions includes/API/Abstracts/WC_Rest_API_Modifier.php

This file was deleted.

63 changes: 35 additions & 28 deletions includes/API/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,32 @@
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
use function is_wp_error;
use function rest_ensure_response;
use function wp_authenticate;
use WP_REST_Controller;
use WCPOS\WooCommercePOS\Services\Auth as AuthService;
use const WCPOS\WooCommercePOS\SHORT_NAME;

/**
*
*/
class Auth extends WP_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = SHORT_NAME . '/v1';

class Auth extends Abstracts\Controller {
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'jwt';

/**
*
*/
protected $auth_service;

/**
* Stores constructor.
*/
public function __construct() {
$this->auth_service = new \WCPOS\WooCommercePOS\Services\Auth();
}

/**
Expand All @@ -50,10 +54,10 @@ public function register_routes(): void {
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'generate_token' ),
'permission_callback' => function ( WP_REST_Request $request ) {
// special case for user=demo param
if ( $request->get_param( 'user' ) === 'demo' ) {
return true;
}
// special case for user=demo param
if ( $request->get_param( 'user' ) === 'demo' ) {
return true;
}

$authorization = $request->get_header( 'authorization' );

Expand Down Expand Up @@ -117,7 +121,8 @@ public function register_routes(): void {

/**
* Get the user and password in the request body and generate a JWT.
* @NOTE - not allowing REST Auth at the moment
*
* @NOTE - not allowing REST Auth at the moment
*
* @param WP_REST_Request $request
* @return WP_Error|WP_REST_Response
Expand All @@ -142,8 +147,9 @@ public function generate_token( WP_REST_Request $request ) {
)
);
} else {
$data = $this->auth_service->generate_token( $user );
}
$auth_service = AuthService::instance();
$data = $auth_service->generate_token( $user );
}

/**
* Let the user modify the data before sending it back
Expand All @@ -162,17 +168,18 @@ public function generate_token( WP_REST_Request $request ) {
return rest_ensure_response( $data );
}

/**
* Validate JWT Token.
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public function validate_token( WP_REST_Request $request ): WP_REST_Response {
$token = $request->get_param( 'jwt' );
$result = $this->auth_service->validate_token( $token );
return rest_ensure_response( $result );
}
/**
* Validate JWT Token.
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public function validate_token( WP_REST_Request $request ): WP_REST_Response {
$token = $request->get_param( 'jwt' );
$auth_service = AuthService::instance();
$result = $auth_service->validate_token( $token );
return rest_ensure_response( $result );
}

/**
* Refresh JWT Token.
Expand Down
Loading

0 comments on commit 05b423d

Please sign in to comment.