1
- import * as http from 'http' ;
2
1
import * as crypto from 'crypto' ;
3
2
import * as argon2 from 'argon2' ;
3
+ import * as express from 'express' ;
4
4
import { ServerParsedArgs } from 'vs/server/node/args' ;
5
- import { serveError } from 'vs/server/node/http' ;
6
5
7
6
/** Ensures that the input is sanitized by checking
8
7
* - it's a string
@@ -15,51 +14,24 @@ export function sanitizeString(str: string): string {
15
14
return typeof str === 'string' && str . trim ( ) . length > 0 ? str . trim ( ) : '' ;
16
15
}
17
16
18
- export const ensureAuthenticated = async ( args : ServerParsedArgs , req : http . IncomingMessage , res : http . ServerResponse ) : Promise < boolean > => {
19
- const isAuthenticated = await authenticated ( args , req ) ;
20
- if ( ! isAuthenticated ) {
21
- serveError ( req , res , 401 , 'Unauthorized' ) ;
22
- }
23
- return isAuthenticated ;
24
- } ;
25
-
26
17
/**
27
18
* Return true if authenticated via cookies.
28
19
*/
29
- export const authenticated = async ( args : ServerParsedArgs , req : http . IncomingMessage ) : Promise < boolean > => {
20
+ export const authenticated = async ( args : ServerParsedArgs , req : express . Request ) : Promise < boolean > => {
30
21
if ( ! args . password && ! args . hashedPassword ) {
31
22
return true ;
32
23
}
33
24
const passwordMethod = getPasswordMethod ( args . hashedPassword ) ;
34
- const cookies = parseCookies ( req ) ;
35
25
const isCookieValidArgs : IsCookieValidArgs = {
36
26
passwordMethod,
37
- cookieKey : sanitizeString ( cookies . key ) ,
27
+ cookieKey : sanitizeString ( req . cookies . key ) ,
38
28
passwordFromArgs : args . password || '' ,
39
29
hashedPasswordFromArgs : args . hashedPassword ,
40
30
} ;
41
31
42
32
return await isCookieValid ( isCookieValidArgs ) ;
43
33
} ;
44
34
45
- function parseCookies ( request : http . IncomingMessage ) : Record < string , string > {
46
- const cookies : Record < string , string > = { } ,
47
- rc = request . headers . cookie ;
48
-
49
- // eslint-disable-next-line code-no-unused-expressions
50
- rc && rc . split ( ';' ) . forEach ( cookie => {
51
- let parts = cookie . split ( '=' ) ;
52
- if ( parts . length > 0 ) {
53
- const name = parts . shift ( ) ! . trim ( ) ;
54
- let value = decodeURI ( parts . join ( '=' ) ) ;
55
- value = value . substring ( 1 , value . length - 1 ) ;
56
- cookies [ name ] = value ;
57
- }
58
- } ) ;
59
-
60
- return cookies ;
61
- }
62
-
63
35
export type PasswordMethod = 'ARGON2' | 'PLAIN_TEXT' ;
64
36
65
37
/**
@@ -88,7 +60,7 @@ type HandlePasswordValidationArgs = {
88
60
/** The PasswordMethod */
89
61
passwordMethod : PasswordMethod
90
62
/** The password provided by the user */
91
- passwordFromRequestBody : string
63
+ passwordFromRequestBody : string | undefined
92
64
/** The password set in PASSWORD or config */
93
65
passwordFromArgs : string | undefined
94
66
/** The hashed-password set in HASHED_PASSWORD or config */
@@ -174,24 +146,26 @@ export async function handlePasswordValidation({
174
146
hashedPassword : '' ,
175
147
} ;
176
148
177
- switch ( passwordMethod ) {
178
- case 'PLAIN_TEXT' : {
179
- const isValid = passwordFromArgs ? safeCompare ( passwordFromRequestBody , passwordFromArgs ) : false ;
180
- passwordValidation . isPasswordValid = isValid ;
181
-
182
- const hashedPassword = await hash ( passwordFromRequestBody ) ;
183
- passwordValidation . hashedPassword = hashedPassword ;
184
- break ;
185
- }
186
- case 'ARGON2' : {
187
- const isValid = await isHashMatch ( passwordFromRequestBody , hashedPasswordFromArgs || '' ) ;
188
- passwordValidation . isPasswordValid = isValid ;
189
-
190
- passwordValidation . hashedPassword = hashedPasswordFromArgs || '' ;
191
- break ;
149
+ if ( passwordFromRequestBody ) {
150
+ switch ( passwordMethod ) {
151
+ case 'PLAIN_TEXT' : {
152
+ const isValid = passwordFromArgs ? safeCompare ( passwordFromRequestBody , passwordFromArgs ) : false ;
153
+ passwordValidation . isPasswordValid = isValid ;
154
+
155
+ const hashedPassword = await hash ( passwordFromRequestBody ) ;
156
+ passwordValidation . hashedPassword = hashedPassword ;
157
+ break ;
158
+ }
159
+ case 'ARGON2' : {
160
+ const isValid = await isHashMatch ( passwordFromRequestBody , hashedPasswordFromArgs || '' ) ;
161
+ passwordValidation . isPasswordValid = isValid ;
162
+
163
+ passwordValidation . hashedPassword = hashedPasswordFromArgs || '' ;
164
+ break ;
165
+ }
166
+ default :
167
+ break ;
192
168
}
193
- default :
194
- break ;
195
169
}
196
170
197
171
return passwordValidation ;
0 commit comments