| |
|
| |
|
| | import 'isomorphic-fetch';
|
| | import * as vscode from 'vscode';
|
| | import { AzureDevOpsAuthenticationProvider } from './authProvider';
|
| |
|
| |
|
| |
|
| | export function activate(context: vscode.ExtensionContext) {
|
| |
|
| | console.log('Congratulations, your extension "vscode-authenticationprovider-sample" is now active!');
|
| |
|
| |
|
| |
|
| | context.subscriptions.push(vscode.authentication.registerAuthenticationProvider(
|
| | AzureDevOpsAuthenticationProvider.id,
|
| | 'Azure Repos',
|
| | new AzureDevOpsAuthenticationProvider(context.secrets),
|
| | ));
|
| |
|
| | const disposable = vscode.commands.registerCommand('vscode-authenticationprovider-sample.login', async () => {
|
| |
|
| | const session = await vscode.authentication.getSession(AzureDevOpsAuthenticationProvider.id, [], { createIfNone: true });
|
| |
|
| | try {
|
| |
|
| |
|
| | const req = await fetch('https://app.vssps.visualstudio.com/_apis/profile/profiles/me?api-version=6.0', {
|
| | headers: {
|
| | authorization: `Basic ${Buffer.from(`:${session.accessToken}`).toString('base64')}`,
|
| | 'content-type': 'application/json',
|
| | },
|
| | });
|
| | if (!req.ok) {
|
| | throw new Error(req.statusText);
|
| | }
|
| | const res = await req.json() as { displayName: string };
|
| | vscode.window.showInformationMessage(`Hello ${res.displayName}`);
|
| | } catch (e) {
|
| | if (e instanceof Error && e.message === 'Unauthorized') {
|
| | vscode.window.showErrorMessage('Failed to get profile. You need to use a PAT that has access to all organizations. Please sign out and try again.');
|
| | }
|
| | throw e;
|
| | }
|
| | });
|
| |
|
| | context.subscriptions.push(disposable);
|
| | }
|
| |
|