|
10 | 10 |
|
11 | 11 | namespace WCPOS\WooCommercePOS\API; |
12 | 12 |
|
13 | | -use WP_Error; |
| 13 | +use const WCPOS\WooCommercePOS\SHORT_NAME; |
| 14 | +use WP_REST_Controller; |
14 | 15 | use WP_REST_Request; |
15 | 16 | use WP_REST_Response; |
16 | 17 | use WP_REST_Server; |
17 | | -use WP_REST_Controller; |
18 | | -use WCPOS\WooCommercePOS\Services\Auth as AuthService; |
19 | | -use const WCPOS\WooCommercePOS\SHORT_NAME; |
20 | 18 |
|
21 | | -/** |
22 | | - * |
23 | | - */ |
24 | 19 | class Auth extends WP_REST_Controller { |
25 | | - /** |
26 | | - * Endpoint namespace. |
27 | | - * |
28 | | - * @var string |
29 | | - */ |
| 20 | + /** |
| 21 | + * Endpoint namespace. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
30 | 25 | protected $namespace = SHORT_NAME . '/v1'; |
31 | 26 |
|
32 | 27 | /** |
33 | 28 | * Route base. |
34 | 29 | * |
35 | 30 | * @var string |
36 | 31 | */ |
37 | | - protected $rest_base = 'jwt'; |
| 32 | + protected $rest_base = 'auth'; |
38 | 33 |
|
39 | 34 | /** |
40 | 35 | * Stores constructor. |
41 | 36 | */ |
42 | 37 | public function __construct() { |
43 | 38 | } |
44 | 39 |
|
45 | | - /** |
46 | | - * |
47 | | - */ |
48 | 40 | public function register_routes(): void { |
49 | | - // Generate JWT token |
| 41 | + // Test authorization method support (public endpoint) |
50 | 42 | register_rest_route( |
51 | 43 | $this->namespace, |
52 | | - '/' . $this->rest_base . '/authorize', |
| 44 | + '/' . $this->rest_base . '/test', |
53 | 45 | array( |
54 | 46 | 'methods' => WP_REST_Server::READABLE, |
55 | | - 'callback' => array( $this, 'generate_token' ), |
56 | | - 'permission_callback' => function ( WP_REST_Request $request ) { |
57 | | - // special case for user=demo param |
58 | | - if ( $request->get_param( 'user' ) === 'demo' ) { |
59 | | - return true; |
60 | | - } |
61 | | - |
62 | | - $authorization = $request->get_header( 'authorization' ); |
63 | | - |
64 | | - return ! is_null( $authorization ); |
65 | | - }, |
66 | | - ) |
67 | | - ); |
68 | | - |
69 | | - // Validate JWT token |
70 | | - register_rest_route( |
71 | | - $this->namespace, |
72 | | - '/' . $this->rest_base . '/validate', |
73 | | - array( |
74 | | - 'methods' => WP_REST_Server::CREATABLE, |
75 | | - 'callback' => array( $this, 'validate_token' ), |
76 | | - 'permission_callback' => '__return_true', |
77 | | - 'args' => array( |
78 | | - 'jwt' => array( |
79 | | - 'description' => __( 'JWT token.', 'woocommerce-pos' ), |
80 | | - 'type' => 'string', |
81 | | - ), |
82 | | - ), |
83 | | - ) |
84 | | - ); |
85 | | - |
86 | | - // Refresh JWT token |
87 | | - register_rest_route( |
88 | | - $this->namespace, |
89 | | - '/' . $this->rest_base . '/refresh', |
90 | | - array( |
91 | | - 'methods' => WP_REST_Server::CREATABLE, |
92 | | - 'callback' => array( $this, 'refresh_token' ), |
93 | | - 'permission_callback' => '__return_true', |
94 | | - 'args' => array( |
95 | | - 'jwt' => array( |
96 | | - 'description' => __( 'JWT token.', 'woocommerce-pos' ), |
97 | | - 'type' => 'string', |
98 | | - ), |
99 | | - ), |
100 | | - ) |
101 | | - ); |
102 | | - |
103 | | - // Revoke JWT token |
104 | | - register_rest_route( |
105 | | - $this->namespace, |
106 | | - '/' . $this->rest_base . '/revoke', |
107 | | - array( |
108 | | - 'methods' => WP_REST_Server::CREATABLE, |
109 | | - 'callback' => array( $this, 'revoke_token' ), |
110 | | - 'permission_callback' => '__return_true', |
111 | | - 'args' => array( |
112 | | - 'jwt' => array( |
113 | | - 'description' => __( 'JWT token.', 'woocommerce-pos' ), |
114 | | - 'type' => 'string', |
115 | | - ), |
116 | | - ), |
| 47 | + 'callback' => array( $this, 'test_authorization' ), |
| 48 | + 'permission_callback' => '__return_true', // Public endpoint - no authentication required |
117 | 49 | ) |
118 | 50 | ); |
119 | 51 | } |
120 | 52 |
|
121 | 53 |
|
122 | 54 | /** |
123 | | - * Get the user and password in the request body and generate a JWT. |
| 55 | + * Test authorization method endpoint. |
124 | 56 | * |
125 | | - * @NOTE - not allowing REST Auth at the moment |
| 57 | + * This public endpoint tests whether the server supports Authorization headers |
| 58 | + * or requires query parameters for authorization. This is important because |
| 59 | + * some servers block Authorization headers for security reasons. |
126 | 60 | * |
127 | | - * @param WP_REST_Request $request |
128 | | - * @return WP_Error|WP_REST_Response |
| 61 | + * @param WP_REST_Request $request The REST request object. |
| 62 | + * |
| 63 | + * @return WP_REST_Response |
129 | 64 | */ |
130 | | - public function generate_token( WP_REST_Request $request ) { |
131 | | - $token = str_replace( 'Basic ', '', $request->get_header( 'authorization' ) ); |
132 | | - $decoded = base64_decode( $token, true ); |
133 | | - list($username, $password) = explode( ':', $decoded ); |
134 | | - |
135 | | - /** Try to authenticate the user with the passed credentials*/ |
136 | | - $user = wp_authenticate( $username, $password ); |
137 | | - |
138 | | - // If the authentication fails return an error |
139 | | - if ( is_wp_error( $user ) ) { |
140 | | - $error_code = $user->get_error_code(); |
141 | | - |
142 | | - $user_data = new WP_Error( |
143 | | - 'woocommerce_pos_' . $error_code, |
144 | | - $user->get_error_message( $error_code ), |
145 | | - array( |
146 | | - 'status' => 403, |
147 | | - ) |
148 | | - ); |
149 | | - } else { |
150 | | - $auth_service = AuthService::instance(); |
151 | | - $user_data = $auth_service->get_user_data( $user ); |
152 | | - $stores = array_map( |
153 | | - function ( $store ) { |
154 | | - return $store->get_data(); |
155 | | - }, |
156 | | - wcpos_get_stores() |
157 | | - ); |
158 | | - $user_data['stores'] = $stores; |
| 65 | + public function test_authorization( WP_REST_Request $request ): WP_REST_Response { |
| 66 | + // Check for Authorization header |
| 67 | + $header_auth = $request->get_header( 'authorization' ); |
| 68 | + $has_header_auth = ! empty( $header_auth ); |
| 69 | + |
| 70 | + // Check for authorization query parameter |
| 71 | + $param_auth = $request->get_param( 'authorization' ); |
| 72 | + $has_param_auth = ! empty( $param_auth ); |
| 73 | + |
| 74 | + // Only return success if we received authorization via at least one method |
| 75 | + if ( ! $has_header_auth && ! $has_param_auth ) { |
| 76 | + return rest_ensure_response( array( |
| 77 | + 'status' => 'error', |
| 78 | + 'message' => 'No authorization token detected', |
| 79 | + ) ); |
159 | 80 | } |
160 | 81 |
|
161 | | - /** |
162 | | - * Let the user modify the data before sending it back |
163 | | - * |
164 | | - * @param {object} $data |
165 | | - * @param {WP_User} $user |
166 | | - * |
167 | | - * @returns {object} Response data |
168 | | - * |
169 | | - * @since 1.0.0 |
170 | | - * |
171 | | - * @hook woocommerce_pos_jwt_auth_token_before_dispatch |
172 | | - */ |
173 | | - $user_data = apply_filters( 'woocommerce_pos_jwt_auth_token_before_dispatch', $user_data, $user ); |
174 | | - |
175 | | - return rest_ensure_response( $user_data ); |
176 | | - } |
| 82 | + $response_data = array( |
| 83 | + 'status' => 'success', |
| 84 | + 'message' => 'Authorization token detected successfully', |
| 85 | + ); |
177 | 86 |
|
178 | | - /** |
179 | | - * Validate JWT Token. |
180 | | - * |
181 | | - * @param WP_REST_Request $request |
182 | | - * @return WP_REST_Response |
183 | | - */ |
184 | | - public function validate_token( WP_REST_Request $request ): WP_REST_Response { |
185 | | - $token = $request->get_param( 'jwt' ); |
186 | | - $auth_service = AuthService::instance(); |
187 | | - $result = $auth_service->validate_token( $token ); |
188 | | - return rest_ensure_response( $result ); |
189 | | - } |
| 87 | + // Add authorization details |
| 88 | + $response_data['received_header_auth'] = $has_header_auth; |
| 89 | + if ( $has_header_auth ) { |
| 90 | + $response_data['header_value'] = $header_auth; |
| 91 | + } |
190 | 92 |
|
191 | | - /** |
192 | | - * Refresh JWT Token. |
193 | | - */ |
194 | | - public function refresh_token(): void { |
195 | | - } |
| 93 | + $response_data['received_param_auth'] = $has_param_auth; |
| 94 | + if ( $has_param_auth ) { |
| 95 | + $response_data['param_value'] = $param_auth; |
| 96 | + } |
196 | 97 |
|
197 | | - /** |
198 | | - * Revoke JWT Token. |
199 | | - */ |
200 | | - public function revoke_token(): void { |
| 98 | + // Indicate which method was used |
| 99 | + if ( $has_header_auth && $has_param_auth ) { |
| 100 | + $response_data['auth_method'] = 'both'; |
| 101 | + } elseif ( $has_header_auth ) { |
| 102 | + $response_data['auth_method'] = 'header'; |
| 103 | + } else { |
| 104 | + $response_data['auth_method'] = 'param'; |
| 105 | + } |
| 106 | + |
| 107 | + return rest_ensure_response( $response_data ); |
201 | 108 | } |
202 | 109 | } |
0 commit comments