3
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
- import * as randomBytes from 'randombytes' ;
7
- import * as querystring from 'querystring' ;
8
- import { Buffer } from 'buffer' ;
9
6
import * as vscode from 'vscode' ;
10
- import { v4 as uuid } from 'uuid ' ;
11
- import fetch , { Response } from 'node-fetch' ;
7
+ import * as querystring from 'querystring ' ;
8
+ import path = require ( 'path' ) ;
12
9
import Logger from './logger' ;
13
- import { isSupportedEnvironment , toBase64UrlEncoding } from './utils' ;
14
- import { sha256 } from './env/node/sha256 ' ;
10
+ import { isSupportedEnvironment } from './utils' ;
11
+ import { generateCodeChallenge , generateCodeVerifier , randomUUID } from './cryptoUtils ' ;
15
12
import { BetterTokenStorage , IDidChangeInOtherWindowEvent } from './betterSecretStorage' ;
16
- import { LoopbackAuthServer } from './authServer' ;
17
- import path = require( 'path' ) ;
13
+ import { LoopbackAuthServer } from './node/authServer' ;
14
+ import { base64Decode } from './node/buffer' ;
15
+ import { fetching } from './node/fetch' ;
18
16
19
17
const redirectUrl = 'https://vscode.dev/redirect' ;
20
18
const loginEndpointUrl = 'https://login.microsoftonline.com/' ;
@@ -295,8 +293,8 @@ export class AzureActiveDirectoryService {
295
293
}
296
294
297
295
private async createSessionWithLocalServer ( scopeData : IScopeData ) {
298
- const codeVerifier = toBase64UrlEncoding ( randomBytes ( 32 ) . toString ( 'base64' ) ) ;
299
- const codeChallenge = toBase64UrlEncoding ( await sha256 ( codeVerifier ) ) ;
296
+ const codeVerifier = generateCodeVerifier ( ) ;
297
+ const codeChallenge = await generateCodeChallenge ( codeVerifier ) ;
300
298
const qs = new URLSearchParams ( {
301
299
response_type : 'code' ,
302
300
response_mode : 'query' ,
@@ -328,15 +326,15 @@ export class AzureActiveDirectoryService {
328
326
329
327
private async createSessionWithoutLocalServer ( scopeData : IScopeData ) : Promise < vscode . AuthenticationSession > {
330
328
let callbackUri = await vscode . env . asExternalUri ( vscode . Uri . parse ( `${ vscode . env . uriScheme } ://vscode.microsoft-authentication` ) ) ;
331
- const nonce = randomBytes ( 16 ) . toString ( 'base64' ) ;
329
+ const nonce = generateCodeVerifier ( ) ;
332
330
const callbackQuery = new URLSearchParams ( callbackUri . query ) ;
333
331
callbackQuery . set ( 'nonce' , encodeURIComponent ( nonce ) ) ;
334
332
callbackUri = callbackUri . with ( {
335
333
query : callbackQuery . toString ( )
336
334
} ) ;
337
335
const state = encodeURIComponent ( callbackUri . toString ( true ) ) ;
338
- const codeVerifier = toBase64UrlEncoding ( randomBytes ( 32 ) . toString ( 'base64' ) ) ;
339
- const codeChallenge = toBase64UrlEncoding ( await sha256 ( codeVerifier ) ) ;
336
+ const codeVerifier = generateCodeVerifier ( ) ;
337
+ const codeChallenge = await generateCodeChallenge ( codeVerifier ) ;
340
338
const signInUrl = `${ loginEndpointUrl } ${ scopeData . tenant } /oauth2/v2.0/authorize` ;
341
339
const oauthStartQuery = new URLSearchParams ( {
342
340
response_type : 'code' ,
@@ -467,10 +465,10 @@ export class AzureActiveDirectoryService {
467
465
468
466
try {
469
467
if ( json . id_token ) {
470
- claims = JSON . parse ( Buffer . from ( json . id_token . split ( '.' ) [ 1 ] , 'base64' ) . toString ( ) ) ;
468
+ claims = JSON . parse ( base64Decode ( json . id_token . split ( '.' ) [ 1 ] ) ) ;
471
469
} else {
472
470
Logger . info ( 'Attempting to parse access_token instead since no id_token was included in the response.' ) ;
473
- claims = JSON . parse ( Buffer . from ( json . access_token . split ( '.' ) [ 1 ] , 'base64' ) . toString ( ) ) ;
471
+ claims = JSON . parse ( base64Decode ( json . access_token . split ( '.' ) [ 1 ] ) ) ;
474
472
}
475
473
} catch ( e ) {
476
474
throw e ;
@@ -491,7 +489,7 @@ export class AzureActiveDirectoryService {
491
489
idToken : json . id_token ,
492
490
refreshToken : json . refresh_token ,
493
491
scope : scopeData . scopeStr ,
494
- sessionId : existingId || `${ id } /${ uuid ( ) } ` ,
492
+ sessionId : existingId || `${ id } /${ randomUUID ( ) } ` ,
495
493
account : {
496
494
label,
497
495
id
@@ -739,10 +737,10 @@ export class AzureActiveDirectoryService {
739
737
let attempts = 0 ;
740
738
while ( attempts <= 3 ) {
741
739
attempts ++ ;
742
- let result : Response | undefined ;
740
+ let result ;
743
741
let errorMessage : string | undefined ;
744
742
try {
745
- result = await fetch ( endpoint , {
743
+ result = await fetching ( endpoint , {
746
744
method : 'POST' ,
747
745
headers : {
748
746
'Content-Type' : 'application/x-www-form-urlencoded' ,
0 commit comments