Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function run() {
cm.banner('Handler Samples');
const username = "";
const password = "";
const workstation = "";
const domain = "";
const url = "";
const basicHandler: hm.BasicCredentialHandler = new hm.BasicCredentialHandler(username, password);
const patHandler: hm.PersonalAccessTokenCredentialHandler = new hm.PersonalAccessTokenCredentialHandler('scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs');
const ntlmHandler: hm.NtlmCredentialHandler = new hm.NtlmCredentialHandler(username, password, workstation, domain);
// These handlers would then be passed to the constructors of the http or rest modules
// const httpClient: httpm.HttpClient = new httpm.HttpClient('vsts-node-api', [ntlmHandler]);
// const response: httpm.HttpClientResponse = await httpClient.get(url);
// console.log("response code: " + response.message.statusCode);
}
let artifactoryUser = tl.getEndpointAuthorizationParameter(artifactoryService, "username", true);
let artifactoryPassword = tl.getEndpointAuthorizationParameter(artifactoryService, "password", true);
let artifactoryAccessToken = tl.getEndpointAuthorizationParameter(artifactoryService, "apitoken", true);
// Check if Artifactory should be accessed using access-token.
if (artifactoryAccessToken) {
return [new clientHandlers.BearerCredentialHandler(artifactoryAccessToken)]
}
// Check if Artifactory should be accessed anonymously.
if (artifactoryUser === "") {
return [];
}
// Use basic authentication.
return [new clientHandlers.BasicCredentialHandler(artifactoryUser, artifactoryPassword)];
}
.basicAuth({
user: 'johndoe',
pass: 'password'
})
.reply(200, {
success: true,
source: "nock"
});
//Set nock for request without credentials or with incorrect credentials
nock('http://microsoft.com')
.get('/')
.reply(200, {
success: false,
source: "nock"
});
let bh: hm.BasicCredentialHandler = new hm.BasicCredentialHandler('johndoe', 'password');
let http: httpm.HttpClient = new httpm.HttpClient('typed-rest-client-tests', [bh]);
let res: httpm.HttpClientResponse = await http.get('http://microsoft.com');
assert(res.message.statusCode == 200, "status code should be 200");
let body: string = await res.readBody();
let obj: any = JSON.parse(body);
assert(obj.source === "nock", "http get request should be intercepted by nock");
assert(obj.success, "Authentication should succeed");
});
.basicAuth({
user: 'johndoe',
pass: 'password'
})
.reply(200, {
success: false,
source: "nock"
});
//Set nock for request without credentials
nock('http://microsoft.com')
.get('/')
.reply(200, {
success: true,
source: "nock"
});
let bh: hm.BasicCredentialHandler = new hm.BasicCredentialHandler('johndoe', 'password');
let http: httpm.HttpClient = new httpm.HttpClient('typed-rest-client-tests', [bh], {presignedUrlPatterns: [/microsoft/i]});
let res: httpm.HttpClientResponse = await http.get('http://microsoft.com');
assert(res.message.statusCode == 200, "status code should be 200");
let body: string = await res.readBody();
let obj: any = JSON.parse(body);
assert(obj.source === "nock", "http get request should be intercepted by nock");
assert(obj.success, "Authentication should succeed");
});