Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic for conditional Authentication Provider registration #877

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ export async function activate(context: ExtensionContext): Promise<void> {
)
);

const session = await authentication.getSession(
ANSIBLE_LIGHTSPEED_AUTH_ID,
[],
{
let session: vscode.AuthenticationSession | undefined;

if (await workspace.getConfiguration("ansible").get("lightspeed.enabled")) {
session = await authentication.getSession(ANSIBLE_LIGHTSPEED_AUTH_ID, [], {
createIfNone: false,
}
);
});
}

if (session) {
window.registerTreeDataProvider(
Expand Down Expand Up @@ -387,6 +387,15 @@ async function resyncAnsibleInventory(): Promise<void> {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function getAuthToken(): Promise<void> {
if (
!(await workspace.getConfiguration("ansible").get("lightspeed.enabled"))
) {
await window.showErrorMessage(
"Enable lightspeed services from settings to use the feature."
);
return;
}

const session = await authentication.getSession(
ANSIBLE_LIGHTSPEED_AUTH_ID,
[],
Expand Down
15 changes: 14 additions & 1 deletion src/features/lightspeed/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class LightSpeedManager {
ANSIBLE_LIGHTSPEED_AUTH_ID,
ANSIBLE_LIGHTSPEED_AUTH_NAME
);
this.lightSpeedAuthenticationProvider.initialize();
this.apiInstance = new LightSpeedAPI(
this.settingsManager,
this.lightSpeedAuthenticationProvider
Expand All @@ -67,7 +68,19 @@ export class LightSpeedManager {
this.updateLightSpeedStatusbar();
}

public reInitialize(): void {
public async reInitialize(): Promise<void> {
const lightspeedEnabled = await vscode.workspace
.getConfiguration("ansible")
.get("lightspeed.enabled");

if (!lightspeedEnabled) {
await this.lightSpeedAuthenticationProvider.dispose();
this.lightSpeedStatusBar.hide();
return;
} else {
this.lightSpeedAuthenticationProvider.initialize();
}

this.updateLightSpeedStatusbar();
}

Expand Down
25 changes: 23 additions & 2 deletions src/features/lightspeed/lightSpeedOAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class LightSpeedAuthenticationProvider
public settingsManager: SettingsManager;
private _sessionChangeEmitter =
new EventEmitter<AuthenticationProviderAuthenticationSessionsChangeEvent>();
private _disposable: Disposable;
private _disposable: Disposable | undefined;
private _uriHandler = new UriEventHandler();
private _authId: string;
private _authName: string;
Expand All @@ -63,6 +63,16 @@ export class LightSpeedAuthenticationProvider
this.settingsManager = settingsManager;
this._authId = authId;
this._authName = authName;
}

public initialize() {
if (this._disposable) {
console.log(
"[ansible-lightspeed-oauth] Auth provider already registered"
);
return;
}

this._disposable = Disposable.from(
authentication.registerAuthenticationProvider(
this._authId,
Expand Down Expand Up @@ -186,7 +196,18 @@ export class LightSpeedAuthenticationProvider
* Dispose the registered services
*/
public async dispose() {
this._disposable.dispose();
if (this._disposable) {
const account = await this.isAuthenticated();

if (account) {
const sessionId = account.id;
this.removeSession(sessionId);
}

console.log("[ansible-lightspeed-oauth] Disposing auth provider");
await this._disposable.dispose();
this._disposable = undefined;
}
}

/* Log in to the Ansible Lightspeed auth service */
Expand Down
2 changes: 1 addition & 1 deletion src/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function updateConfigurationChanges(
lightSpeedManager: LightSpeedManager
): Promise<void> {
await metaData.updateAnsibleInfoInStatusbar();
lightSpeedManager.reInitialize();
await lightSpeedManager.reInitialize();
await pythonInterpreter.updatePythonInfoInStatusbar();

await extSettings.reinitialize();
Expand Down