Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Commit

Permalink
Add/Fix: Persist transaction ID and initialize to random number
Browse files Browse the repository at this point in the history
NOTE: According to
<https://matrix.org/docs/spec/client_server/r0.3.0.html#id183>, the
transaction ID should be scoped to the access token, so we should
preserve it with the token.  However, if the client crashes and fails
to save the TID, and then reuses it in the future...what happens?  The
server seems to accept messages with already-used TIDs.  Maybe it has
some kind of heuristic...  I found these:
<matrix-org/synapse#1481> and
<matrix-org/synapse#1624>.
  • Loading branch information
alphapapa committed Jun 11, 2018
1 parent 4aadf50 commit 4a33db1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
21 changes: 19 additions & 2 deletions matrix-api-r0.3.0.el
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,26 @@ FN-NAME should be a string, and is available in the ELSE form as `fn-name'."
(access-token :initarg :access-token
:documentation "API access_token.")
(txn-id :initarg :txn-id
:initform 0
;; Initialize to a random number. This may avoid a potential problem like:

;; 1. User connects.
;; 2. User sends one message with txn-id 0.
;; 3. Client crashes (Emacs is killed, whatever), txn-id is not saved to disk.
;; 4. User restarts Emacs and connects.
;; 5. User sends another message with txn-id 0.

;; If this happens within 30 minutes (see links below), I guess it's possible that the
;; server would reject it as a duplicate txn.
:instance-initform (random 100000)
:type integer
:documentation "Transaction ID. Defaults to 0 and should be automatically incremented for each request.")
;; NOTE: According to <https://matrix.org/docs/spec/client_server/r0.3.0.html#id183>, the
;; transaction ID should be scoped to the access token, so we should preserve it with the
;; token. However, if the client crashes and fails to save the TID, and then reuses it
;; in the future...what happens? The server seems to accept messages with already-used
;; TIDs. Maybe it has some kind of heuristic... I found these:
;; <https://github.com/matrix-org/synapse/issues/1481> and
;; <https://github.com/matrix-org/synapse/pull/1624>.
:documentation "Transaction ID. Defaults to 0 and should be automatically incremented for each request. According to the API, the scope of the transaction ID is an access token, so this should be preserved with the access token.")
(rooms :initarg :rooms
:type list
:documentation "List of room objects user has joined.")
Expand Down
11 changes: 7 additions & 4 deletions matrix-client-ng.el
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,16 @@ method without it."
;; Use saved token
;; FIXME: Change "username" to "user" when we no longer need compatibility with old code
(setq user (a-get saved 'username)
access-token (a-get saved 'token))
access-token (a-get saved 'token)
txn-id (a-get saved 'txn-id))
;; Not saved: prompt for username and password
(setq user (or user (read-string "User ID: "))
password (or password (read-passwd "Password: "))))
(if access-token
;; Use saved token and call post-login hook
(matrix-client-ng-login-hook (matrix-session :user user
:access-token access-token))
:access-token access-token
:txn-id txn-id))
;; Log in with username and password
(matrix-login (matrix-session :user user)
password))))
Expand All @@ -255,11 +257,12 @@ Add session to sessions list and run initial sync."
"Save username and access token for session SESSION to file."
;; TODO: Check if file exists; if so, ensure it has a proper header so we know it's ours.
(with-temp-file matrix-client-ng-save-token-file
(with-slots (user access-token) session
(with-slots (user access-token txn-id) session
;; FIXME: Change "username" to "user" when we no longer need compatibility with old code
;; FIXME: Change token to access-token for clarity.
(prin1 (a-list 'username user
'token access-token)
'token access-token
'txn-id txn-id)
(current-buffer))))
;; Ensure permissions are safe
(chmod matrix-client-ng-save-token-file #o600))
Expand Down

0 comments on commit 4a33db1

Please sign in to comment.