Skip to content

Commit d9cb4ad

Browse files
committed
Remove hardcoding of types
1 parent ccb7097 commit d9cb4ad

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

inc/endpoints/class-authorization.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WP\OAuth2\Endpoints;
44

55
use WP_Error;
6+
use WP\OAuth2;
67
use WP\OAuth2\Client;
78
use WP\OAuth2\Types;
89

@@ -24,21 +25,18 @@ public function handle_request() {
2425
$type = null;
2526
}
2627

27-
switch ( $type ) {
28-
case 'code':
29-
$handler = new Types\AuthorizationCode();
30-
break;
31-
32-
case 'token':
33-
$handler = new Types\Implicit();
34-
break;
35-
36-
default:
37-
$result = new WP_Error(
38-
'oauth2.endpoints.authorization.handle_request.invalid_type',
39-
__( 'Invalid response type specified.', 'oauth2' )
40-
);
41-
break;
28+
// Match type to a handler.
29+
$grant_types = OAuth2\get_grant_types();
30+
foreach ( $grant_types as $type_handler ) {
31+
if ( $type_handler->get_response_type_code() === $type ) {
32+
$handler = $type_handler;
33+
}
34+
}
35+
if ( empty( $handler ) ) {
36+
$result = new WP_Error(
37+
'oauth2.endpoints.authorization.handle_request.invalid_type',
38+
__( 'Invalid response type specified.', 'oauth2' )
39+
);
4240
}
4341

4442
if ( empty( $result ) ) {

inc/types/class-authorization-code.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
use WP\OAuth2\Client;
77

88
class AuthorizationCode extends Base {
9+
/**
10+
* Get response_type code for authorisation page.
11+
*
12+
* This is used to determine which type to route requests to.
13+
*
14+
* @return string
15+
*/
16+
public function get_response_type_code() {
17+
return 'code';
18+
}
19+
920
protected function handle_authorization_submission( $submit, Client $client, $data ) {
1021
$redirect_uri = $data['redirect_uri'];
1122

inc/types/class-implicit.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
use WP\OAuth2\Client;
77

88
class Implicit extends Base {
9+
/**
10+
* Get response_type code for authorisation page.
11+
*
12+
* This is used to determine which type to route requests to.
13+
*
14+
* @return string
15+
*/
16+
public function get_response_type_code() {
17+
return 'token';
18+
}
919

1020
protected function handle_authorization_submission( $submit, Client $client, $data ) {
1121
$redirect_uri = $data['redirect_uri'];

inc/types/class-type.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,13 @@
44

55
interface Type {
66
public function handle_authorisation();
7+
8+
/**
9+
* Get response_type code for authorisation page.
10+
*
11+
* This is used to determine which type to route requests to.
12+
*
13+
* @return string
14+
*/
15+
public function get_response_type_code();
716
}

0 commit comments

Comments
 (0)