@@ -80,6 +80,50 @@ describe('Authentication Handlers Tests', function () {
80
80
assert ( ! asJson . success , "success = false; Authentication should fail" ) ;
81
81
} ) ;
82
82
83
+ it ( '[Basic Auth] - does redirection request with basic auth' , async ( ) => {
84
+ const url : string = 'http://microsoft.com' ;
85
+ const redirectionUrl : string = 'http://jfrog.com' ;
86
+ const user : string = _authHandlersOptions . basicAuth . username ;
87
+ const pass : string = _authHandlersOptions . basicAuth . password ;
88
+
89
+ //Set nock for redirection with credentials
90
+ const redirectAuthScope = nock ( url )
91
+ . get ( '/' )
92
+ . basicAuth ( { user, pass } )
93
+ . reply ( httpm . HttpCodes . MovedPermanently , undefined , {
94
+ location : redirectionUrl
95
+ } ) ;
96
+
97
+ //Set nock for request without expecting/matching Authorization header(s)
98
+ nock ( redirectionUrl )
99
+ . matchHeader ( 'authorization' , ( val : string | undefined ) => ! val )
100
+ . get ( '/' )
101
+ . reply ( httpm . HttpCodes . OK , {
102
+ success : true ,
103
+ source : "nock"
104
+ } ) ;
105
+
106
+ //Set nock for request with expecting/matching Authorization header(s)
107
+ nock ( redirectionUrl )
108
+ . matchHeader ( 'authorization' , ( val : string | undefined ) => val )
109
+ . get ( '/' )
110
+ . reply ( httpm . HttpCodes . BadRequest , {
111
+ success : false ,
112
+ source : "nock"
113
+ } ) ;
114
+
115
+ const basicAuthHandler : hm . BasicCredentialHandler = new hm . BasicCredentialHandler ( user , pass ) ;
116
+ let httpClient : httpm . HttpClient = new httpm . HttpClient ( 'typed-rest-client-tests' , [ basicAuthHandler ] ) ;
117
+ let httpResponse : httpm . HttpClientResponse = await httpClient . get ( url ) ;
118
+ let body : string = await httpResponse . readBody ( ) ;
119
+ let asJson : any = JSON . parse ( body ) ;
120
+
121
+ assert ( redirectAuthScope . isDone ( ) ) ;
122
+ assert ( httpResponse . message . statusCode == httpm . HttpCodes . OK , "status code should be 200 - OK" ) ;
123
+ assert ( asJson . source === "nock" , "http get request should be intercepted by nock" ) ;
124
+ assert ( asJson . success , "Authentication should not occur in redirection to other hosts" ) ;
125
+ } ) ;
126
+
83
127
it ( '[Basic Auth - Presigned] doesnt use auth when presigned' , async ( ) => {
84
128
const url : string = 'http://microsoft.com' ;
85
129
const user : string = _authHandlersOptions . basicAuth . username ;
@@ -165,6 +209,53 @@ describe('Authentication Handlers Tests', function () {
165
209
assert ( ! asJson . success , "success = false; Authentication should fail" ) ;
166
210
} ) ;
167
211
212
+ it ( '[Personal Access Token] - does redirection request with PAT token auth' , async ( ) => {
213
+ const url : string = 'http://microsoft.com' ;
214
+ const redirectionUrl : string = 'http://jfrog.com' ;
215
+ const secret : string = _authHandlersOptions . personalAccessToken . secret ;
216
+ const personalAccessToken : string = Buffer . from ( `PAT:${ secret } ` ) . toString ( 'base64' ) ;
217
+ const expectedAuthHeader : string = `Basic ${ personalAccessToken } ` ;
218
+ const patAuthHandler : hm . PersonalAccessTokenCredentialHandler =
219
+ new hm . PersonalAccessTokenCredentialHandler ( secret ) ;
220
+
221
+ //Nock request for redirection with expecting/matching Authorization header(s)
222
+ const redirectAuthScope = nock ( url )
223
+ . matchHeader ( 'Authorization' , expectedAuthHeader )
224
+ . matchHeader ( 'X-TFS-FedAuthRedirect' , 'Suppress' )
225
+ . get ( '/' )
226
+ . reply ( httpm . HttpCodes . MovedPermanently , undefined , {
227
+ location : redirectionUrl
228
+ } ) ;
229
+
230
+ //Set nock for request without expecting/matching Authorization header(s)
231
+ nock ( redirectionUrl )
232
+ . matchHeader ( 'authorization' , ( val : string | undefined ) => ! val )
233
+ . get ( '/' )
234
+ . reply ( httpm . HttpCodes . OK , {
235
+ success : true ,
236
+ source : "nock"
237
+ } ) ;
238
+
239
+ //Set nock for request with expecting/matching Authorization header(s)
240
+ nock ( redirectionUrl )
241
+ . matchHeader ( 'authorization' , ( val : string | undefined ) => val )
242
+ . get ( '/' )
243
+ . reply ( httpm . HttpCodes . BadRequest , {
244
+ success : false ,
245
+ source : "nock"
246
+ } ) ;
247
+
248
+ let httpClient : httpm . HttpClient = new httpm . HttpClient ( 'typed-rest-client-tests' , [ patAuthHandler ] ) ;
249
+ let httpResponse : httpm . HttpClientResponse = await httpClient . get ( url ) ;
250
+ let body : string = await httpResponse . readBody ( ) ;
251
+ let asJson : any = JSON . parse ( body ) ;
252
+
253
+ assert ( redirectAuthScope . isDone ( ) ) ;
254
+ assert ( httpResponse . message . statusCode == httpm . HttpCodes . OK , "status code should be 200 - OK" ) ;
255
+ assert ( asJson . source === "nock" , "http get request should be intercepted by nock" ) ;
256
+ assert ( asJson . success , "Authentication should not occur in redirection to other hosts" ) ;
257
+ } ) ;
258
+
168
259
it ( '[Bearer Token] - does basic http get request with bearer token authentication' , async ( ) => {
169
260
const url : string = 'http://microsoft.com' ;
170
261
const bearerToken : string = _authHandlersOptions . bearer . token ;
@@ -216,6 +307,52 @@ describe('Authentication Handlers Tests', function () {
216
307
assert ( httpResponse . message . statusCode === httpm . HttpCodes . Unauthorized , "statusCode returned should be 401 - Unauthorized" ) ; //statusCode is 401 - Unauthorized
217
308
} ) ;
218
309
310
+ it ( '[Bearer Token] - does redirection request with bearer token authentication' , async ( ) => {
311
+ const url : string = 'http://microsoft.com' ;
312
+ const redirectionUrl : string = 'http://jfrog.com' ;
313
+ const bearerToken : string = _authHandlersOptions . bearer . token ;
314
+
315
+ const expectedAuthHeader : string = `Bearer ${ bearerToken } ` ;
316
+ const bearerTokenAuthHandler : hm . BearerCredentialHandler = new hm . BearerCredentialHandler ( bearerToken ) ;
317
+
318
+ //Nock request for redirection with expecting/matching Authorization header(s)
319
+ const redirectAuthScope = nock ( url )
320
+ . matchHeader ( 'Authorization' , expectedAuthHeader )
321
+ . matchHeader ( 'X-TFS-FedAuthRedirect' , 'Suppress' )
322
+ . get ( '/' )
323
+ . reply ( httpm . HttpCodes . MovedPermanently , undefined , {
324
+ location : redirectionUrl
325
+ } ) ;
326
+
327
+ //Set nock for request without expecting/matching Authorization header(s)
328
+ nock ( redirectionUrl )
329
+ . matchHeader ( 'authorization' , ( val : string | undefined ) => ! val )
330
+ . get ( '/' )
331
+ . reply ( httpm . HttpCodes . OK , {
332
+ success : true ,
333
+ source : "nock"
334
+ } ) ;
335
+
336
+ //Set nock for request with expecting/matching Authorization header(s)
337
+ nock ( redirectionUrl )
338
+ . matchHeader ( 'authorization' , ( val : string | undefined ) => val )
339
+ . get ( '/' )
340
+ . reply ( httpm . HttpCodes . BadRequest , {
341
+ success : false ,
342
+ source : "nock"
343
+ } ) ;
344
+
345
+ let httpClient : httpm . HttpClient = new httpm . HttpClient ( 'typed-rest-client-tests' , [ bearerTokenAuthHandler ] ) ;
346
+ let httpResponse : httpm . HttpClientResponse = await httpClient . get ( url ) ;
347
+ let body : string = await httpResponse . readBody ( ) ;
348
+ let asJson : any = JSON . parse ( body ) ;
349
+
350
+ assert ( redirectAuthScope . isDone ( ) ) ;
351
+ assert ( httpResponse . message . statusCode == httpm . HttpCodes . OK , "status code should be 200 - OK" ) ;
352
+ assert ( asJson . source === "nock" , "http get request should be intercepted by nock" ) ;
353
+ assert ( asJson . success , "Authentication should not occur in redirection to other hosts" ) ;
354
+ } ) ;
355
+
219
356
it ( '[NTLM] - does basic http get request with NTLM Authentication' , async ( ) => {
220
357
/**
221
358
* Following NTLM Authentication Example on:
0 commit comments