|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WP\OAuth2; |
| 4 | + |
| 5 | +use WP\OAuth2\Types\Type; |
| 6 | +use WP_REST_Response; |
| 7 | + |
| 8 | +function bootstrap() { |
| 9 | + // Core authentication hooks. |
| 10 | + add_action( 'init', __NAMESPACE__ . '\\Client::register_type' ); |
| 11 | + add_filter( 'determine_current_user', __NAMESPACE__ . '\\Authentication\\attempt_authentication', 11 ); |
| 12 | + |
| 13 | + // REST API integration. |
| 14 | + add_filter( 'rest_authentication_errors', __NAMESPACE__ . '\\Authentication\\maybe_report_errors' ); |
| 15 | + add_filter( 'rest_index', __NAMESPACE__ . '\\register_in_index' ); |
| 16 | + add_action( 'rest_api_init', __NAMESPACE__ . '\\Endpoints\\register' ); |
| 17 | + |
| 18 | + // Internal default hooks. |
| 19 | + add_filter( 'oauth2.grant_types', __NAMESPACE__ . '\\register_grant_types', 0 ); |
| 20 | + |
| 21 | + // Admin-related. |
| 22 | + add_action( 'init', __NAMESPACE__ . '\\rest_oauth2_load_authorize_page' ); |
| 23 | + add_action( 'admin_menu', __NAMESPACE__ . '\\Admin\\register' ); |
| 24 | + Admin\Profile\bootstrap(); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Register the authorization page |
| 29 | + * |
| 30 | + * Alas, login_init is too late to register pages, as the action is already |
| 31 | + * sanitized before this. |
| 32 | + */ |
| 33 | +function rest_oauth2_load_authorize_page() { |
| 34 | + $authorizer = new Endpoints\Authorization(); |
| 35 | + $authorizer->register_hooks(); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Get valid grant types. |
| 40 | + * |
| 41 | + * @return Type[] Map of grant type to handler object. |
| 42 | + */ |
| 43 | +function get_grant_types() { |
| 44 | + /** |
| 45 | + * Filter valid grant types. |
| 46 | + * |
| 47 | + * Default supported grant types are added in register_grant_types(). |
| 48 | + * Note that additional grant types must follow the extension policy in the |
| 49 | + * OAuth 2 specification. |
| 50 | + * |
| 51 | + * @param Type[] $grant_types Map of grant type to handler object. |
| 52 | + */ |
| 53 | + $grant_types = apply_filters( 'oauth2.grant_types', [] ); |
| 54 | + foreach ( $grant_types as $type => $handler ) { |
| 55 | + if ( ! $handler instanceof Type ) { |
| 56 | + /* translators: 1: Grant type name, 2: Grant type interface */ |
| 57 | + $message = __( 'Skipping invalid grant type "%1$s". Required interface "%1$s" not implemented.', 'oauth2' ); |
| 58 | + _doing_it_wrong( __FUNCTION__, sprintf( $message, $type, 'WP\\OAuth2\\Types\\Type' ), '0.1.0' ); |
| 59 | + unset( $grant_types[ $type ] ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return $grant_types; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Register default grant types. |
| 68 | + * |
| 69 | + * Callback for the oauth2.grant_types hook. |
| 70 | + * |
| 71 | + * @param array $types Existing grant types. |
| 72 | + * @return array Grant types with additional types registered. |
| 73 | + */ |
| 74 | +function register_grant_types( $types ) { |
| 75 | + $types['authorization_code'] = new Types\Authorization_Code(); |
| 76 | + $types['implicit'] = new Types\Implicit(); |
| 77 | + |
| 78 | + return $types; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Register the OAuth 2 authentication scheme in the API index. |
| 83 | + * |
| 84 | + * @param WP_REST_Response $response Index response object. |
| 85 | + * @return WP_REST_Response Update index repsonse object. |
| 86 | + */ |
| 87 | +function register_in_index( WP_REST_Response $response ) { |
| 88 | + $data = $response->get_data(); |
| 89 | + |
| 90 | + $data['authentication']['oauth2'] = [ |
| 91 | + 'endpoints' => [ |
| 92 | + 'authorization' => get_authorization_url(), |
| 93 | + 'token' => get_token_url(), |
| 94 | + ], |
| 95 | + 'grant_types' => array_keys( get_grant_types() ), |
| 96 | + ]; |
| 97 | + |
| 98 | + $response->set_data( $data ); |
| 99 | + return $response; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Get the authorization endpoint URL. |
| 104 | + * |
| 105 | + * @return string URL for the OAuth 2 authorization endpoint. |
| 106 | + */ |
| 107 | +function get_authorization_url() { |
| 108 | + $url = wp_login_url(); |
| 109 | + $url = add_query_arg( 'action', 'oauth2_authorize', $url ); |
| 110 | + |
| 111 | + /** |
| 112 | + * Filter the authorization URL. |
| 113 | + * |
| 114 | + * @param string $url URL for the OAuth 2 authorization endpoint. |
| 115 | + */ |
| 116 | + return apply_filters( 'oauth2.get_authorization_url', $url ); |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Get the token endpoint URL. |
| 121 | + * |
| 122 | + * @return string URL for the OAuth 2 token endpoint. |
| 123 | + */ |
| 124 | +function get_token_url() { |
| 125 | + $url = rest_url( 'oauth2/access_token' ); |
| 126 | + |
| 127 | + /** |
| 128 | + * Filter the token URL. |
| 129 | + * |
| 130 | + * @param string $url URL for the OAuth 2 token endpoint. |
| 131 | + */ |
| 132 | + return apply_filters( 'oauth2.get_token_url', $url ); |
| 133 | +} |
0 commit comments