1- import * as http from 'http' ;
21import * as crypto from 'crypto' ;
32import * as argon2 from 'argon2' ;
3+ import * as express from 'express' ;
44import { ServerParsedArgs } from 'vs/server/node/args' ;
5- import { serveError } from 'vs/server/node/http' ;
65
76/** Ensures that the input is sanitized by checking
87 * - it's a string
@@ -15,51 +14,24 @@ export function sanitizeString(str: string): string {
1514 return typeof str === 'string' && str . trim ( ) . length > 0 ? str . trim ( ) : '' ;
1615}
1716
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-
2617/**
2718 * Return true if authenticated via cookies.
2819 */
29- export const authenticated = async ( args : ServerParsedArgs , req : http . IncomingMessage ) : Promise < boolean > => {
20+ export const authenticated = async ( args : ServerParsedArgs , req : express . Request ) : Promise < boolean > => {
3021 if ( ! args . password && ! args . hashedPassword ) {
3122 return true ;
3223 }
3324 const passwordMethod = getPasswordMethod ( args . hashedPassword ) ;
34- const cookies = parseCookies ( req ) ;
3525 const isCookieValidArgs : IsCookieValidArgs = {
3626 passwordMethod,
37- cookieKey : sanitizeString ( cookies . key ) ,
27+ cookieKey : sanitizeString ( req . cookies . key ) ,
3828 passwordFromArgs : args . password || '' ,
3929 hashedPasswordFromArgs : args . hashedPassword ,
4030 } ;
4131
4232 return await isCookieValid ( isCookieValidArgs ) ;
4333} ;
4434
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-
6335export type PasswordMethod = 'ARGON2' | 'PLAIN_TEXT' ;
6436
6537/**
@@ -88,7 +60,7 @@ type HandlePasswordValidationArgs = {
8860 /** The PasswordMethod */
8961 passwordMethod : PasswordMethod
9062 /** The password provided by the user */
91- passwordFromRequestBody : string
63+ passwordFromRequestBody : string | undefined
9264 /** The password set in PASSWORD or config */
9365 passwordFromArgs : string | undefined
9466 /** The hashed-password set in HASHED_PASSWORD or config */
@@ -174,24 +146,26 @@ export async function handlePasswordValidation({
174146 hashedPassword : '' ,
175147 } ;
176148
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 ;
192168 }
193- default :
194- break ;
195169 }
196170
197171 return passwordValidation ;
0 commit comments