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