Skip to content

Commit

Permalink
auth: Add a getSessionWithScopes to AzureAuthentication (#1762)
Browse files Browse the repository at this point in the history
* Add a getSessionWithScopes to AzureAuthentication

* Remove scopes as a parameter

* Bump package version

* Fix build and PR feedback

* Actually listen to PR feedback
  • Loading branch information
nturinski authored Aug 6, 2024
1 parent f35ccad commit bbd9dbf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
4 changes: 2 additions & 2 deletions auth/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion auth/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@microsoft/vscode-azext-azureauth",
"author": "Microsoft Corporation",
"version": "2.4.1",
"version": "2.5.0",
"description": "Azure authentication helpers for Visual Studio Code",
"tags": [
"azure",
Expand Down
9 changes: 8 additions & 1 deletion auth/src/AzureAuthentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ import type * as vscode from 'vscode';
* Represents a means of obtaining authentication data for an Azure subscription.
*/
export interface AzureAuthentication {
/**
* Gets a VS Code authentication session for an Azure subscription.
* Always uses the default scope, `https://management.azure.com/.default/` and respects `microsoft-sovereign-cloud.environment` setting.
*
* @returns A VS Code authentication session or undefined, if none could be obtained.
*/
getSession(): vscode.ProviderResult<vscode.AuthenticationSession>;
/**
* Gets a VS Code authentication session for an Azure subscription.
*
* @param scopes - The scopes for which the authentication is needed.
*
* @returns A VS Code authentication session or undefined, if none could be obtained.
*/
getSession(scopes?: string[]): vscode.ProviderResult<vscode.AuthenticationSession>;
getSessionWithScopes(scopes: string[]): vscode.ProviderResult<vscode.AuthenticationSession>;
}
29 changes: 15 additions & 14 deletions auth/src/AzureDevOpsSubscriptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,25 +152,26 @@ export class AzureDevOpsSubscriptionProvider implements AzureSubscriptionProvide
}

const accessToken = (await this._tokenCredential?.getToken("https://management.azure.com/.default"))?.token || '';
const getSession = () => {
return {
accessToken,
id: this._tokenCredential?.tenantId || '',
account: {
id: this._tokenCredential?.tenantId || '',
label: this._tokenCredential?.tenantId || '',
},
tenantId: this._tokenCredential?.tenantId || '',
scopes: scopes || [],
};
};
return {
client: new armSubs.SubscriptionClient(this._tokenCredential,),
credential: this._tokenCredential,
authentication: {
getSession: (_scopes: string[] | undefined) => {
return {
accessToken,
id: this._tokenCredential?.tenantId || '',
account: {
id: this._tokenCredential?.tenantId || '',
label: this._tokenCredential?.tenantId || '',
},
tenantId: this._tokenCredential?.tenantId || '',
scopes: scopes || [],
};

}
getSession,
getSessionWithScopes: getSession,
}
};
}
}

public onDidSignIn: Event<void> = () => { return new Disposable(() => { /*empty*/ }) };
Expand Down
5 changes: 4 additions & 1 deletion auth/src/VSCodeAzureSubscriptionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,10 @@ export class VSCodeAzureSubscriptionProvider extends vscode.Disposable implement
client: new armSubs.SubscriptionClient(credential, { endpoint }),
credential: credential,
authentication: {
getSession: () => session
getSession: () => session,
getSessionWithScopes: (scopes) => {
return getSessionFromVSCode(scopes, tenantId, { createIfNone: false, silent: true })
},
}
};
}
Expand Down

0 comments on commit bbd9dbf

Please sign in to comment.