Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.07% covered (warning)
79.07%
204 / 258
50.00% covered (danger)
50.00%
8 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tokens
79.07% covered (warning)
79.07%
204 / 258
50.00% covered (danger)
50.00%
8 / 16
197.39
0.00% covered (danger)
0.00%
0 / 1
 delete_all
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
1 / 1
9
 validate_blog_token
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
56
 get
77.14% covered (warning)
77.14%
54 / 70
0.00% covered (danger)
0.00%
0 / 1
27.78
 update_user_token
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 sign_role
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
 return_30
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_access_token
78.18% covered (warning)
78.18%
43 / 55
0.00% covered (danger)
0.00%
0 / 1
42.64
 update_blog_token
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 disconnect_user
75.00% covered (warning)
75.00%
6 / 8
0.00% covered (danger)
0.00%
0 / 1
3.14
 get_connected_users
n/a
0 / 0
n/a
0 / 0
1
 get_signed_token
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
5
 get_user_tokens
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update_user_tokens
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 set_lock
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
2.50
 remove_lock
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 is_locked
77.78% covered (warning)
77.78%
14 / 18
0.00% covered (danger)
0.00%
0 / 1
7.54
1<?php
2/**
3 * The Jetpack Connection Tokens class file.
4 *
5 * @package automattic/jetpack-connection
6 */
7
8namespace Automattic\Jetpack\Connection;
9
10use Automattic\Jetpack\Constants;
11use Automattic\Jetpack\Roles;
12use DateInterval;
13use DateTime;
14use Exception;
15use Jetpack_Options;
16use WP_Error;
17
18/**
19 * The Jetpack Connection Tokens class that manages tokens.
20 */
21class Tokens {
22
23    const MAGIC_NORMAL_TOKEN_KEY = ';normal;';
24
25    /**
26     * Datetime format.
27     */
28    const DATE_FORMAT_ATOM = 'Y-m-d\TH:i:sP';
29
30    /**
31     * Deletes all connection tokens and transients from the local Jetpack site.
32     */
33    public function delete_all() {
34        Jetpack_Options::delete_option(
35            array(
36                'blog_token',
37                'user_token',
38                'user_tokens',
39            )
40        );
41
42        $this->remove_lock();
43    }
44
45    /**
46     * Perform the API request to validate the blog and user tokens.
47     *
48     * @param int|null $user_id ID of the user we need to validate token for. Current user's ID by default.
49     *
50     * @return array|false|WP_Error The API response: `array( 'blog_token_is_healthy' => true|false, 'user_token_is_healthy' => true|false )`.
51     */
52    public function validate( $user_id = null ) {
53        $blog_id = Jetpack_Options::get_option( 'id' );
54        if ( ! $blog_id ) {
55            return new WP_Error( 'site_not_registered', 'Site not registered.' );
56        }
57        $url = sprintf(
58            '%s/%s/v%s/%s',
59            Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ),
60            'wpcom',
61            '2',
62            'sites/' . $blog_id . '/jetpack-token-health'
63        );
64
65        $user_token = $this->get_access_token( $user_id ? $user_id : get_current_user_id() );
66        $blog_token = $this->get_access_token();
67
68        // Cannot validate non-existent tokens.
69        if ( false === $user_token || false === $blog_token ) {
70            return false;
71        }
72
73        $method   = 'POST';
74        $body     = array(
75            'user_token' => $this->get_signed_token( $user_token ),
76            'blog_token' => $this->get_signed_token( $blog_token ),
77        );
78        $response = Client::_wp_remote_request( $url, compact( 'body', 'method' ) );
79
80        if ( is_wp_error( $response ) || ! wp_remote_retrieve_body( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
81            return false;
82        }
83
84        $body = json_decode( wp_remote_retrieve_body( $response ), true );
85
86        return $body ? $body : false;
87    }
88
89    /**
90     * Perform the API request to validate only the blog.
91     *
92     * @return bool|WP_Error Boolean with the test result. WP_Error if test cannot be performed.
93     */
94    public function validate_blog_token() {
95        $blog_id = Jetpack_Options::get_option( 'id' );
96        if ( ! $blog_id ) {
97            return new WP_Error( 'site_not_registered', 'Site not registered.' );
98        }
99        $url = sprintf(
100            '%s/%s/v%s/%s',
101            Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ),
102            'wpcom',
103            '2',
104            'sites/' . $blog_id . '/jetpack-token-health/blog'
105        );
106
107        $method   = 'GET';
108        $response = Client::remote_request( compact( 'url', 'method' ) );
109
110        if ( is_wp_error( $response ) || ! wp_remote_retrieve_body( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
111            return false;
112        }
113
114        $body = json_decode( wp_remote_retrieve_body( $response ), true );
115
116        return is_array( $body ) && isset( $body['is_healthy'] ) && true === $body['is_healthy'];
117    }
118
119    /**
120     * Obtains the auth token.
121     *
122     * @param array  $data The request data.
123     * @param string $token_api_url The URL of the Jetpack "token" API.
124     * @return object|WP_Error Returns the auth token on success.
125     *                          Returns a WP_Error on failure.
126     */
127    public function get( $data, $token_api_url ) {
128        $roles = new Roles();
129        $role  = $roles->translate_current_user_to_role();
130
131        if ( ! $role ) {
132            return new WP_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack-connection' ) );
133        }
134
135        $client_secret = $this->get_access_token();
136        if ( ! $client_secret ) {
137            return new WP_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack-connection' ) );
138        }
139
140        /**
141         * Filter the URL of the first time the user gets redirected back to your site for connection
142         * data processing.
143         *
144         * @since 1.7.0
145         * @since-jetpack 8.0.0
146         *
147         * @param string $redirect_url Defaults to the site admin URL.
148         */
149        $processing_url = apply_filters( 'jetpack_token_processing_url', admin_url( 'admin.php' ) );
150
151        $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
152
153        /**
154        * Filter the URL to redirect the user back to when the authentication process
155        * is complete.
156        *
157        * @since 1.7.0
158        * @since-jetpack 8.0.0
159        *
160        * @param string $redirect_url Defaults to the site URL.
161        */
162        $redirect = apply_filters( 'jetpack_token_redirect_url', $redirect );
163
164        $redirect_uri = ( 'calypso' === $data['auth_type'] )
165            ? $data['redirect_uri']
166            : add_query_arg(
167                array(
168                    'handler'  => 'jetpack-connection-webhooks',
169                    'action'   => 'authorize',
170                    '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),
171                    'redirect' => $redirect ? rawurlencode( $redirect ) : false,
172                ),
173                esc_url( $processing_url )
174            );
175
176        /**
177         * Filters the token request data.
178         *
179         * @since 1.7.0
180         * @since-jetpack 8.0.0
181         *
182         * @param array $request_data request data.
183         */
184        $body = apply_filters(
185            'jetpack_token_request_body',
186            array(
187                'client_id'     => Jetpack_Options::get_option( 'id' ),
188                'client_secret' => $client_secret->secret,
189                'grant_type'    => 'authorization_code',
190                'code'          => $data['code'],
191                'redirect_uri'  => $redirect_uri,
192            )
193        );
194
195        $args = array(
196            'method'  => 'POST',
197            'body'    => $body,
198            'headers' => array(
199                'Accept' => 'application/json',
200            ),
201        );
202        add_filter( 'http_request_timeout', array( $this, 'return_30' ), PHP_INT_MAX - 1 );
203        $response = Client::_wp_remote_request( $token_api_url, $args );
204        remove_filter( 'http_request_timeout', array( $this, 'return_30' ), PHP_INT_MAX - 1 );
205
206        if ( is_wp_error( $response ) ) {
207            return new WP_Error( 'token_http_request_failed', $response->get_error_message() );
208        }
209
210        $code   = wp_remote_retrieve_response_code( $response );
211        $entity = wp_remote_retrieve_body( $response );
212
213        if ( $entity ) {
214            $json = json_decode( $entity );
215        } else {
216            $json = false;
217        }
218
219        if ( 200 !== $code || ! empty( $json->error ) ) {
220            if ( empty( $json->error ) ) {
221                return new WP_Error( 'unknown', '', $code );
222            }
223
224            /* translators: Error description string. */
225            $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack-connection' ), (string) $json->error_description ) : '';
226
227            return new WP_Error( (string) $json->error, $error_description, $code );
228        }
229
230        if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) {
231            return new WP_Error( 'access_token', '', $code );
232        }
233
234        if ( empty( $json->token_type ) || 'X_JETPACK' !== strtoupper( $json->token_type ) ) {
235            return new WP_Error( 'token_type', '', $code );
236        }
237
238        if ( empty( $json->scope ) ) {
239            return new WP_Error( 'scope', 'No Scope', $code );
240        }
241
242        // TODO: get rid of the error silencer.
243        // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
244        @list( $role, $hmac ) = explode( ':', $json->scope );
245        if ( empty( $role ) || empty( $hmac ) ) {
246            return new WP_Error( 'scope', 'Malformed Scope', $code );
247        }
248
249        if ( $this->sign_role( $role ) !== $json->scope ) {
250            return new WP_Error( 'scope', 'Invalid Scope', $code );
251        }
252
253        $cap = $roles->translate_role_to_cap( $role );
254        if ( ! $cap ) {
255            return new WP_Error( 'scope', 'No Cap', $code );
256        }
257
258        if ( ! current_user_can( $cap ) ) {
259            return new WP_Error( 'scope', 'current_user_cannot', $code );
260        }
261
262        return (string) $json->access_token;
263    }
264
265    /**
266     * Enters a user token into the user_tokens option
267     *
268     * @param int    $user_id The user id.
269     * @param string $token The user token.
270     * @param bool   $is_master_user Whether the user is the master user.
271     * @return bool
272     */
273    public function update_user_token( $user_id, $token, $is_master_user ) {
274        // Not designed for concurrent updates.
275        $user_tokens = $this->get_user_tokens();
276        if ( ! is_array( $user_tokens ) ) {
277            $user_tokens = array();
278        }
279        $user_tokens[ $user_id ] = $token;
280        if ( $is_master_user ) {
281            $master_user = $user_id;
282            $options     = compact( 'user_tokens', 'master_user' );
283        } else {
284            $options = compact( 'user_tokens' );
285        }
286        return Jetpack_Options::update_options( $options );
287    }
288
289    /**
290     * Sign a user role with the master access token.
291     * If not specified, will default to the current user.
292     *
293     * @access public
294     *
295     * @param string $role    User role.
296     * @param int    $user_id ID of the user.
297     * @return string Signed user role.
298     */
299    public function sign_role( $role, $user_id = null ) {
300        if ( empty( $user_id ) ) {
301            $user_id = (int) get_current_user_id();
302        }
303
304        if ( ! $user_id ) {
305            return false;
306        }
307
308        $token = $this->get_access_token();
309        if ( ! $token || is_wp_error( $token ) ) {
310            return false;
311        }
312
313        return $role . ':' . hash_hmac( 'md5', "{$role}|{$user_id}", $token->secret );
314    }
315
316    /**
317     * Increases the request timeout value to 30 seconds.
318     *
319     * @return int Returns 30.
320     */
321    public function return_30() {
322        return 30;
323    }
324
325    /**
326     * Gets the requested token.
327     *
328     * Tokens are one of two types:
329     * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token,
330     *    though some sites can have multiple "Special" Blog Tokens (see below). These tokens
331     *    are not associated with a user account. They represent the site's connection with
332     *    the Jetpack servers.
333     * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token.
334     *
335     * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the
336     * token, and $private is a secret that should never be displayed anywhere or sent
337     * over the network; it's used only for signing things.
338     *
339     * Blog Tokens can be "Normal" or "Special".
340     * * Normal: The result of a normal connection flow. They look like
341     *   "{$random_string_1}.{$random_string_2}"
342     *   That is, $token_key and $private are both random strings.
343     *   Sites only have one Normal Blog Token. Normal Tokens are found in either
344     *   Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN
345     *   constant (rare).
346     * * Special: A connection token for sites that have gone through an alternative
347     *   connection flow. They look like:
348     *   ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}"
349     *   That is, $private is a random string and $token_key has a special structure with
350     *   lots of semicolons.
351     *   Most sites have zero Special Blog Tokens. Special tokens are only found in the
352     *   JETPACK_BLOG_TOKEN constant.
353     *
354     * In particular, note that Normal Blog Tokens never start with ";" and that
355     * Special Blog Tokens always do.
356     *
357     * When searching for a matching Blog Tokens, Blog Tokens are examined in the following
358     * order:
359     * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant)
360     * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' ))
361     * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant)
362     *
363     * @param int|false    $user_id   false: Return the Blog Token. int: Return that user's User Token.
364     * @param string|false $token_key If provided, check that the token matches the provided input.
365     * @param bool|true    $suppress_errors If true, return a falsy value when the token isn't found; When false, return a descriptive WP_Error when the token isn't found.
366     *
367     * @return object|false|WP_Error
368     */
369    public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) {
370        if ( $this->is_locked() ) {
371            $this->delete_all();
372            return false;
373        }
374
375        $possible_special_tokens = array();
376        $possible_normal_tokens  = array();
377        $user_tokens             = $this->get_user_tokens();
378
379        if ( $user_id ) {
380            $resolved_user_id = true === $user_id ? (int) Jetpack_Options::get_option( 'master_user' ) : (int) $user_id;
381
382            if ( ! $user_tokens ) {
383                return $suppress_errors ? false : new WP_Error( 'no_user_tokens', __( 'No user tokens found', 'jetpack-connection' ), array( 'user_id' => $resolved_user_id ) );
384            }
385            if ( true === $user_id ) { // connection owner.
386                if ( ! $resolved_user_id ) {
387                    return $suppress_errors ? false : new WP_Error( 'empty_master_user_option', __( 'No primary user defined', 'jetpack-connection' ) );
388                }
389                $user_id = $resolved_user_id;
390            }
391            if ( ! isset( $user_tokens[ $user_id ] ) || ! $user_tokens[ $user_id ] ) {
392                // translators: %s is the user ID.
393                return $suppress_errors ? false : new WP_Error( 'no_token_for_user', sprintf( __( 'No token for user %d', 'jetpack-connection' ), $user_id ), array( 'user_id' => (int) $user_id ) );
394            }
395            $user_token_chunks = explode( '.', $user_tokens[ $user_id ] );
396            if ( empty( $user_token_chunks[1] ) || empty( $user_token_chunks[2] ) ) {
397                // translators: %s is the user ID.
398                return $suppress_errors ? false : new WP_Error( 'token_malformed', sprintf( __( 'Token for user %d is malformed', 'jetpack-connection' ), $user_id ), array( 'user_id' => (int) $user_id ) );
399            }
400            if ( $user_token_chunks[2] !== (string) $user_id ) {
401                // translators: %1$d is the ID of the requested user. %2$d is the user ID found in the token.
402                return $suppress_errors ? false : new WP_Error( 'user_id_mismatch', sprintf( __( 'Requesting user_id %1$d does not match token user_id %2$d', 'jetpack-connection' ), $user_id, $user_token_chunks[2] ), array( 'user_id' => (int) $user_id ) );
403            }
404            $possible_normal_tokens[] = "{$user_token_chunks[0]}.{$user_token_chunks[1]}";
405        } else {
406            $stored_blog_token = Jetpack_Options::get_option( 'blog_token' );
407            if ( $stored_blog_token ) {
408                $possible_normal_tokens[] = $stored_blog_token;
409            }
410
411            $defined_tokens_string = Constants::get_constant( 'JETPACK_BLOG_TOKEN' );
412
413            if ( $defined_tokens_string ) {
414                $defined_tokens = explode( ',', $defined_tokens_string );
415                foreach ( $defined_tokens as $defined_token ) {
416                    if ( ';' === $defined_token[0] ) {
417                        $possible_special_tokens[] = $defined_token;
418                    } else {
419                        $possible_normal_tokens[] = $defined_token;
420                    }
421                }
422            }
423        }
424
425        if ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) {
426            $possible_tokens = $possible_normal_tokens;
427        } else {
428            $possible_tokens = array_merge( $possible_special_tokens, $possible_normal_tokens );
429        }
430
431        if ( ! $possible_tokens ) {
432            // If no user tokens were found, it would have failed earlier, so this is about blog token.
433            return $suppress_errors ? false : new WP_Error( 'no_possible_tokens', __( 'No blog token found', 'jetpack-connection' ) );
434        }
435
436        $valid_token = false;
437
438        if ( false === $token_key ) {
439            // Use first token.
440            $valid_token = $possible_tokens[0];
441        } elseif ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) {
442            // Use first normal token.
443            $valid_token = $possible_tokens[0]; // $possible_tokens only contains normal tokens because of earlier check.
444        } else {
445            // Use the token matching $token_key or false if none.
446            // Ensure we check the full key.
447            $token_check = rtrim( $token_key, '.' ) . '.';
448
449            foreach ( $possible_tokens as $possible_token ) {
450                if ( hash_equals( substr( $possible_token, 0, strlen( $token_check ) ), $token_check ) ) {
451                    $valid_token = $possible_token;
452                    break;
453                }
454            }
455        }
456
457        if ( ! $valid_token ) {
458            if ( $user_id ) {
459                // translators: %d is the user ID.
460                return $suppress_errors ? false : new WP_Error( 'no_valid_user_token', sprintf( __( 'Invalid token for user %d', 'jetpack-connection' ), $user_id ), array( 'user_id' => (int) $user_id ) );
461            } else {
462                return $suppress_errors ? false : new WP_Error( 'no_valid_blog_token', __( 'Invalid blog token', 'jetpack-connection' ) );
463            }
464        }
465
466        return (object) array(
467            'secret'           => $valid_token,
468            'external_user_id' => (int) $user_id,
469        );
470    }
471
472    /**
473     * Updates the blog token to a new value.
474     *
475     * @access public
476     *
477     * @param string $token the new blog token value.
478     * @return Boolean Whether updating the blog token was successful.
479     */
480    public function update_blog_token( $token ) {
481        return Jetpack_Options::update_option( 'blog_token', $token );
482    }
483
484    /**
485     * Unlinks the current user from the linked WordPress.com user.
486     *
487     * @access public
488     * @static
489     *
490     * @todo Refactor to properly load the XMLRPC client independently.
491     *
492     * @param int $user_id The user identifier.
493     *
494     * @return bool Whether the disconnection of the user was successful.
495     */
496    public function disconnect_user( $user_id ) {
497        $tokens = $this->get_user_tokens();
498        if ( ! $tokens ) {
499            return false;
500        }
501
502        if ( ! isset( $tokens[ $user_id ] ) ) {
503            return false;
504        }
505
506        unset( $tokens[ $user_id ] );
507
508        $this->update_user_tokens( $tokens );
509
510        return true;
511    }
512
513    /**
514     * Returns an array of user_id's that have user tokens for communicating with wpcom.
515     * Able to select by specific capability.
516     *
517     * @deprecated 1.30.0
518     * @see Manager::get_connected_users
519     *
520     * @param string   $capability The capability of the user.
521     * @param int|null $limit How many connected users to get before returning.
522     * @return array Array of WP_User objects if found.
523     */
524    public function get_connected_users( $capability = 'any', $limit = null ) {
525        _deprecated_function( __METHOD__, '1.30.0' );
526        return ( new Manager( 'jetpack' ) )->get_connected_users( $capability, $limit );
527    }
528
529    /**
530     * Fetches a signed token.
531     *
532     * @param object $token the token.
533     * @return WP_Error|string a signed token
534     */
535    public function get_signed_token( $token ) {
536        if ( ! isset( $token->secret ) || empty( $token->secret ) ) {
537            return new WP_Error( 'invalid_token' );
538        }
539
540        list( $token_key, $token_secret ) = explode( '.', $token->secret );
541
542        $token_key = sprintf(
543            '%s:%d:%d',
544            $token_key,
545            Constants::get_constant( 'JETPACK__API_VERSION' ),
546            $token->external_user_id
547        );
548
549        $timestamp = time();
550
551        if ( function_exists( 'wp_generate_password' ) ) {
552            $nonce = wp_generate_password( 10, false );
553        } else {
554            $nonce = substr( sha1( (string) wp_rand( 0, 1000000 ) ), 0, 10 );
555        }
556
557        $normalized_request_string = implode(
558            "\n",
559            array(
560                $token_key,
561                $timestamp,
562                $nonce,
563            )
564        ) . "\n";
565
566        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
567        $signature = base64_encode( hash_hmac( 'sha1', $normalized_request_string, $token_secret, true ) );
568
569        $auth = array(
570            'token'     => $token_key,
571            'timestamp' => $timestamp,
572            'nonce'     => $nonce,
573            'signature' => $signature,
574        );
575
576        $header_pieces = array();
577        foreach ( $auth as $key => $value ) {
578            $header_pieces[] = sprintf( '%s="%s"', $key, $value );
579        }
580
581        return implode( ' ', $header_pieces );
582    }
583
584    /**
585     * Gets the list of user tokens
586     *
587     * @since 1.30.0
588     *
589     * @return bool|array An array of user tokens where keys are user IDs and values are the tokens. False if no user token is found.
590     */
591    public function get_user_tokens() {
592        return Jetpack_Options::get_option( 'user_tokens' );
593    }
594
595    /**
596     * Updates the option that stores the user tokens
597     *
598     * @since 1.30.0
599     *
600     * @param array $tokens An array of user tokens where keys are user IDs and values are the tokens.
601     * @return bool Was the option successfully updated?
602     *
603     * @todo add validate the input.
604     */
605    public function update_user_tokens( $tokens ) {
606        return Jetpack_Options::update_option( 'user_tokens', $tokens );
607    }
608
609    /**
610     * Lock the tokens to the current site URL.
611     *
612     * @param int $timespan How long the tokens should be locked, in seconds.
613     *
614     * @return bool
615     */
616    public function set_lock( $timespan = HOUR_IN_SECONDS ) {
617        try {
618            $expires = ( new DateTime() )->add( DateInterval::createFromDateString( (int) $timespan . ' seconds' ) );
619        } catch ( Exception $e ) {
620            return false;
621        }
622
623        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
624        return Jetpack_Options::update_option( 'token_lock', $expires->format( static::DATE_FORMAT_ATOM ) . '|||' . base64_encode( Urls::site_url() ) );
625    }
626
627    /**
628     * Remove the site lock from tokens.
629     *
630     * @return bool
631     */
632    public function remove_lock() {
633        Jetpack_Options::delete_option( 'token_lock' );
634
635        return true;
636    }
637
638    /**
639     * Check if the domain is locked, remove the lock if needed.
640     * Possible scenarios:
641     * - lock expired, site URL matches the lock URL: remove the lock, return false.
642     * - lock not expired, site URL matches the lock URL: return false.
643     * - site URL does not match the lock URL (expiration date is ignored): return true, do not remove the lock.
644     *
645     * @return bool
646     */
647    public function is_locked() {
648        $the_lock = Jetpack_Options::get_option( 'token_lock' );
649        if ( ! $the_lock ) {
650            // Not locked.
651            return false;
652        }
653
654        $the_lock = explode( '|||', $the_lock, 2 );
655        if ( count( $the_lock ) !== 2 ) {
656            // Something's wrong with the lock.
657            $this->remove_lock();
658            return false;
659        }
660
661        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
662        $locked_site_url = base64_decode( $the_lock[1] );
663        $expires         = $the_lock[0];
664
665        $expiration_date = DateTime::createFromFormat( static::DATE_FORMAT_ATOM, $expires );
666        if ( false === $expiration_date || ! $locked_site_url ) {
667            // Something's wrong with the lock.
668            $this->remove_lock();
669            return false;
670        }
671
672        if ( Urls::site_url() === $locked_site_url ) {
673            if ( new DateTime() > $expiration_date ) {
674                // Site lock expired.
675                // Site URL matches, removing the lock.
676                $this->remove_lock();
677            }
678
679            return false;
680        }
681
682        // Site URL doesn't match.
683        return true;
684    }
685}