Skip to content

Commit

Permalink
realm: Add session room property to realm JWTs (#2058)
Browse files Browse the repository at this point in the history
  • Loading branch information
backspace authored Jan 20, 2025
1 parent 8ea6f45 commit fc4cc23
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/matrix/tests/login.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ test.describe('Login', () => {
let claims = jwt.verify(token, REALM_SECRET_SEED) as {
user: string;
realm: string;
sessionRoom: string;
permissions: ('read' | 'write' | 'realm-owner')[];
};
expect(claims.user).toStrictEqual('@user1:localhost');
expect(claims.realm).toStrictEqual(`${appURL}/`);
expect(claims.sessionRoom).toMatch(/!\w*:localhost/);
expect(claims.permissions).toMatchObject(['read', 'write']);

// reload to page to show that the access token persists
Expand Down
10 changes: 9 additions & 1 deletion packages/realm-server/tests/realm-server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ let createJWT = (
user: string,
permissions: RealmPermissions['user'] = [],
) => {
return realm.createJWT({ user, realm: realm.url, permissions }, '7d');
return realm.createJWT(
{
user,
realm: realm.url,
permissions,
sessionRoom: `test-session-room-for-${user}`,
},
'7d',
);
};

module(basename(__filename), function () {
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-common/realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface FileRef {
export interface TokenClaims {
user: string;
realm: string;
sessionRoom: string;
permissions: RealmPermissions['user'];
}

Expand Down Expand Up @@ -654,7 +655,7 @@ export class Realm {
requestContext,
});
},
createJWT: async (user: string) => {
createJWT: async (user: string, sessionRoom: string) => {
let permissions = requestContext.permissions;

let userPermissions = await new RealmPermissionChecker(
Expand All @@ -666,6 +667,7 @@ export class Realm {
{
user,
realm: this.url,
sessionRoom,
permissions: userPermissions,
},
'7d',
Expand Down

0 comments on commit fc4cc23

Please sign in to comment.