Skip to content

Commit 3e6bd5d

Browse files
committed
Convert Session to a class
1 parent 2529613 commit 3e6bd5d

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

src/core/authentication/session/session.dto.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { type DateTime } from 'luxon';
2+
import { DataObject } from '~/common';
23
import { type ID } from '~/common/id-field';
34
import { type ScopedRole } from '../../../components/authorization/dto';
45

5-
export interface Session {
6+
class RawSession extends DataObject {
67
readonly token: string;
78
readonly issuedAt: DateTime;
89
readonly userId: ID;
@@ -21,3 +22,13 @@ export interface Session {
2122
roles: readonly ScopedRole[];
2223
};
2324
}
25+
26+
export class Session extends RawSession {
27+
static from(session: RawSession): Session {
28+
return Session.defaultValue(Session, session);
29+
}
30+
31+
with(next: Partial<RawSession>): Session {
32+
return Object.assign(Session.defaultValue(Session), this, next);
33+
}
34+
}

src/core/authentication/session/session.manager.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { AuthenticationRepository } from '../authentication.repository';
1717
import { CanImpersonateEvent } from '../events/can-impersonate.event';
1818
import { JwtService } from '../jwt.service';
1919
import { NoSessionException } from './no-session.exception';
20-
import { type Session } from './session.dto';
20+
import { Session } from './session.dto';
2121
import { SessionHost } from './session.host';
2222

2323
/**
@@ -86,22 +86,21 @@ export class SessionManager {
8686
}
8787
: undefined;
8888

89-
const requesterSession: Session = {
89+
const requesterSession = Session.from({
9090
token,
9191
issuedAt: DateTime.fromMillis(iat),
9292
userId: result.userId ?? anon.id,
9393
anonymous: !result.userId,
9494
roles: result.roles,
95-
};
95+
});
9696

97-
const session: Session = impersonatee
98-
? {
99-
...requesterSession,
97+
const session = impersonatee
98+
? requesterSession.with({
10099
userId: impersonatee?.id ?? requesterSession.userId,
101100
roles: impersonatee.roles,
102101
impersonator: requesterSession,
103102
impersonatee,
104-
}
103+
})
105104
: requesterSession;
106105

107106
if (impersonatee) {
@@ -133,14 +132,14 @@ export class SessionManager {
133132
return id;
134133
});
135134
const unresolvedId = 'unresolvedId' as ID;
136-
const session: Session = {
135+
const session = Session.from({
137136
token: 'system',
138137
issuedAt: DateTime.now(),
139138
userId: unresolvedId,
140139
anonymous: false,
141140
roles: ['global:Administrator'],
142141
...input,
143-
};
142+
});
144143
type LazySession = Session &
145144
Promise<Session> & { withRoles: (...roles: Role[]) => LazySession };
146145
return new Proxy(session, {
@@ -180,25 +179,25 @@ export class SessionManager {
180179
}
181180

182181
asRole<R>(role: Role, fn: () => R): R {
183-
const session: Session = {
182+
const session = Session.from({
184183
token: 'system',
185184
issuedAt: DateTime.now(),
186185
userId: 'anonymous' as ID,
187186
anonymous: false,
188187
roles: [`global:${role}`],
189-
};
188+
});
190189
return this.sessionHost.withSession(session, fn);
191190
}
192191

193192
async sessionForUser(userId: ID): Promise<Session> {
194193
const roles = await this.repo.rolesForUser(userId);
195-
const session: Session = {
194+
const session = Session.from({
196195
token: 'system',
197196
issuedAt: DateTime.now(),
198197
userId,
199198
anonymous: false,
200199
roles,
201-
};
200+
});
202201
return session;
203202
}
204203
}

0 commit comments

Comments
 (0)