@@ -19,7 +19,7 @@ my $next_token = 0;
19
19
20
20
# Perpetually correct access token for authenticating with v2 Identity Service API endpoints.
21
21
# v2 endpoint calls to this identity server should include this value for their
22
- # `id_access_token ` parameter
22
+ # `access_token ` parameter
23
23
my $ID_ACCESS_TOKEN = " swordfish" ;
24
24
25
25
sub _init
@@ -121,7 +121,7 @@ sub on_request
121
121
elsif ( $path eq " /_matrix/identity/v2/3pid/bind" ) {
122
122
$self -> check_v2( $req ) and $self -> on_bind( $req );
123
123
}
124
- elsif ( # v2 /unbind does not require an id_access_token param
124
+ elsif ( # v2 /unbind does not require an access_token param
125
125
$path eq " /_matrix/identity/v2/3pid/unbind" or
126
126
$path eq " /_matrix/identity/api/v1/3pid/unbind"
127
127
) {
@@ -137,47 +137,39 @@ sub on_request
137
137
138
138
$server->check_v2 ( $req ) and do_something_else();
139
139
140
- A helper method that takes an HTTP request and checks if an C<id_access_token > parameter
141
- matching C<$ID_ACCESS_TOKEN > is present in either the query parameters or the top-level JSON of
142
- the request body .
140
+ A helper method that takes an HTTP request and checks if an C<access_token > parameter
141
+ matching C<$ID_ACCESS_TOKEN > is present in either the query parameters or the Authorization
142
+ header (after the Bearer declaration) .
143
143
144
- Returns C<0 > or C<1 > depending on whether a correct C<id_access_token > value was found.
144
+ Returns C<0 > or C<1 > depending on whether a correct C<access_token > value was found.
145
145
146
- Responds to the HTTP request with an error message if no C<id_access_token > value was found.
146
+ Responds to the HTTP request with an error message if no C<access_token > value was found.
147
147
148
148
=cut
149
149
150
150
sub check_v2
151
151
{
152
- # Check that either an id_access_token query parameter or JSON body key exists in the req
152
+ # Check that either an access_token query parameter or JSON body key exists in the req
153
153
my $self = shift ;
154
154
my ( $req ) = @_ ;
155
155
my %resp ;
156
156
157
- if (
158
- $req -> query_param(" id_access_token" ) and
159
- $req -> query_param(" id_access_token" ) eq $ID_ACCESS_TOKEN
160
- ) {
157
+ my $query_param = $req -> query_param(" access_token" );
158
+ if ( $query_param and $query_param eq $ID_ACCESS_TOKEN ) {
161
159
# We found it!
162
160
return 1;
163
161
}
164
162
165
- # Check the JSON body for the token. This isn't required for all endpoints so only try if
166
- # the request has a body.
167
- # We use an eval in case this request doesn't have a JSON body
168
- my $body = eval { $req -> body_from_json };
169
-
170
- if (
171
- $body and
172
- $body -> {id_access_token } and
173
- $body -> {id_access_token } eq $ID_ACCESS_TOKEN
174
- ) {
163
+ # Check the Authorization header for the token
164
+ # Should be in the form Authorization: Bearer <access_token>
165
+ my $auth_header = $req -> header(" Authorization" );
166
+ if ( $auth_header and $auth_header eq " Bearer " . $ID_ACCESS_TOKEN ) {
175
167
# We found it!
176
168
return 1;
177
169
}
178
170
179
171
# Couldn't find an access token
180
- $resp {error } = " Missing id_access_token parameter" ;
172
+ $resp {error } = " Missing access_token parameter" ;
181
173
$resp {errcode } = " M_MISSING_PARAM" ;
182
174
$req -> respond_json( \%resp , code => 400 );
183
175
return 0;
0 commit comments