Skip to content

Commit ba13b2e

Browse files
authored
feat: Authorizing URLs implemented and tested. (#745)
* feat: Authorizing URLs implemented and tested. * feat: More woographql_*_nonce functions implemented. * chore: linting changes made. * chore: linting changes made. * fix: woographql_*_ functions tested. * chore: WPCS compliance met. * devops: lint-code script updated to PHP v8.0 * chore: WPCS compliance met * devops: TransferSessionHandlerTest & QLSessionHandlerTest updated * devops: codeclimate.yml added. * chore: Linter compliance met * devops: Harmonizing WordPress doc written and Settings doc updated. * chore: Typo fixed in docs. * fix: General bugfixes and improvements related to Auth URLs * devops: More docs. * chore: Linter compliance met * chore: small change made to docs.
1 parent 6ea0474 commit ba13b2e

36 files changed

+2287
-89
lines changed

.github/workflows/lint-code.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
- name: Setup PHP
2424
uses: shivammathur/setup-php@v2
2525
with:
26-
php-version: 7.3
26+
php-version: 8.0
2727
extensions: mbstring, intl
2828
tools: composer
2929

access-functions.php

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,6 @@ function wc_graphql_camel_case_to_underscore( $string ) {
229229
}
230230
}//end if
231231

232-
/**
233-
* Plugin global functions.
234-
*
235-
* @package Axis\Plugin_Distributor
236-
*/
237-
238232
if ( ! function_exists( 'woographql_setting' ) ) :
239233
/**
240234
* Get an option value from WooGraphQL settings
@@ -275,6 +269,92 @@ function woographql_setting( string $option_name, $default = '', $section_name =
275269
}
276270
endif;
277271

272+
if ( ! function_exists( 'woographql_get_session_uid' ) ) :
273+
/**
274+
* Returns end-user's customer ID.
275+
*
276+
* @return string|int
277+
*/
278+
function woographql_get_session_uid() {
279+
return WC()->session->get_customer_id();
280+
}
281+
endif;
282+
283+
if ( ! function_exists( 'woographql_get_session_token' ) ) :
284+
/**
285+
* Returns session user's "client_session_id"
286+
*
287+
* @return string
288+
*/
289+
function woographql_get_session_token() {
290+
return WC()->session->get_client_session_id();
291+
}
292+
endif;
293+
294+
if ( ! function_exists( 'woographql_create_nonce' ) ) :
295+
/**
296+
* Creates WooGraphQL session transfer nonces.
297+
*
298+
* @param string $action Nonce name.
299+
*/
300+
function woographql_create_nonce( $action = -1 ) {
301+
$uid = woographql_get_session_uid();
302+
$token = woographql_get_session_token();
303+
$i = wp_nonce_tick( $action );
304+
305+
return substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
306+
}
307+
endif;
308+
309+
if ( ! function_exists( 'woographql_verify_nonce' ) ) :
310+
/**
311+
* Validate WooGraphQL session transfer nonces.
312+
*
313+
* @param string $nonce Nonce to validated.
314+
* @param integer|string $action Nonce name.
315+
*
316+
* @return bool
317+
*/
318+
function woographql_verify_nonce( $nonce, $action = -1 ) {
319+
$nonce = (string) $nonce;
320+
$uid = woographql_get_session_uid();
321+
322+
if ( empty( $nonce ) ) {
323+
return false;
324+
}
325+
326+
$token = woographql_get_session_token();
327+
$i = wp_nonce_tick( $action );
328+
329+
// Nonce generated 0-12 hours ago.
330+
$expected = substr( wp_hash( $i . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
331+
if ( hash_equals( $expected, $nonce ) ) {
332+
return 1;
333+
}
334+
335+
// Nonce generated 12-24 hours ago.
336+
$expected = substr( wp_hash( ( $i - 1 ) . '|' . $action . '|' . $uid . '|' . $token, 'nonce' ), -12, 10 );
337+
if ( hash_equals( $expected, $nonce ) ) {
338+
return 2;
339+
}
340+
341+
/**
342+
* Fires when nonce verification fails.
343+
*
344+
* @since 4.4.0
345+
*
346+
* @param string $nonce The invalid nonce.
347+
* @param string|int $action The nonce action.
348+
* @param WP_User $user The current user object.
349+
* @param string $token The user's session token.
350+
*/
351+
do_action( 'graphql_verify_nonce_failed', $nonce, $action, $uid, $token );
352+
353+
// Invalid nonce.
354+
return false;
355+
}
356+
endif;
357+
278358

279359

280360

codeception.dist.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ modules:
5757
REST:
5858
depends: WPBrowser
5959
url: '%WORDPRESS_URL%'
60+
cookies: false
6061
WPFilesystem:
6162
wpRootFolder: '%WP_CORE_DIR%'
6263
plugins: '/wp-content/plugins'

codeclimate.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
plugins:
2+
phpcodesniffer:
3+
enabled: true
4+
config:
5+
standard: "phpcs.xml.dist"

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"firebase/php-jwt": "^6.1.0"
2626
},
2727
"require-dev": {
28+
"automattic/vipwpcs": "^2.3",
2829
"squizlabs/php_codesniffer": "^3.5",
2930
"wp-coding-standards/wpcs": "^2.3"
3031
},
@@ -34,6 +35,7 @@
3435
"sort-packages": true,
3536
"allow-plugins": {
3637
"johnpbloch/wordpress-core-installer": true,
38+
"dealerdirect/phpcodesniffer-composer-installer": true,
3739
"composer/installers": true
3840
}
3941
},

0 commit comments

Comments
 (0)