Authentication
1 week ago
Consent
1 week ago
Endpoints
1 week ago
Helpers
1 week ago
PKCE
1 week ago
Storage
1 week ago
Bootstrap.php
1 week ago
Constants.php
1 week ago
Module.php
1 week ago
Constants.php
147 lines
| 1 | <?php |
| 2 | /** |
| 3 | * OAuth-related constants. |
| 4 | * |
| 5 | * @package PrestoPlayer |
| 6 | * @subpackage Services\OAuth |
| 7 | */ |
| 8 | |
| 9 | namespace PrestoPlayer\Services\OAuth; |
| 10 | |
| 11 | /** |
| 12 | * Central place for OAuth scopes, TTLs and REST path fragments. |
| 13 | */ |
| 14 | class Constants { |
| 15 | |
| 16 | /** |
| 17 | * Read-only abilities scope. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | public const SCOPE_READ = 'presto:read'; |
| 22 | |
| 23 | /** |
| 24 | * Create / update abilities scope. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public const SCOPE_WRITE = 'presto:write'; |
| 29 | |
| 30 | /** |
| 31 | * Delete (destructive) abilities scope. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | public const SCOPE_DESTRUCTIVE = 'presto:destructive'; |
| 36 | |
| 37 | /** |
| 38 | * Settings / license / admin operations scope. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | public const SCOPE_ADMIN = 'presto:admin'; |
| 43 | |
| 44 | /** |
| 45 | * Access token lifetime in seconds (1 hour). |
| 46 | * |
| 47 | * @var int |
| 48 | */ |
| 49 | public const ACCESS_TOKEN_TTL = 3600; |
| 50 | |
| 51 | /** |
| 52 | * Refresh token lifetime in seconds (30 days). |
| 53 | * |
| 54 | * @var int |
| 55 | */ |
| 56 | public const REFRESH_TOKEN_TTL = 2592000; |
| 57 | |
| 58 | /** |
| 59 | * Absolute refresh-token lifetime in seconds (90 days). A rotation chain is |
| 60 | * refused once it passes this cap measured from the original authorization, |
| 61 | * so a client that keeps refreshing can't hold a grant forever. |
| 62 | * |
| 63 | * @var int |
| 64 | */ |
| 65 | public const REFRESH_TOKEN_ABSOLUTE_TTL = 7776000; |
| 66 | |
| 67 | /** |
| 68 | * Authorization code lifetime in seconds (10 minutes). |
| 69 | * |
| 70 | * @var int |
| 71 | */ |
| 72 | public const AUTH_CODE_TTL = 600; |
| 73 | |
| 74 | /** |
| 75 | * REST namespace shared by every Presto Player route. |
| 76 | * |
| 77 | * @var string |
| 78 | */ |
| 79 | public const REST_NAMESPACE = 'presto-player/v1'; |
| 80 | |
| 81 | /** |
| 82 | * Base path segment under the namespace for OAuth endpoints. |
| 83 | * |
| 84 | * @var string |
| 85 | */ |
| 86 | public const OAUTH_BASE = 'oauth'; |
| 87 | |
| 88 | /** |
| 89 | * Site-relative path for the browser-facing consent screen. Served outside |
| 90 | * the REST stack (via parse_request) so WordPress core's REST cookie-nonce |
| 91 | * check can't log the user out on GET or 403 the consent POST. |
| 92 | * |
| 93 | * @var string |
| 94 | */ |
| 95 | public const AUTHORIZE_PATH = '/presto-player/oauth/authorize'; |
| 96 | |
| 97 | /** |
| 98 | * The WordPress capability a user must have to grant the given OAuth scope. |
| 99 | * |
| 100 | * Read/write map to edit_posts; the privileged destructive/admin scopes |
| 101 | * require manage_options, so an Editor can't consent a client into deleting |
| 102 | * content or changing settings. |
| 103 | * |
| 104 | * @param string $scope Scope identifier. |
| 105 | * @return string Capability name. |
| 106 | */ |
| 107 | public static function capabilityForScope( $scope ) { |
| 108 | switch ( $scope ) { |
| 109 | case self::SCOPE_ADMIN: |
| 110 | case self::SCOPE_DESTRUCTIVE: |
| 111 | return 'manage_options'; |
| 112 | default: |
| 113 | return 'edit_posts'; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Resolve the full list of allowed OAuth scopes. |
| 119 | * |
| 120 | * Pro (or third parties) can append their own identifiers via the |
| 121 | * `presto_player_oauth_scopes` filter. The four built-in scopes are always |
| 122 | * present in the return value regardless of filter mutations. |
| 123 | * |
| 124 | * @return array<int, string> |
| 125 | */ |
| 126 | public static function allowedScopes() { |
| 127 | $default = array( |
| 128 | self::SCOPE_READ, |
| 129 | self::SCOPE_WRITE, |
| 130 | self::SCOPE_DESTRUCTIVE, |
| 131 | self::SCOPE_ADMIN, |
| 132 | ); |
| 133 | |
| 134 | /** |
| 135 | * Filters the list of allowed OAuth scope identifiers. |
| 136 | * |
| 137 | * @param array<int, string> $scopes Default scope identifiers. |
| 138 | */ |
| 139 | $filtered = apply_filters( 'presto_player_oauth_scopes', $default ); |
| 140 | if ( ! is_array( $filtered ) ) { |
| 141 | $filtered = array(); |
| 142 | } |
| 143 | |
| 144 | return array_values( array_unique( array_merge( $default, array_filter( array_map( 'strval', $filtered ) ) ) ) ); |
| 145 | } |
| 146 | } |
| 147 |