22
33namespace WP \OAuth2 \Authentication ;
44
5- use WP_Http ;
65use WP \OAuth2 \Tokens ;
76
87/**
@@ -23,7 +22,7 @@ function get_authorization_header() {
2322 if ( function_exists ( 'getallheaders ' ) ) {
2423 $ headers = getallheaders ();
2524
26- // Check for the authoization header case-insensitively
25+ // Check for the authorization header case-insensitively
2726 foreach ( $ headers as $ key => $ value ) {
2827 if ( strtolower ( $ key ) === 'authorization ' ) {
2928 return $ value ;
@@ -36,30 +35,57 @@ function get_authorization_header() {
3635
3736function get_provided_token () {
3837 $ 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 ];
4153 }
4254
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 ' ] ) ) {
4660 return null ;
4761 }
4862
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 ;
5074}
5175
5276/**
5377 * Try to authenticate if possible.
5478 *
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
5682 */
5783function attempt_authentication ( $ user = null ) {
5884 if ( ! empty ( $ user ) ) {
5985 return $ user ;
6086 }
6187
62- // Were we given an token?
88+ // Were we given a token?
6389 $ token_value = get_provided_token ();
6490 if ( empty ( $ token_value ) ) {
6591 // No data provided, pass.
@@ -69,16 +95,20 @@ function attempt_authentication( $user = null ) {
6995 // Attempt to find the token.
7096 $ token = Tokens \get_by_id ( $ token_value );
7197 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 );
8099 }
81100
82101 // Token found, authenticate as the user.
83102 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