This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
testapp.ts
62 lines (52 loc) · 2.64 KB
/
testapp.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
"use strict";
import * as os from 'os';
import { DeviceFlowAuthenticator, DeviceFlowDetails, IDeviceFlowAuthenticationOptions, IDeviceFlowTokenOptions } from '../src/deviceflow';
async function run() {
const resourceUri: string = `https://<some-aad-account>.visualstudio.com`; //AAD-backed account
//const resourceUri: string = `https://my-personal-account.visualstudio.com`; //MSA-backed account
try {
const authOptions: IDeviceFlowAuthenticationOptions = {
//authorityHost: 'https://management.core.windows.net/',
clientId: '00000000-0000-0000-0000-000000000000',
redirectUri: 'https://some-url.com',
//userAgent: ''
};
const tokenOptions: IDeviceFlowTokenOptions = {
//grantType: 'device_code',
//tokenScope: '',
tokenDescription: `vsts-device-flow-auth test app: ${resourceUri} on ${os.hostname()}`,
};
const dfa: DeviceFlowAuthenticator = new DeviceFlowAuthenticator(resourceUri, authOptions, tokenOptions);
const obj: DeviceFlowDetails = await dfa.GetDeviceFlowDetails();
console.log(`message: ${obj.Message}`);
console.log(`user code: ${obj.UserCode}`);
console.log(`verify url: ${obj.VerificationUrl}`);
console.log(``);
//Set breakpoint on line below in order to go and do the authentication
console.log(`Go do the Device Flow authentication...`);
console.log(``);
// The following simulates a timeout call from the client
// setTimeout(() => {
// dfa.Cancel(true);
// }, 8000);
const pat: string = await dfa.WaitForPersonalAccessToken();
console.log(`Personal Access Token is: ${pat}`);
} catch (err) {
const statusCode: string = err.statusCode || '';
const message: string = err.message || '';
let method: string = '';
let optionsUrl: string = '';
if (err.options && err.options.method) {
method = err.options.method;
}
if (err.options && err.options.url) {
optionsUrl = err.options.url;
}
console.log(`Unable to get personal access token for ${resourceUri}. [statusCode: '${statusCode}' method: '${method}' message: '${message}' optionsUrl: '${optionsUrl}']`);
}
}
run();