Skip to content

Commit

Permalink
Merge pull request #55 from 10up/fix/52
Browse files Browse the repository at this point in the history
Fix: authorization mapkit issue
  • Loading branch information
helen authored Jun 26, 2020
2 parents b40e2fa + d0f8f79 commit a42b469
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
12 changes: 12 additions & 0 deletions includes/rest-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
40 changes: 23 additions & 17 deletions src/components/AppleMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
} );
}
}

Expand Down

0 comments on commit a42b469

Please sign in to comment.