@@ -80,25 +80,26 @@ class Client {
80
80
const { req, res } = this ;
81
81
const { url } = req ;
82
82
const ip = req . connection . remoteAddress ;
83
- const method = url . substring ( METHOD_OFFSET ) ;
83
+ const name = url . substring ( METHOD_OFFSET ) ;
84
84
const session = await this . application . auth . restore ( req ) ;
85
- if ( ! session && method !== 'signIn' ) {
86
- this . application . logger . error ( `Forbidden ${ url } ` ) ;
87
- this . error ( 403 ) ;
88
- semaphore . leave ( ) ;
89
- return ;
90
- }
91
85
const args = await receiveArgs ( req ) ;
92
86
const sandbox = session ? session . sandbox : undefined ;
93
87
const context = session ? session . context : { } ;
94
88
try {
95
- const proc = this . application . runScript ( method , sandbox ) ;
96
- const result = await proc ( context ) ( args ) ;
89
+ const exp = this . application . runScript ( name , sandbox ) ;
90
+ const { method, access } = exp ( context ) ;
91
+ if ( ! session && access !== 'public' ) {
92
+ this . application . logger . error ( `Forbidden ${ url } ` ) ;
93
+ this . error ( 403 ) ;
94
+ semaphore . leave ( ) ;
95
+ return ;
96
+ }
97
+ const result = await method ( args ) ;
97
98
if ( res . finished ) {
98
99
semaphore . leave ( ) ;
99
100
return ;
100
101
}
101
- if ( method === 'signIn ' ) {
102
+ if ( ! session && access === 'public ' ) {
102
103
const session = this . application . auth . start ( req , ip , result . userId ) ;
103
104
res . setHeader ( 'Set-Cookie' , session . cookie ) ;
104
105
}
@@ -151,19 +152,20 @@ const apiws = (application, connection, req) => async message => {
151
152
}
152
153
try {
153
154
const ip = req . connection . remoteAddress ;
154
- const { method, args } = JSON . parse ( message ) ;
155
+ const { method : name , args } = JSON . parse ( message ) ;
155
156
const session = await application . auth . restore ( req ) ;
156
- if ( ! session && method !== 'signIn' ) {
157
- application . logger . error ( `Forbidden: ${ method } ` ) ;
157
+ const sandbox = session ? session . sandbox : undefined ;
158
+ const context = session ? session . context : { } ;
159
+ const exp = application . runScript ( name , sandbox ) ;
160
+ const { method, access } = exp ( context ) ;
161
+ if ( ! session && access !== 'public' ) {
162
+ application . logger . error ( `Forbidden: ${ name } ` ) ;
158
163
send ( { result : 'error' , reason : 'forbidden' } ) ;
159
164
semaphore . leave ( ) ;
160
165
return ;
161
166
}
162
- const sandbox = session ? session . sandbox : undefined ;
163
- const context = session ? session . context : { } ;
164
- const proc = application . runScript ( method , sandbox ) ;
165
- const result = await proc ( context ) ( args ) ;
166
- if ( method === 'signIn' ) {
167
+ const result = await method ( args ) ;
168
+ if ( ! session && access === 'public' ) {
167
169
const session = application . auth . start ( req , ip , result . userId ) ;
168
170
result . token = session . cookie ;
169
171
}
0 commit comments