-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from Noahnc/feat/refactor_spacelift_auth_handling
Feat/refactor_spacelift_auth_handling
- Loading branch information
Showing
13 changed files
with
234 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/utils/spacelift/spacelift_authentication_handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { Ispacectl } from "./spacectl"; | ||
import { SpaceliftJwt } from "../../models/spacelift/jwt"; | ||
import { getLogger } from "../logger"; | ||
import { GraphQLClient, gql } from "graphql-request"; | ||
import * as vscode from "vscode"; | ||
|
||
interface ViewerId { | ||
viewer: id; | ||
} | ||
|
||
interface id { | ||
id: string | undefined; | ||
} | ||
|
||
export interface IspaceliftAuthenticationHandler { | ||
get_token(): Promise<SpaceliftJwt | null>; | ||
check_token_valid(): Promise<boolean>; | ||
login_interactive(): Promise<boolean>; | ||
} | ||
|
||
export class SpaceliftAuthenticationHandler implements IspaceliftAuthenticationHandler { | ||
private _spacectl: Ispacectl; | ||
private _cli: Ispacectl; | ||
private _spaceliftJwt: SpaceliftJwt | undefined; | ||
private _graphQLClient: GraphQLClient; | ||
|
||
constructor(spacectl: Ispacectl, cli: Ispacectl, graphqlClient: GraphQLClient) { | ||
this._spacectl = spacectl; | ||
this._cli = cli; | ||
this._graphQLClient = graphqlClient; | ||
} | ||
|
||
async check_token_valid(): Promise<boolean> { | ||
if (this._spaceliftJwt === undefined) { | ||
this._spaceliftJwt = await this._cli.getExportedToken(); | ||
} | ||
if (this._spaceliftJwt.isExpired()) { | ||
return false; | ||
} | ||
return this.check_token_not_revoked(); | ||
} | ||
|
||
async check_token_not_revoked(): Promise<boolean> { | ||
if (this._spaceliftJwt === undefined) { | ||
return false; | ||
} | ||
|
||
const query = gql` | ||
{ | ||
viewer { | ||
id | ||
} | ||
} | ||
`; | ||
|
||
this._graphQLClient.setHeaders({ | ||
authorization: `Bearer ${this._spaceliftJwt.rawToken}`, | ||
}); | ||
|
||
const valid = await this._graphQLClient | ||
.request<ViewerId>(query) | ||
.then((data) => { | ||
if (data === undefined || data === null) { | ||
return false; | ||
} | ||
// Check if data has an viewer.id filed (which is only present if the token is valid | ||
if (data.viewer === undefined || data.viewer === null || data.viewer.id === undefined || data.viewer.id === null) { | ||
return false; | ||
} | ||
getLogger().debug("Spacelift token is valid and not revoked"); | ||
return true; | ||
}) | ||
.catch((error) => { | ||
getLogger().debug("Failed to validate token: " + error); | ||
return false; | ||
}); | ||
return valid; | ||
} | ||
|
||
async login_interactive(): Promise<boolean> { | ||
// aks the user if he wants to login with the web browser | ||
const result = await vscode.window.showWarningMessage("Spacectl not authenticated, do you want to login with the web browser?", "Yes", "No").then(async (selection) => { | ||
if (selection === "Yes") { | ||
return true; | ||
} | ||
return false; | ||
}); | ||
if (result === false) { | ||
return false; | ||
} | ||
|
||
const login_result = await vscode.window.withProgress( | ||
{ | ||
location: vscode.ProgressLocation.Notification, | ||
title: "Waiting for spacectl login", | ||
cancellable: true, | ||
}, | ||
async (progress, token) => { | ||
token.onCancellationRequested(() => { | ||
getLogger().debug("User has cancelled spacectl login"); | ||
vscode.window.showWarningMessage("Spacectl login cancelled"); | ||
return false; | ||
}); | ||
return await this._cli.loginInteractive(); | ||
} | ||
); | ||
if (login_result === false) { | ||
return false; | ||
} | ||
this._spaceliftJwt = await this._cli.getExportedToken(); | ||
return true; | ||
} | ||
|
||
async get_token(): Promise<SpaceliftJwt | null> { | ||
if (this._spaceliftJwt === undefined) { | ||
getLogger().debug("No spacelift token cached, trying export from spacectl"); | ||
this._spaceliftJwt = await this._cli.getExportedToken(); | ||
if (await this.check_token_valid()) { | ||
getLogger().debug("Got valid spacelift token from spacectl"); | ||
return this._spaceliftJwt; | ||
} | ||
getLogger().warn("Newly exported spacelift token from spacectl is not valid. Retruning null as token."); | ||
return null; | ||
} | ||
|
||
if (await this.check_token_valid()) { | ||
getLogger().debug("Cached spacelift token is valid, returning it"); | ||
return this._spaceliftJwt; | ||
} | ||
|
||
getLogger().debug("Cached spacelift token is not valid, trying to get new token from spacectl"); | ||
this._spaceliftJwt = await this._cli.getExportedToken(); | ||
|
||
if (await this.check_token_valid()) { | ||
getLogger().debug("Got valid spacelift token from spacectl"); | ||
return this._spaceliftJwt; | ||
} | ||
getLogger().warn("spacectl token is not valid"); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import * as vscode from "vscode"; | ||
import { getLogger } from "../../utils/logger"; | ||
import { BaseStatusBarItem, IvscodeStatusBarItemSettings } from "./base_statusbar_item"; | ||
import { IspaceliftAuthenticationHandler } from "../../utils/spacelift/spacelift_authentication_handler"; | ||
|
||
export class SpaceliftApiAuthenticationStatus extends BaseStatusBarItem { | ||
private readonly _authHandler: IspaceliftAuthenticationHandler; | ||
constructor(context: vscode.ExtensionContext, settings: IvscodeStatusBarItemSettings, authHandler: IspaceliftAuthenticationHandler) { | ||
super(context, settings); | ||
this._authHandler = authHandler; | ||
} | ||
|
||
protected async run() { | ||
if ((await this._authHandler.check_token_valid()) == false) { | ||
getLogger().debug("No valid spacelift token, showing status bar item to show login required"); | ||
this._statusBarItem.text = "$(error) authenticate spacectl"; | ||
this._statusBarItem.color = "orange"; | ||
this._statusBarItem.show(); | ||
return; | ||
} | ||
getLogger().debug("Valid spacelift token, hiding status bar item"); | ||
this._statusBarItem.hide(); | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters