@@ -25,7 +25,7 @@ import { ConfigService } from '~/core';
25
25
import { Identity } from '~/core/authentication' ;
26
26
import { GlobalHttpHook , type IRequest } from '~/core/http' ;
27
27
import { rolesForScope } from '../authorization/dto' ;
28
- import { Anonymous } from './anonymous .decorator' ;
28
+ import { AuthLevel } from './auth-level .decorator' ;
29
29
import { AuthenticationService } from './authentication.service' ;
30
30
import { SessionHost } from './session.host' ;
31
31
@@ -65,56 +65,59 @@ export class SessionInterceptor implements NestInterceptor {
65
65
throw new Error ( 'Session holder for request is not in async context' ) ;
66
66
}
67
67
68
- const type = executionContext . getType ( ) ;
69
-
70
- let isMutation = true ;
71
- let session ;
72
- if ( type === 'graphql' ) {
73
- const gqlExecutionContext = GqlExecutionContext . create ( executionContext ) ;
74
- const op = gqlExecutionContext . getInfo ( ) . operation ;
75
- isMutation = op . operation === 'mutation' ;
76
- session = await this . handleGql ( executionContext ) ;
77
- } else if ( type === 'http' ) {
78
- const request = executionContext . switchToHttp ( ) . getRequest ( ) ;
79
- isMutation = request . method !== 'GET' && request . method !== 'HEAD' ;
80
- session = await this . handleHttp ( executionContext ) ;
68
+ const isMutation = this . isMutation ( executionContext ) ;
69
+ const authLevel =
70
+ AuthLevel . get ( executionContext . getHandler ( ) as FnLike ) ??
71
+ AuthLevel . get ( executionContext . getClass ( ) ) ??
72
+ ( isMutation ? 'authenticated' : 'anonymous' ) ;
73
+
74
+ if ( authLevel === 'sessionless' ) {
75
+ return next . handle ( ) ;
81
76
}
82
- session$ . next ( session ) ;
83
-
84
- const allowAnonymous =
85
- Anonymous . get ( executionContext . getHandler ( ) as FnLike ) ??
86
- Anonymous . get ( executionContext . getClass ( ) ) ??
87
- ! isMutation ;
88
- if ( ! allowAnonymous && session ) {
89
- this . identity . verifyLoggedIn ( ) ;
77
+
78
+ const session = await this . startFromContext ( executionContext ) ;
79
+ if ( session ) {
80
+ session$ . next ( session ) ;
81
+ if ( authLevel === 'authenticated' ) {
82
+ this . identity . verifyLoggedIn ( ) ;
83
+ }
90
84
}
91
85
92
86
return next . handle ( ) ;
93
87
}
94
88
95
- private async handleHttp ( executionContext : ExecutionContext ) {
96
- const enabled = Reflect . getMetadata (
97
- 'SESSION_WATERMARK' ,
98
- executionContext . getClass ( ) ,
99
- executionContext . getHandler ( ) . name ,
100
- ) ;
101
- if ( ! enabled ) {
102
- return ;
89
+ private isMutation ( executionContext : ExecutionContext ) {
90
+ switch ( executionContext . getType ( ) ) {
91
+ case 'graphql' : {
92
+ const gqlExecutionContext =
93
+ GqlExecutionContext . create ( executionContext ) ;
94
+ const op = gqlExecutionContext . getInfo ( ) . operation ;
95
+ return op . operation === 'mutation' ;
96
+ }
97
+ case 'http' : {
98
+ const request = executionContext . switchToHttp ( ) . getRequest ( ) ;
99
+ return request . method !== 'GET' && request . method !== 'HEAD' ;
100
+ }
101
+ default :
102
+ return undefined ;
103
103
}
104
- const request = executionContext . switchToHttp ( ) . getRequest ( ) ;
105
- return await this . hydrateSession ( { request } ) ;
106
104
}
107
105
108
- private async handleGql ( executionContext : ExecutionContext ) {
109
- const gqlExecutionContext = GqlExecutionContext . create ( executionContext ) ;
110
- const ctx = gqlExecutionContext . getContext ( ) ;
111
- const info = gqlExecutionContext . getInfo ( ) ;
112
-
113
- if ( info . fieldName !== 'session' ) {
114
- const session = await this . hydrateSession ( ctx ) ;
115
- return session ;
106
+ private async startFromContext ( executionContext : ExecutionContext ) {
107
+ switch ( executionContext . getType ( ) ) {
108
+ case 'graphql' : {
109
+ const gqlExecutionContext =
110
+ GqlExecutionContext . create ( executionContext ) ;
111
+ const ctx = gqlExecutionContext . getContext ( ) ;
112
+ return await this . hydrateSession ( ctx ) ;
113
+ }
114
+ case 'http' : {
115
+ const request = executionContext . switchToHttp ( ) . getRequest ( ) ;
116
+ return await this . hydrateSession ( { request } ) ;
117
+ }
118
+ default :
119
+ return undefined ;
116
120
}
117
- return undefined ;
118
121
}
119
122
120
123
async hydrateSession ( context : Pick < GqlContextType , 'request' > ) {
0 commit comments