Device Flow authentication package for Visual Studio Team Services
npm install vsts-device-flow-auth
$ npm install --save vsts-device-flow-auth
`
Usage
------------
Create a DeviceFlowAuthenticator with the URI of the user's Team Services account. You will also need to provide your application's client id and redirect Uri. For
information on how to register an application with Azure, see How to register an app with the v2.0 endpoint
for details.
`ts
const authOptions: IDeviceFlowAuthenticationOptions = {
clientId: '00000000-0000-0000-0000-000000000000',
redirectUri: 'https://some-url.com'
};
const dfa: DeviceFlowAuthenticator = new DeviceFlowAuthenticator("https://my-corporate-account.visualstudio.com", authOptions);
`
Call GetDeviceFlowDetails() to acquire the device_code, verification_url and optional message for the user to authenticate with Team Services.
`ts
const obj: DeviceFlowDetails = await dfa.GetDeviceFlowDetails();
`
Call WaitForPersonalAccessToken() to get the token to use on the user's behalf.
`ts
const pat: string = await dfa.WaitForPersonalAccessToken();
`
Below is a short TypeScript example of how to use the package. A complete example can be found in the GitHub repository.
`ts
import { DeviceFlowAuthenticator, DeviceFlowDetails, IDeviceFlowAuthenticationOptions, IDeviceFlowTokenOptions } from 'vsts-device-flow-auth';
async function run() {
const resourceUri: string = https://my-corporate-account.visualstudio.com;
//const resourceUri: string = https://my-personal-account.visualstudio.com;
const authOptions: IDeviceFlowAuthenticationOptions = {
clientId: '00000000-0000-0000-0000-000000000000',
redirectUri: 'https://some-url.com'
};
const tokenOptions: IDeviceFlowTokenOptions = {
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(Go do the Device Flow authentication...);
console.log();
const pat: string = await dfa.WaitForPersonalAccessToken();
console.log(pat);
}
run();
``