Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Use SecretStorage for auth token instead of public file #14

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ You can copy the token from the Bitburner application 'API Server' context menu:

#### Adding the token to the extension:

The token will ultimately end up in the workspace configuration (See your workspaces`./.vscode/settings.json`), so you can either:

- Add the token manually to the workspace `settings.json` to the key of `bitburner.authToken`.
- Use the command palette (CTRL/CMD + SHIFT + P) and select `Bitburner: Add Auth Token`.
- Paste the Auth Token copied via the games context menu in to the input box.

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"url": "https://github.com/hexnaught/vscode-bitburner-connector/issues"
},
"contributors": [
"Hexnaught <[email protected]>"
"Hexnaught <[email protected]>",
"Malanius <[email protected]>"
],
"categories": [
"Other"
Expand Down
32 changes: 19 additions & 13 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ const BB_GAME_CONFIG = {
let fsWatcher;
let fwEnabled;

/**
* @type {vscode.SecretStorage}
*/
let secrets;

// TODO: Refactor this user config to combined 'extension/user config' with better internal API/structure
/**
* @type {{ scriptRoot: string, fwEnabled: boolean, showPushSuccessNotification: boolean, showFileWatcherEnabledNotification: boolean, authToken: string }}
* @type {{ scriptRoot: string, fwEnabled: boolean, showPushSuccessNotification: boolean, showFileWatcherEnabledNotification: boolean }}
*/
let sanitizedUserConfig;

/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
secrets = context.secrets;
/**
* @type Array<vscode.Disposable>
*/
Expand Down Expand Up @@ -60,11 +66,14 @@ function activate(context) {
prompt: `Please enter the Bitburner Auth Token, for more information, see 'README #authentication'.`,
})
.then((authToken) => {
vscode.workspace
.getConfiguration(`bitburner`)
.update(`authToken`, authToken)
secrets
.store(`authToken`, authToken.replace(/^bearer/i, ``).trim())
.then(() => {
showToast(`Bitburner Auth Token Added!`);
})
.then(undefined, (err) => {
console.error(`Storing of token failed: `, err);
showToast(`Failed to add Bitburner Auth Token!`, `error`);
});
});
}),
Expand Down Expand Up @@ -257,7 +266,7 @@ const getCurrentOpenDocURI = () => vscode.window.activeTextEditor.document.uri.f
* Make a POST request to the expected port of the game
* @param {{ action: `CREATE` | `UPDATE` | `UPSERT` | `DELETE`, filename: string, code?: string }} payload The payload to send to the game client
*/
const doPostRequestToBBGame = (payload) => {
const doPostRequestToBBGame = async (payload) => {
// If the file is going to be in a director, it NEEDS the leading `/`, i.e. `/my-dir/file.js`
// If the file is standalone, it CAN NOT HAVE a leading slash, i.e. `file.js`
// The game will not accept the file and/or have undefined behaviour otherwise...
Expand All @@ -269,6 +278,7 @@ const doPostRequestToBBGame = (payload) => {
cleanPayload.filename = `/${cleanPayload.filename}`;
}

const token = await secrets.get(`authToken`);
const stringPayload = JSON.stringify(cleanPayload);
const options = {
hostname: BB_GAME_CONFIG.url,
Expand All @@ -278,7 +288,7 @@ const doPostRequestToBBGame = (payload) => {
headers: {
"Content-Type": `application/json`,
"Content-Length": stringPayload.length,
Authorization: `Bearer ${sanitizedUserConfig.authToken}`,
Authorization: `Bearer ${token}`,
},
};

Expand All @@ -305,7 +315,7 @@ const doPostRequestToBBGame = (payload) => {
};

// TODO: Overhaul internal user/extension config 'API'
const sanitizeUserConfig = () => {
const sanitizeUserConfig = async () => {
const userConfig = vscode.workspace.getConfiguration(`bitburner`);
const fwInspect = vscode.workspace.getConfiguration(`bitburner`).inspect(`fileWatcher.enable`);

Expand All @@ -320,7 +330,7 @@ const sanitizeUserConfig = () => {
}

// Checks if initializing or user config changed for fileWatcher.enabled
if (!sanitizedUserConfig || !userConfig.get(`authToken`) || sanitizedUserConfig.fwEnabled !== fwVal) {
if (!sanitizedUserConfig || (await secrets.get(`authToken`)) || sanitizedUserConfig.fwEnabled !== fwVal) {
fwEnabled = fwVal;
}

Expand All @@ -329,10 +339,6 @@ const sanitizeUserConfig = () => {
fwEnabled: fwVal,
showPushSuccessNotification: userConfig.get(`showPushSuccessNotification`),
showFileWatcherEnabledNotification: userConfig.get(`showFileWatcherEnabledNotification`),
authToken: userConfig
.get(`authToken`)
.replace(/^bearer/i, ``)
.trim(),
};
};

Expand Down Expand Up @@ -362,7 +368,7 @@ const showToast = (message, toastType = `information`, opts = { forceShow: false

const isValidGameFile = (fileURI) => BB_GAME_CONFIG.validFileExtensions.some((ext) => fileURI.endsWith(ext));

const isAuthTokenSet = () => Boolean(sanitizedUserConfig.authToken);
const isAuthTokenSet = async () => Boolean(await secrets.get(`authToken`));
const showAuthError = () => {
showToast(
`No Bitburner Auth Token is set. Please see the 'Authorization' section of the extensions README.md for more information.`,
Expand Down