2
2
3
3
namespace WP \OAuth2 \Authentication ;
4
4
5
- use WP_Http ;
6
5
use WP \OAuth2 \Tokens ;
7
6
8
7
/**
@@ -23,7 +22,7 @@ function get_authorization_header() {
23
22
if ( function_exists ( 'getallheaders ' ) ) {
24
23
$ headers = getallheaders ();
25
24
26
- // Check for the authoization header case-insensitively
25
+ // Check for the authorization header case-insensitively
27
26
foreach ( $ headers as $ key => $ value ) {
28
27
if ( strtolower ( $ key ) === 'authorization ' ) {
29
28
return $ value ;
@@ -36,30 +35,57 @@ function get_authorization_header() {
36
35
37
36
function get_provided_token () {
38
37
$ header = get_authorization_header ();
39
- if ( empty ( $ header ) || ! is_string ( $ header ) ) {
40
- return null ;
38
+ if ( $ header ) {
39
+ return get_token_from_bearer_header ( $ header );
40
+ }
41
+
42
+ $ token = get_token_from_request ();
43
+ if ( $ token ) {
44
+ return $ token ;
45
+ }
46
+
47
+ return null ;
48
+ }
49
+
50
+ function get_token_from_bearer_header ( $ header ) {
51
+ if ( is_string ( $ header ) && preg_match ( '/Bearer ([a-zA-Z0-9\-._~\+\/=]+)/ ' , trim ( $ header ), $ matches ) ) {
52
+ return $ matches [1 ];
41
53
}
42
54
43
- // Attempt to parse as a Bearer header.
44
- $ is_valid = preg_match ( '/Bearer ([a-zA-Z0-9=.~\-\+\/]+)/ ' , trim ( $ header ), $ matches );
45
- if ( ! $ is_valid ) {
55
+ return null ;
56
+ }
57
+
58
+ function get_token_from_request () {
59
+ if ( empty ( $ _GET ['access_token ' ] ) ) {
46
60
return null ;
47
61
}
48
62
49
- return $ matches [1 ];
63
+ $ token = $ _GET ['access_token ' ];
64
+ if ( is_string ( $ token ) ) {
65
+ return $ token ;
66
+ }
67
+
68
+ // Please note that the following includes PHP 5.3+ code. Ryan said it would be fine, soon. ;)
69
+ add_filter ( 'rest_authentication_errors ' , function ( $ error ) use ( $ token ) {
70
+ return null === $ error ? create_invalid_token_error ( $ token ) : null ;
71
+ } );
72
+
73
+ return null ;
50
74
}
51
75
52
76
/**
53
77
* Try to authenticate if possible.
54
78
*
55
- * @param WP_User|null $user Existing authenticated user.
79
+ * @param \WP_User|null $user Existing authenticated user.
80
+ *
81
+ * @return \WP_User|int|\WP_Error
56
82
*/
57
83
function attempt_authentication ( $ user = null ) {
58
84
if ( ! empty ( $ user ) ) {
59
85
return $ user ;
60
86
}
61
87
62
- // Were we given an token?
88
+ // Were we given a token?
63
89
$ token_value = get_provided_token ();
64
90
if ( empty ( $ token_value ) ) {
65
91
// No data provided, pass.
@@ -69,16 +95,20 @@ function attempt_authentication( $user = null ) {
69
95
// Attempt to find the token.
70
96
$ token = Tokens \get_by_id ( $ token_value );
71
97
if ( empty ( $ token ) ) {
72
- return new WP_Error (
73
- 'oauth2.authentication.attempt_authentication.invalid_token ' ,
74
- __ ( 'Supplied token is invalid. ' , 'oauth2 ' ),
75
- array (
76
- 'status ' => WP_Http::FORBIDDEN ,
77
- 'token ' => $ token_value ,
78
- ),
79
- );
98
+ return create_invalid_token_error ( $ token );
80
99
}
81
100
82
101
// Token found, authenticate as the user.
83
102
return $ token ->get_user_id ();
84
- }
103
+ }
104
+
105
+ function create_invalid_token_error ( $ token ) {
106
+ return new \WP_Error (
107
+ 'oauth2.authentication.attempt_authentication.invalid_token ' ,
108
+ __ ( 'Supplied token is invalid. ' , 'oauth2 ' ),
109
+ array (
110
+ 'status ' => \WP_Http::FORBIDDEN ,
111
+ 'token ' => $ token ,
112
+ )
113
+ );
114
+ }
0 commit comments