All files / services/core microsoft-auth-service.ts

85.71% Statements 6/7
100% Branches 1/1
100% Functions 1/1
85.71% Lines 6/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x               3x 3x 3x 3x   3x          
import * as vscode from 'vscode';
 
/**
 * Service for handling Microsoft Entra ID authentication using VS Code's built-in authentication API.
 */
export class MicrosoftAuthService {
	private static readonly PROVIDER_ID = 'microsoft';
 
	/**
	 * Checks if there's an existing session without prompting the user.
	 * @param scopes The OAuth scopes to check.
	 * @returns A promise that resolves to the access token if a session exists, or undefined.
	 */
	async getSession(scopes: string[], createIfNone: boolean = false): Promise<vscode.AuthenticationSession | undefined> {
		try {
			const id = MicrosoftAuthService.PROVIDER_ID;
			const options = { createIfNone: createIfNone };
			const session = await vscode.authentication.getSession(id, scopes, options);
 
			return session;
		} catch {
			return undefined;
		}
	}
}