@@ -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-
238232if ( ! 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 }
276270endif ;
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
0 commit comments