| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Utils; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
/** |
| 8 |
* The codes the Google sign-in callback may hand back through the redirect URL. |
| 9 |
* |
| 10 |
* Only a code travels. The callback used to put the failure PROSE in an |
| 11 |
* `error_message` query parameter, which the sign-in screen then rendered as |
| 12 |
* markup — a reflected XSS anyone could fire by getting an administrator to |
| 13 |
* open a crafted admin URL. A code cannot carry a payload: the React side |
| 14 |
* matches it against this same list and discards anything it does not |
| 15 |
* recognise, and the copy for each one lives in the plugin. |
| 16 |
* |
| 17 |
* Values mirror `includes/Utils/Response/ErrorCode.php` on the `staging` branch |
| 18 |
* so the two converge when that work lands on the release line. |
| 19 |
*/ |
| 20 |
class AuthErrorCode { |
| 21 |
|
| 22 |
/** |
| 23 |
* The state token was missing, expired, or minted for another user. |
| 24 |
*/ |
| 25 |
const AUTH_STATE_INVALID = 'templately_auth_state_invalid'; |
| 26 |
|
| 27 |
/** |
| 28 |
* The callback arrived without an API key to connect with. |
| 29 |
*/ |
| 30 |
const AUTH_MISSING_API_KEY = 'templately_auth_missing_api_key'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Google itself refused — it returns its reason in `?error=`, which we |
| 34 |
* deliberately do not forward. |
| 35 |
*/ |
| 36 |
const AUTH_PROVIDER_FAILED = 'templately_auth_provider_failed'; |
| 37 |
|
| 38 |
/** |
| 39 |
* The key was rejected when we tried to connect with it. Also the catch-all: |
| 40 |
* anything without a code of its own collapses here, so no upstream-authored |
| 41 |
* string ever needs to reach the URL. |
| 42 |
*/ |
| 43 |
const INVALID_API_KEY = 'templately_invalid_api_key'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Every code this class defines. |
| 47 |
* |
| 48 |
* @return string[] |
| 49 |
*/ |
| 50 |
public static function all() { |
| 51 |
return [ |
| 52 |
self::AUTH_STATE_INVALID, |
| 53 |
self::AUTH_MISSING_API_KEY, |
| 54 |
self::AUTH_PROVIDER_FAILED, |
| 55 |
self::INVALID_API_KEY, |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Whether a value is one of ours. |
| 61 |
* |
| 62 |
* @param mixed $code Candidate code, typically straight off the query string. |
| 63 |
* |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
public static function exists( $code ) { |
| 67 |
return is_string( $code ) && in_array( $code, self::all(), true ); |
| 68 |
} |
| 69 |
} |
| 70 |
|