From 12c2dcc6d272d532d1e89adfe8ba7bebfc3823ad Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Thu, 4 Jul 2024 13:34:56 +0200 Subject: [PATCH] Use Authorization header instead of GET param to pass access_token --- scripts/patrix | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/scripts/patrix b/scripts/patrix index 9894d0e..c0aee4c 100644 --- a/scripts/patrix +++ b/scripts/patrix @@ -159,6 +159,9 @@ sub send_request { die "Missing an URI" unless $param->{uri}; my $req = HTTP::Request->new( $param->{method}, $param->{uri} ); $req->header('Content-Type' => $param->{content_type}); + if (defined $opt->{access_token}){ + $req->header('Authorization' => "Bearer $opt->{access_token}"); + } $req->content($param->{content}); my $resp = $lwp->request( $req ); debug("Server responded:\n" . to_json(from_json($resp->decoded_content), { pretty => 1 })); @@ -208,7 +211,7 @@ sub login { # Invalidate the access_token sub logout { debug("Trying to logout"); - my $uri = $opt->{server} . '/_matrix/client/r0/logout?access_token=' . $opt->{access_token}; + my $uri = $opt->{server} . '/_matrix/client/r0/logout'; my $resp = send_request({ uri => $uri }); @@ -218,8 +221,8 @@ sub logout { # Join the specified room, before we can send anything sub join_room { debug("Trying to join room $opt->{room}"); - # Room must be escaped. if not and room is an alias, it'll start with # so the access_token won't be sent - my $uri = $opt->{server} . '/_matrix/client/r0/join/' . uri_escape( $opt->{room} ) . '?access_token=' . $opt->{access_token}; + # Room must be escaped. + my $uri = $opt->{server} . '/_matrix/client/r0/join/' . uri_escape( $opt->{room} ); my $resp = send_request({ uri => $uri }); @@ -229,7 +232,7 @@ sub join_room { # Retrieve the actual permissions for a room sub get_room_permissions { debug('Getting actual room state'); - my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels?access_token=' . $opt->{access_token}; + my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels'; my $resp = send_request({ method => 'GET', uri => $uri @@ -250,7 +253,7 @@ sub who_am_i { # Send a text message (either message or notice as both are similar) sub send_msg { - my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message?access_token=' . $opt->{access_token}; + my $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message'; # Ignore --message if reading from stdin if ($stdin){ $opt->{message} = ''; @@ -282,7 +285,7 @@ sub send_file { # Sending a file is a 2 steps operation. First we need to upload the file to the media store # And then we post the uri on the room debug("Uploading file $file to the media store"); - my $uri = $opt->{server} . '/_matrix/media/v1/upload?access_token=' . $opt->{access_token} . '&filename=' . basename($file); + my $uri = $opt->{server} . '/_matrix/media/v1/upload?filename=' . basename($file); my $mime = mimetype($file); my $resp = send_request({ uri => $uri, @@ -296,7 +299,7 @@ sub send_file { die "Server did not sent the file URI\n" unless ($file_uri); debug("File uploaded, with the URI $file_uri\nNow Sending the file link to the room $opt->{room}"); # Now lets post a new message with the URI of the file - $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message?access_token=' . $opt->{access_token}; + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/send/m.room.message'; my $json = { msgtype => 'm.file', body => basename($file), @@ -328,7 +331,7 @@ sub send_file { # Note that there's no pagination handling yet, so you might not have all the results sub list_room { debug("Fetching list of public rooms on $opt->{server}"); - my $uri = $opt->{server} . '/_matrix/client/r0/publicRooms?access_token=' . $opt->{access_token}; + my $uri = $opt->{server} . '/_matrix/client/r0/publicRooms'; my $resp = send_request({ uri => $uri, }); @@ -346,7 +349,7 @@ sub list_room { # Create a new room sub create_room { debug("Creating a new room on $opt->{server}"); - my $uri = $opt->{server} . '/_matrix/client/r0/createRoom?access_token=' . $opt->{access_token}; + my $uri = $opt->{server} . '/_matrix/client/r0/createRoom'; my $json = {}; my $resp = send_request({ uri => $uri, @@ -366,7 +369,7 @@ sub modify_room { # A new alias should be added if ($opt->{alias}){ debug('Adding ' . $opt->{alias} . ' as a room alias'); - $uri = $opt->{server} . '/_matrix/client/r0/directory/room/' . uri_escape($opt->{alias}) . '?access_token=' . $opt->{access_token}; + $uri = $opt->{server} . '/_matrix/client/r0/directory/room/' . uri_escape($opt->{alias}); $json = { room_id => $opt->{room} }; @@ -381,7 +384,7 @@ sub modify_room { # The name of the room is being updated if ($opt->{name}){ debug('Changing the room name to ' . $opt->{name}); - $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.name?access_token=' . $opt->{access_token}; + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.name'; $json = { name => $opt->{name} }; @@ -396,7 +399,7 @@ sub modify_room { # The topic is being updated if ($opt->{topic}){ debug('Changing the room topic to ' . $opt->{topic}); - $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.topic?access_token=' . $opt->{access_token}; + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.topic'; $json = { topic => $opt->{topic} }; @@ -411,7 +414,7 @@ sub modify_room { # Changing joining rules if ($opt->{join_rules}){ debug('Changing the joining rules to '. $opt->{join_rules}); - $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.join_rules?access_token=' . $opt->{access_token}; + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.join_rules'; $json = { join_rules => $opt->{join_rules} }; @@ -475,7 +478,7 @@ sub modify_room { } my $perm = merge($current_perm, $new_perm); debug("New permissions for this room will be:\n" . to_json($perm, { pretty => 1 })); - $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels?access_token=' . $opt->{access_token}; + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/state/m.room.power_levels'; $resp = send_request({ method => 'PUT', uri => $uri, @@ -487,7 +490,7 @@ sub modify_room { # New invitees should be added if ($opt->{invite}){ debug('Inviting ' . join(',', @{$opt->{invite}}) . ' to join the room'); - $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/invite?access_token=' . $opt->{access_token}; + $uri = $opt->{server} . '/_matrix/client/r0/rooms/' . $opt->{room} . '/invite'; foreach my $invite (@{$opt->{invite}}){ $json = { user_id => $invite @@ -511,7 +514,7 @@ sub modify_room { sub del_room_alias { debug("Removing room alias $opt->{alias}"); - my $uri = $opt->{server} . "/_matrix/client/r0/directory/room/" . uri_escape($opt->{alias}) . '?access_token=' . $opt->{access_token}; + my $uri = $opt->{server} . "/_matrix/client/r0/directory/room/" . uri_escape($opt->{alias}); my $resp = send_request({ method => 'DELETE', uri => $uri