Skip to content

Commit e242f69

Browse files
Accept access_token instead of id_access_token on the IS, accept Authorization header (#697)
Synapse PR: matrix-org/synapse#6013 which has context We now accept `access_token` instead of `id_access_token` and do so using the `Authorization` header instead of in the JSON body, as [MSC2140](https://github.com/matrix-org/matrix-doc/pull/2140/files#diff-c03a26de5ac40fb532de19cb7fc2aaf7R80) states.
1 parent 2c04e47 commit e242f69

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

lib/SyTest/Identity/Server.pm

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ my $next_token = 0;
1919

2020
# Perpetually correct access token for authenticating with v2 Identity Service API endpoints.
2121
# v2 endpoint calls to this identity server should include this value for their
22-
# `id_access_token` parameter
22+
# `access_token` parameter
2323
my $ID_ACCESS_TOKEN = "swordfish";
2424

2525
sub _init
@@ -121,7 +121,7 @@ sub on_request
121121
elsif ( $path eq "/_matrix/identity/v2/3pid/bind" ) {
122122
$self->check_v2( $req ) and $self->on_bind( $req );
123123
}
124-
elsif ( # v2 /unbind does not require an id_access_token param
124+
elsif ( # v2 /unbind does not require an access_token param
125125
$path eq "/_matrix/identity/v2/3pid/unbind" or
126126
$path eq "/_matrix/identity/api/v1/3pid/unbind"
127127
) {
@@ -137,47 +137,39 @@ sub on_request
137137
138138
$server->check_v2 ( $req ) and do_something_else();
139139
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).
143143
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.
145145
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.
147147
148148
=cut
149149

150150
sub check_v2
151151
{
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
153153
my $self = shift;
154154
my ( $req ) = @_;
155155
my %resp;
156156

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 ) {
161159
# We found it!
162160
return 1;
163161
}
164162

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 ) {
175167
# We found it!
176168
return 1;
177169
}
178170

179171
# Couldn't find an access token
180-
$resp{error} = "Missing id_access_token parameter";
172+
$resp{error} = "Missing access_token parameter";
181173
$resp{errcode} = "M_MISSING_PARAM";
182174
$req->respond_json( \%resp, code => 400 );
183175
return 0;

0 commit comments

Comments
 (0)