Skip to content

Commit 56838e8

Browse files
committed
Test the scope of a transaction IDs
This adds two tests, which check the current spec behaviour of transaction IDs, which are that they are scoped to a series of access tokens, and not the device ID. The first test highlight this behaviour, by logging in with refresh token enabled, sending an event, using the refresh token and syncing with the new access token. On the sync, the transaction ID should be there, but currently in Synapse it is not. The second test highlight that the transaction ID is not scoped to the device ID, by logging in twice with the same device ID, sending an event with the first access token, and syncing with the second access token. In that case, the sync should not contain the transaction ID, but I think it's the case in HS implementations which use the device ID to scope the transaction IDs, like Conduit. Related: matrix-org/matrix-spec#1133, matrix-org/matrix-spec#1236, matrix-org/synapse#13064 and matrix-org/synapse#13083
1 parent 67644cd commit 56838e8

File tree

2 files changed

+196
-1
lines changed

2 files changed

+196
-1
lines changed

internal/client/client.go

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,81 @@ func (c *CSAPI) LoginUser(t *testing.T, localpart, password string) (userID, acc
438438
return userID, accessToken, deviceID
439439
}
440440

441-
//RegisterUser will register the user with given parameters and
441+
// LoginUserWithDeviceID will log in to a homeserver on an existing device
442+
func (c *CSAPI) LoginUserWithDeviceID(t *testing.T, localpart, password, deviceID string) (userID, accessToken string) {
443+
t.Helper()
444+
reqBody := map[string]interface{}{
445+
"identifier": map[string]interface{}{
446+
"type": "m.id.user",
447+
"user": localpart,
448+
},
449+
"device_id": deviceID,
450+
"password": password,
451+
"type": "m.login.password",
452+
}
453+
res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "login"}, WithJSONBody(t, reqBody))
454+
455+
body, err := ioutil.ReadAll(res.Body)
456+
if err != nil {
457+
t.Fatalf("unable to read response body: %v", err)
458+
}
459+
460+
userID = gjson.GetBytes(body, "user_id").Str
461+
accessToken = gjson.GetBytes(body, "access_token").Str
462+
if gjson.GetBytes(body, "device_id").Str != deviceID {
463+
t.Fatalf("device_id returned by login does not match the one requested")
464+
}
465+
return userID, accessToken
466+
}
467+
468+
// LoginUserWithRefreshToken will log in to a homeserver, with refresh token enabled,
469+
// and create a new device on an existing user.
470+
func (c *CSAPI) LoginUserWithRefreshToken(t *testing.T, localpart, password string) (userID, accessToken, refreshToken, deviceID string, expiresInMs int64) {
471+
t.Helper()
472+
reqBody := map[string]interface{}{
473+
"identifier": map[string]interface{}{
474+
"type": "m.id.user",
475+
"user": localpart,
476+
},
477+
"password": password,
478+
"type": "m.login.password",
479+
"refresh_token": true,
480+
}
481+
res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "login"}, WithJSONBody(t, reqBody))
482+
483+
body, err := ioutil.ReadAll(res.Body)
484+
if err != nil {
485+
t.Fatalf("unable to read response body: %v", err)
486+
}
487+
488+
userID = gjson.GetBytes(body, "user_id").Str
489+
accessToken = gjson.GetBytes(body, "access_token").Str
490+
deviceID = gjson.GetBytes(body, "device_id").Str
491+
refreshToken = gjson.GetBytes(body, "refresh_token").Str
492+
expiresInMs = gjson.GetBytes(body, "expires_in_ms").Int()
493+
return userID, accessToken, refreshToken, deviceID, expiresInMs
494+
}
495+
496+
// RefreshToken will consume a refresh token and return a new access token and refresh token.
497+
func (c *CSAPI) ConsumeRefreshToken(t *testing.T, refreshToken string) (newAccessToken, newRefreshToken string, expiresInMs int64) {
498+
t.Helper()
499+
reqBody := map[string]interface{}{
500+
"refresh_token": refreshToken,
501+
}
502+
res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "refresh"}, WithJSONBody(t, reqBody))
503+
504+
body, err := ioutil.ReadAll(res.Body)
505+
if err != nil {
506+
t.Fatalf("unable to read response body: %v", err)
507+
}
508+
509+
newAccessToken = gjson.GetBytes(body, "access_token").Str
510+
newRefreshToken = gjson.GetBytes(body, "refresh_token").Str
511+
expiresInMs = gjson.GetBytes(body, "expires_in_ms").Int()
512+
return newAccessToken, newRefreshToken, expiresInMs
513+
}
514+
515+
// RegisterUser will register the user with given parameters and
442516
// return user ID & access token, and fail the test on network error
443517
func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID, accessToken, deviceID string) {
444518
t.Helper()

tests/csapi/txnid_scope_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package csapi_tests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/matrix-org/complement/internal/b"
7+
"github.com/matrix-org/complement/internal/client"
8+
"github.com/matrix-org/complement/runtime"
9+
"github.com/tidwall/gjson"
10+
)
11+
12+
func mustHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt {
13+
return client.SyncTimelineHas(roomID, func(r gjson.Result) bool {
14+
if r.Get("event_id").Str == eventID {
15+
if !r.Get("unsigned.transaction_id").Exists() {
16+
t.Fatalf("Event %s in room %s should have a 'transaction_id', but it did not", eventID, roomID)
17+
}
18+
19+
return true
20+
}
21+
22+
return false
23+
})
24+
}
25+
26+
func mustNotHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt {
27+
return client.SyncTimelineHas(roomID, func(r gjson.Result) bool {
28+
if r.Get("event_id").Str == eventID {
29+
res := r.Get("unsigned.transaction_id")
30+
if res.Exists() {
31+
t.Fatalf("Event %s in room %s should NOT have a 'transaction_id', but it did (%s)", eventID, roomID, res.Str)
32+
}
33+
34+
return true
35+
}
36+
37+
return false
38+
})
39+
}
40+
41+
// TestTxnAfterRefresh tests that when a client refreshes its access token,
42+
// it still gets back a transaction ID in the sync response.
43+
func TestTxnAfterRefresh(t *testing.T) {
44+
// Dendrite doesn't support refresh tokens yet.
45+
runtime.SkipIf(t, runtime.Dendrite)
46+
47+
deployment := Deploy(t, b.BlueprintCleanHS)
48+
defer deployment.Destroy(t)
49+
50+
deployment.RegisterUser(t, "hs1", "alice", "password", false)
51+
52+
c := deployment.Client(t, "hs1", "")
53+
54+
var refreshToken string
55+
c.UserID, c.AccessToken, refreshToken, c.DeviceID, _ = c.LoginUserWithRefreshToken(t, "alice", "password")
56+
57+
// Create a room where we can send events.
58+
roomID := c.CreateRoom(t, map[string]interface{}{})
59+
60+
// Let's send an event, and wait for it to appear in the sync.
61+
eventID := c.SendEventUnsynced(t, roomID, b.Event{
62+
Type: "m.room.message",
63+
Content: map[string]interface{}{
64+
"msgtype": "m.text",
65+
"body": "first",
66+
},
67+
})
68+
69+
// When syncing, we should find the event and it should have a transaction ID.
70+
token := c.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID))
71+
72+
// Now do the same, but refresh the token before syncing.
73+
eventID = c.SendEventUnsynced(t, roomID, b.Event{
74+
Type: "m.room.message",
75+
Content: map[string]interface{}{
76+
"msgtype": "m.text",
77+
"body": "second",
78+
},
79+
})
80+
81+
// Use the refresh token to get a new access token.
82+
c.AccessToken, refreshToken, _ = c.ConsumeRefreshToken(t, refreshToken)
83+
84+
// When syncing, we should find the event and it should also have a transaction ID.
85+
c.MustSyncUntil(t, client.SyncReq{Since: token}, mustHaveTransactionID(t, roomID, eventID))
86+
}
87+
88+
// TestTxnScope tests that transaction IDs are scoped to the access token, not the device
89+
func TestTxnScope(t *testing.T) {
90+
deployment := Deploy(t, b.BlueprintCleanHS)
91+
defer deployment.Destroy(t)
92+
93+
deployment.RegisterUser(t, "hs1", "alice", "password", false)
94+
95+
// Create a first client, which allocates a device ID.
96+
c1 := deployment.Client(t, "hs1", "")
97+
c1.UserID, c1.AccessToken, c1.DeviceID = c1.LoginUser(t, "alice", "password")
98+
99+
// Create a room where we can send events.
100+
roomID := c1.CreateRoom(t, map[string]interface{}{})
101+
102+
// Let's send an event, and wait for it to appear in the timeline.
103+
eventID := c1.SendEventUnsynced(t, roomID, b.Event{
104+
Type: "m.room.message",
105+
Content: map[string]interface{}{
106+
"msgtype": "m.text",
107+
"body": "first",
108+
},
109+
})
110+
111+
// When syncing, we should find the event and it should have a transaction ID on the first client.
112+
c1.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID))
113+
114+
// Create a second client, inheriting the first device ID.
115+
c2 := deployment.Client(t, "hs1", "")
116+
c2.UserID, c2.AccessToken = c2.LoginUserWithDeviceID(t, "alice", "password", c1.DeviceID)
117+
c2.DeviceID = c1.DeviceID
118+
119+
// When syncing, we should find the event and it should *not* have a transaction ID on the second client.
120+
c2.MustSyncUntil(t, client.SyncReq{}, mustNotHaveTransactionID(t, roomID, eventID))
121+
}

0 commit comments

Comments
 (0)