Skip to content

Commit 6451797

Browse files
authored
Add DeviceID to client.CSAPI (#374)
* Add DeviceID to client.CSAPI * Don't fail if DeviceID could not be found
1 parent 915311e commit 6451797

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

internal/client/client.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type SyncReq struct {
7272
type CSAPI struct {
7373
UserID string
7474
AccessToken string
75+
DeviceID string
7576
BaseURL string
7677
Client *http.Client
7778
// how long are we willing to wait for MustSyncUntil.... calls
@@ -298,7 +299,7 @@ func (c *CSAPI) MustSyncUntil(t *testing.T, syncReq SyncReq, checks ...SyncCheck
298299

299300
//RegisterUser will register the user with given parameters and
300301
// return user ID & access token, and fail the test on network error
301-
func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID, accessToken string) {
302+
func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID, accessToken, deviceID string) {
302303
t.Helper()
303304
reqBody := map[string]interface{}{
304305
"auth": map[string]string{
@@ -316,12 +317,13 @@ func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID,
316317

317318
userID = gjson.GetBytes(body, "user_id").Str
318319
accessToken = gjson.GetBytes(body, "access_token").Str
319-
return userID, accessToken
320+
deviceID = gjson.GetBytes(body, "device_id").Str
321+
return userID, accessToken, deviceID
320322
}
321323

322324
// RegisterSharedSecret registers a new account with a shared secret via HMAC
323325
// See https://github.com/matrix-org/synapse/blob/e550ab17adc8dd3c48daf7fedcd09418a73f524b/synapse/_scripts/register_new_matrix_user.py#L40
324-
func (c *CSAPI) RegisterSharedSecret(t *testing.T, user, pass string, isAdmin bool) (userID, password string) {
326+
func (c *CSAPI) RegisterSharedSecret(t *testing.T, user, pass string, isAdmin bool) (userID, accessToken, deviceID string) {
325327
resp := c.DoFunc(t, "GET", []string{"_synapse", "admin", "v1", "register"})
326328
if resp.StatusCode != 200 {
327329
t.Skipf("Homeserver image does not support shared secret registration, /_synapse/admin/v1/register returned HTTP %d", resp.StatusCode)
@@ -354,7 +356,10 @@ func (c *CSAPI) RegisterSharedSecret(t *testing.T, user, pass string, isAdmin bo
354356
}
355357
resp = c.MustDoFunc(t, "POST", []string{"_synapse", "admin", "v1", "register"}, WithJSONBody(t, reqBody))
356358
body = must.ParseJSON(t, resp.Body)
357-
return gjson.GetBytes(body, "user_id").Str, gjson.GetBytes(body, "access_token").Str
359+
userID = gjson.GetBytes(body, "user_id").Str
360+
accessToken = gjson.GetBytes(body, "access_token").Str
361+
deviceID = gjson.GetBytes(body, "device_id").Str
362+
return userID, accessToken, deviceID
358363
}
359364

360365
// GetCapbabilities queries the server's capabilities

internal/docker/builder.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ func (d *Builder) construct(bprint b.Blueprint) (errs []error) {
304304
}
305305
}
306306

307+
deviceIDs := runner.DeviceIDs(res.homeserver.Name)
308+
for userID, deviceID := range deviceIDs {
309+
labels["device_id"+userID] = deviceID
310+
}
311+
307312
// Combine the labels for tokens and application services
308313
asLabels := labelsForApplicationServices(res.homeserver)
309314
for k, v := range asLabels {

internal/docker/deployer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ func deployImage(
369369
ContainerID: containerID,
370370
AccessTokens: tokensFromLabels(inspect.Config.Labels),
371371
ApplicationServices: asIDToRegistrationFromLabels(inspect.Config.Labels),
372+
DeviceIDs: deviceIDsFromLabels(inspect.Config.Labels),
372373
}
373374
if lastErr != nil {
374375
return d, fmt.Errorf("%s: failed to check server is up. %w", contextStr, lastErr)

internal/docker/deployment.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type HomeserverDeployment struct {
2727
ContainerID string // e.g 10de45efba
2828
AccessTokens map[string]string // e.g { "@alice:hs1": "myAcc3ssT0ken" }
2929
ApplicationServices map[string]string // e.g { "my-as-id": "id: xxx\nas_token: xxx ..."} }
30+
DeviceIDs map[string]string // e.g { "@alice:hs1": "myDeviceID" }
3031
}
3132

3233
// Destroy the entire deployment. Destroys all running containers. If `printServerLogs` is true,
@@ -51,9 +52,14 @@ func (d *Deployment) Client(t *testing.T, hsName, userID string) *client.CSAPI {
5152
t.Fatalf("Deployment.Client - HS name '%s' - user ID '%s' not found", hsName, userID)
5253
return nil
5354
}
55+
deviceID := dep.DeviceIDs[userID]
56+
if deviceID == "" && userID != "" {
57+
t.Logf("WARNING: Deployment.Client - HS name '%s' - user ID '%s' - deviceID not found", hsName, userID)
58+
}
5459
return &client.CSAPI{
5560
UserID: userID,
5661
AccessToken: token,
62+
DeviceID: deviceID,
5763
BaseURL: dep.BaseURL,
5864
Client: client.NewLoggedClient(t, hsName, nil),
5965
SyncUntilTimeout: 5 * time.Second,
@@ -75,17 +81,18 @@ func (d *Deployment) RegisterUser(t *testing.T, hsName, localpart, password stri
7581
SyncUntilTimeout: 5 * time.Second,
7682
Debug: d.Deployer.debugLogging,
7783
}
78-
var userID, accessToken string
84+
var userID, accessToken, deviceID string
7985
if isAdmin {
80-
userID, accessToken = client.RegisterSharedSecret(t, localpart, password, isAdmin)
86+
userID, accessToken, deviceID = client.RegisterSharedSecret(t, localpart, password, isAdmin)
8187
} else {
82-
userID, accessToken = client.RegisterUser(t, localpart, password)
88+
userID, accessToken, deviceID = client.RegisterUser(t, localpart, password)
8389
}
8490

8591
// remember the token so subsequent calls to deployment.Client return the user
8692
dep.AccessTokens[userID] = accessToken
8793

8894
client.UserID = userID
8995
client.AccessToken = accessToken
96+
client.DeviceID = deviceID
9097
return client
9198
}

internal/docker/labels.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,13 @@ func labelsForApplicationServices(hs b.Homeserver) map[string]string {
5050
}
5151
return labels
5252
}
53+
54+
func deviceIDsFromLabels(labels map[string]string) map[string]string {
55+
userIDToToken := make(map[string]string)
56+
for k, v := range labels {
57+
if strings.HasPrefix(k, "device_id") {
58+
userIDToToken[strings.TrimPrefix(k, "device_id")] = v
59+
}
60+
}
61+
return userIDToToken
62+
}

internal/instruction/runner.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ func (r *Runner) AccessTokens(hsDomain string) map[string]string {
100100
return res
101101
}
102102

103+
// DeviceIDs returns the device ids for all users who were created on the given HS domain.
104+
// Returns a map of user_id => device_id
105+
func (r *Runner) DeviceIDs(hsDomain string) map[string]string {
106+
res := make(map[string]string)
107+
r.lookup.Range(func(k, v interface{}) bool {
108+
key := k.(string)
109+
val := v.(string)
110+
if strings.HasPrefix(key, "device_@") && strings.HasSuffix(key, ":"+hsDomain) {
111+
res[strings.TrimPrefix(key, "device_")] = val
112+
}
113+
return true
114+
})
115+
return res
116+
}
117+
103118
// Load a previously stored value from RunInstructions
104119
func (r *Runner) GetStoredValue(opts RunOpts, key string) string {
105120
fullKey := opts.StoreNamespace + key
@@ -541,7 +556,8 @@ func instructionRegister(hs b.Homeserver, user b.User) instruction {
541556
accessToken: "",
542557
body: body,
543558
storeResponse: map[string]string{
544-
"user_@" + user.Localpart + ":" + hs.Name: ".access_token",
559+
"user_@" + user.Localpart + ":" + hs.Name: ".access_token",
560+
"device_@" + user.Localpart + ":" + hs.Name: ".device_id",
545561
},
546562
}
547563
}
@@ -581,7 +597,8 @@ func instructionLogin(hs b.Homeserver, user b.User) instruction {
581597
accessToken: "",
582598
body: body,
583599
storeResponse: map[string]string{
584-
"user_@" + user.Localpart + ":" + hs.Name: ".access_token",
600+
"user_@" + user.Localpart + ":" + hs.Name: ".access_token",
601+
"device_@" + user.Localpart + ":" + hs.Name: ".device_id",
585602
},
586603
}
587604
}

0 commit comments

Comments
 (0)