Skip to content

Commit c70e594

Browse files
authored
Merge pull request #245 from akirk/allow-other-auths
Allow other OAuth2 providers
2 parents c522286 + f4c2e64 commit c70e594

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

includes/class-indieauth-authorize.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,17 @@ public function determine_current_user( $user_id ) {
155155

156156
$params = $this->verify_access_token( $token );
157157
if ( ! isset( $params ) ) {
158-
return 0;
158+
return $user_id;
159159
}
160160
if ( is_oauth_error( $params ) ) {
161161
$this->error = $params;
162-
return 0;
162+
return $user_id;
163163
}
164164
if ( is_array( $params ) ) {
165-
// If this is a token auth response, add this constant.
166-
define( 'INDIEAUTH_TOKEN', true );
165+
// If this is a token auth response and not a test run, add this constant.
166+
if ( ! function_exists( 'tests_add_filter' ) ) {
167+
define( 'INDIEAUTH_TOKEN', true );
168+
}
167169

168170
$this->response = $params;
169171
$this->scopes = explode( ' ', $params['scope'] );
@@ -181,7 +183,7 @@ public function determine_current_user( $user_id ) {
181183
'response' => $me,
182184
)
183185
);
184-
return 0;
186+
return $user_id;
185187

186188
}
187189

tests/test-authorize.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,69 @@ public function test_authorize() {
5151
$_REQUEST['micropub'] = 'endpoint';
5252
$_POST['access_token'] = $token;
5353
$authorize = new Indieauth_Local_Authorize();
54-
$user_id = $authorize->determine_current_user( 0 );
54+
$user_id = $authorize->determine_current_user( false );
5555
$this->assertEquals( $user_id, self::$author_id );
5656
}
5757

58+
public function test_authorize_bearer() {
59+
$token = self::set_token();
60+
$_REQUEST['micropub'] = 'endpoint';
61+
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer ' . $token;
62+
$authorize = new Indieauth_Local_Authorize();
63+
$authorize->load();
64+
$user_id = apply_filters( 'determine_current_user', false );
65+
$this->assertEquals( $user_id, self::$author_id );
66+
}
67+
68+
public function test_authorize_bearer_other_non_matching_provider() {
69+
$token = self::set_token();
70+
$self_author_id = self::$author_id;
71+
add_filter( 'determine_current_user', function( $user_id ) use ( $self_author_id ) {
72+
if ( 'Bearer other-valid-token' === $_SERVER['HTTP_AUTHORIZATION'] ) {
73+
return $self_author_id + 1;
74+
}
75+
return $user_id;
76+
} );
77+
$_REQUEST['micropub'] = 'endpoint';
78+
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer ' . $token;
79+
$authorize = new Indieauth_Local_Authorize();
80+
$authorize->load();
81+
$user_id = apply_filters( 'determine_current_user', false );
82+
$this->assertEquals( $user_id, self::$author_id );
83+
}
84+
85+
public function test_authorize_bearer_other_provider() {
86+
$self_author_id = self::$author_id;
87+
add_filter( 'determine_current_user', function( $user_id ) use ( $self_author_id ) {
88+
if ( 'Bearer other-valid-token' === $_SERVER['HTTP_AUTHORIZATION'] ) {
89+
return $self_author_id;
90+
}
91+
return $user_id;
92+
} );
93+
$_REQUEST['micropub'] = 'endpoint';
94+
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer other-valid-token';
95+
$authorize = new Indieauth_Local_Authorize();
96+
$authorize->load();
97+
$user_id = apply_filters( 'determine_current_user', false );
98+
$this->assertEquals( $user_id, self::$author_id );
99+
}
100+
101+
public function test_authorize_bearer_no_valid_token_other_provider() {
102+
$self_author_id = self::$author_id;
103+
add_filter( 'determine_current_user', function( $user_id ) use ( $self_author_id ) {
104+
if ( 'Bearer other-valid-token' === $_SERVER['HTTP_AUTHORIZATION'] ) {
105+
return $self_author_id;
106+
}
107+
return $user_id;
108+
} );
109+
$_REQUEST['micropub'] = 'endpoint';
110+
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer other-invalid-token';
111+
$authorize = new Indieauth_Local_Authorize();
112+
$authorize->load();
113+
$user_id = apply_filters( 'determine_current_user', false );
114+
$this->assertFalse( $user_id );
115+
}
116+
58117
// Tests map_meta_cap for standard permissions
59118
public function test_publish_posts_with_scopes() {
60119
add_filter( 'indieauth_scopes',

0 commit comments

Comments
 (0)