@@ -7,7 +7,6 @@ import { DateTime } from 'luxon';
7
7
import type { Writable } from 'ts-essentials' ;
8
8
import {
9
9
DuplicateException ,
10
- type GqlContextType ,
11
10
type ID ,
12
11
InputException ,
13
12
type Role ,
@@ -16,7 +15,6 @@ import {
16
15
UnauthenticatedException ,
17
16
UnauthorizedException ,
18
17
} from '~/common' ;
19
- import { sessionFromContext } from '~/common/session' ;
20
18
import { ConfigService , ILogger , Logger } from '~/core' ;
21
19
import { ForgotPassword } from '~/core/email/templates' ;
22
20
import { disableAccessPolicies , Gel } from '~/core/gel' ;
@@ -28,6 +26,7 @@ import { AuthenticationRepository } from './authentication.repository';
28
26
import { CryptoService } from './crypto.service' ;
29
27
import type { LoginInput , RegisterInput , ResetPasswordInput } from './dto' ;
30
28
import { NoSessionException } from './no-session.exception' ;
29
+ import { SessionHost } from './session.host' ;
31
30
32
31
interface JwtPayload {
33
32
iat : number ;
@@ -44,6 +43,7 @@ export class AuthenticationService {
44
43
private readonly repo : AuthenticationRepository ,
45
44
private readonly gel : Gel ,
46
45
private readonly agents : SystemAgentRepository ,
46
+ private readonly sessionHost : SessionHost ,
47
47
private readonly moduleRef : ModuleRef ,
48
48
) { }
49
49
@@ -101,10 +101,10 @@ export class AuthenticationService {
101
101
return userId ;
102
102
}
103
103
104
- async updateSession ( context : GqlContextType ) {
105
- const prev = sessionFromContext ( context ) ;
104
+ async refreshCurrentSession ( ) {
105
+ const prev = this . sessionHost . current ;
106
106
const newSession = await this . resumeSession ( prev . token ) ;
107
- context . session $. next ( newSession ) ;
107
+ this . sessionHost . current $. next ( newSession ) ;
108
108
return newSession ;
109
109
}
110
110
@@ -169,10 +169,12 @@ export class AuthenticationService {
169
169
: requesterSession ;
170
170
171
171
if ( impersonatee ) {
172
- const p = this . privileges . for ( requesterSession , AssignableRoles ) ;
173
- const valid = impersonatee . roles . every ( ( role ) =>
174
- p . can ( 'edit' , withoutScope ( role ) ) ,
175
- ) ;
172
+ const valid = this . sessionHost . withSession ( requesterSession , ( ) => {
173
+ const p = this . privileges . for ( AssignableRoles ) ;
174
+ return impersonatee . roles . every ( ( role ) =>
175
+ p . can ( 'edit' , withoutScope ( role ) ) ,
176
+ ) ;
177
+ } ) ;
176
178
if ( ! valid ) {
177
179
// Don't expose what the requester is unable to do as this could leak
178
180
// private information.
@@ -230,6 +232,15 @@ export class AuthenticationService {
230
232
return this . repo . waitForRootUserId ( ) ;
231
233
}
232
234
235
+ async asUser < R > (
236
+ user : ID < 'User' > | Session ,
237
+ fn : ( session : Session ) => Promise < R > ,
238
+ ) : Promise < R > {
239
+ const session =
240
+ typeof user === 'string' ? await this . sessionForUser ( user ) : user ;
241
+ return await this . sessionHost . withSession ( session , ( ) => fn ( session ) ) ;
242
+ }
243
+
233
244
async sessionForUser ( userId : ID ) : Promise < Session > {
234
245
const roles = await this . repo . rolesForUser ( userId ) ;
235
246
const session : Session = {
0 commit comments