-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathclass-authorization-code.php
More file actions
67 lines (57 loc) · 1.4 KB
/
class-authorization-code.php
File metadata and controls
67 lines (57 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace WP\OAuth2\Types;
use WP_Error;
use WP\OAuth2\Client;
class Authorization_Code extends Base {
/**
* Get response_type code for authorisation page.
*
* This is used to determine which type to route requests to.
*
* @return string
*/
public function get_response_type_code() {
return 'code';
}
/**
* Handles the authorization.
*
* @param string $submit
* @param Client $client
* @param array $data
*
* @return WP_Error
*/
protected function handle_authorization_submission( $submit, Client $client, $data ) {
$redirect_uri = $data['redirect_uri'];
switch ( $submit ) {
case 'authorize':
// Generate authorization code and redirect back.
$user = wp_get_current_user();
$code = $client->generate_authorization_code( $user, $data );
if ( is_wp_error( $code ) ) {
return $code;
}
$redirect_args = [
'code' => $code->get_code(),
];
break;
case 'cancel':
$redirect_args = [
'error' => 'access_denied',
];
break;
default:
return new WP_Error(
'oauth2.types.authorization_code.handle_authorisation.invalid_action',
__( 'Invalid form action.', 'oauth2' )
);
}
if ( ! empty( $data['state'] ) ) {
$redirect_args['state'] = $data['state'];
}
$generated_redirect = add_query_arg( urlencode_deep( $redirect_args ), $redirect_uri );
wp_redirect( $generated_redirect );
exit;
}
}