diff --git a/includes/rest-routes.php b/includes/rest-routes.php index bbaff2f..9abb524 100644 --- a/includes/rest-routes.php +++ b/includes/rest-routes.php @@ -62,6 +62,18 @@ function get_jwt() { if ( ! isset( $team_id ) || '' === $team_id ) { return new WP_Error( 'NoKey', 'Missing Team ID', [ 'status' => 401 ] ); } + if ( 10 !== strlen( $key_id ) ) { + return new WP_Error( 'InvalidKey', 'Invalid Key ID', [ 'status' => 401 ] ); + } + if ( 10 !== strlen( $team_id ) ) { + return new WP_Error( 'InvalidKey', 'Invalid Team ID', [ 'status' => 401 ] ); + } + if ( + 0 !== strpos( $private_key, '-----BEGIN PRIVATE KEY-----' ) + && ! strpos( $private_key, '-----END PRIVATE KEY-----' ) + ) { + return new WP_Error( 'InvalidKey', 'Invalid Private Key', [ 'status' => 401 ] ); + } $header = [ 'alg' => 'ES256', diff --git a/src/components/AppleMap.js b/src/components/AppleMap.js index 48490f4..9034ede 100644 --- a/src/components/AppleMap.js +++ b/src/components/AppleMap.js @@ -153,24 +153,30 @@ class AppleMap { } static authenticateMap() { - function getJWTToken( resolveCallback ) { - apiFetch( { path: 'MapsBlockApple/v1/GetJWT/' } ) - .then( resolveCallback ) - .catch( ( error ) => { - dispatch( 'core/notices' ).createErrorNotice( - error.message, - { - isDismissible: true, - type: 'snackbar', - } - ); - mapkit.dispatchEvent( new Event( 'error' ) ); + apiFetch( { path: 'MapsBlockApple/v1/GetJWT/' } ) + .then( () => { + mapkit.init( { + authorizationCallback( done ) { + /** + * JWT only lives for 30 mins. Calling it again here to + * allow mapkit to get new token when needed. + * + * @see https://github.com/10up/maps-block-apple/issues/48 + * @see https://github.com/10up/maps-block-apple/pull/52 + */ + apiFetch( { path: 'MapsBlockApple/v1/GetJWT/' } ).then( + done + ); + }, } ); - } - - mapkit.init( { - authorizationCallback: getJWTToken, - } ); + } ) + .catch( ( error ) => { + dispatch( 'core/notices' ).createErrorNotice( error.message, { + isDismissible: true, + type: 'snackbar', + } ); + mapkit.dispatchEvent( new Event( 'error' ) ); + } ); } }