@@ -47,20 +47,21 @@ export class SQLServer {
4747
4848 const contextByRequest = async ( request , session ) => {
4949 let userForContext = session . user ;
50+ let { securityContext } = session ;
5051
5152 if ( request . meta . changeUser && request . meta . changeUser !== session . user ) {
5253 const canSwitch = session . superuser || await canSwitchSqlUser ( session . user , request . meta . changeUser ) ;
5354 if ( canSwitch ) {
5455 userForContext = request . meta . changeUser ;
56+ const current = await checkSqlAuth ( request , userForContext , null ) ;
57+ securityContext = current . securityContext ;
5558 } else {
5659 throw new Error (
5760 `You cannot change security context via __user from ${ session . user } to ${ request . meta . changeUser } , because it's not allowed.`
5861 ) ;
5962 }
6063 }
61- // @todo Store security context in native for session's user, but not for switching
62- const current = await checkSqlAuth ( request , userForContext ) ;
63- return this . contextByNativeReq ( request , current . securityContext , request . id ) ;
64+ return this . contextByNativeReq ( request , securityContext , request . id ) ;
6465 } ;
6566
6667 const canSwitchUserForSession = async ( session , user ) => session . superuser || canSwitchSqlUser ( session . user , user ) ;
@@ -69,19 +70,19 @@ export class SQLServer {
6970 port : options . sqlPort ,
7071 pgPort : options . pgSqlPort ,
7172 nonce : options . sqlNonce ,
72- checkAuth : async ( { request, user } ) => {
73- const { password, superuser } = await checkSqlAuth ( request , user ) ;
73+ checkAuth : async ( { request, user, password } ) => {
74+ const { password : returnedPassword , superuser, securityContext , skipPasswordCheck } = await checkSqlAuth ( request , user , password ) ;
7475
7576 // Strip securityContext to improve speed deserialization
7677 return {
77- password,
78+ password : returnedPassword ,
7879 superuser : superuser || false ,
80+ securityContext,
81+ skipPasswordCheck,
7982 } ;
8083 } ,
8184 meta : async ( { request, session } ) => {
82- // @todo Store security context in native
83- const { securityContext } = await checkSqlAuth ( request , session . user ) ;
84- const context = await this . apiGateway . contextByReq ( < any > request , securityContext , request . id ) ;
85+ const context = await this . apiGateway . contextByReq ( < any > request , session . securityContext , request . id ) ;
8586
8687 // eslint-disable-next-line no-async-promise-executor
8788 return new Promise ( async ( resolve , reject ) => {
@@ -177,9 +178,7 @@ export class SQLServer {
177178 sqlGenerators : async ( paramsJson : string ) => {
178179 // TODO get rid of it
179180 const { request, session } = JSON . parse ( paramsJson ) ;
180- // @todo Store security context in native
181- const { securityContext } = await checkSqlAuth ( request , session . user ) ;
182- const context = await this . apiGateway . contextByReq ( < any > request , securityContext , request . id ) ;
181+ const context = await this . apiGateway . contextByReq ( < any > request , session . securityContext , request . id ) ;
183182
184183 // eslint-disable-next-line no-async-promise-executor
185184 return new Promise ( async ( resolve , reject ) => {
@@ -200,16 +199,12 @@ export class SQLServer {
200199 }
201200
202201 protected wrapCheckSqlAuthFn ( checkSqlAuth : CheckSQLAuthFn ) : CheckSQLAuthFn {
203- return async ( req , user ) => {
204- const response = await checkSqlAuth ( req , user ) ;
205- if ( typeof response !== 'object' || response . password === null ) {
202+ return async ( req , user , password ) => {
203+ const response = await checkSqlAuth ( req , user , password ) ;
204+ if ( typeof response !== 'object' ) {
206205 throw new Error ( 'checkSqlAuth must return an object' ) ;
207206 }
208207
209- if ( ! response . password ) {
210- throw new Error ( 'checkSqlAuth must return an object with password field' ) ;
211- }
212-
213208 return response ;
214209 } ;
215210 }
@@ -255,7 +250,8 @@ export class SQLServer {
255250
256251 return {
257252 password : allowedPassword ,
258- securityContext : { }
253+ securityContext : { } ,
254+ skipPasswordCheck : getEnv ( 'devMode' ) && ! allowedPassword
259255 } ;
260256 } ;
261257 }
0 commit comments