Skip to content

Commit

Permalink
feat: support file storage for provisioned tokens in OSes != darwin (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauorriols authored Nov 30, 2023
1 parent 8726eed commit a4be425
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/utils/access_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ function base64url(binary: Uint8Array): string {
return urlSafeOutput;
}

async function sha256(random_string: string): Promise<Uint8Array> {
async function sha256(randomString: string): Promise<Uint8Array> {
return new Uint8Array(
await crypto.subtle.digest(
"SHA-256",
new TextEncoder().encode(random_string),
new TextEncoder().encode(randomString),
),
);
}
1 change: 1 addition & 0 deletions src/utils/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function getConfigPaths() {
return {
configDir,
updatePath: join(configDir, "update.json"),
credentialsPath: join(configDir, "credentials.json"),
};
}

Expand Down
30 changes: 24 additions & 6 deletions src/utils/token_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ if (Deno.build.os === "darwin") {
const memory = await import("./token_storage/memory.ts");
module = {
get: defaultOnError(
"Failed to get token from Keychain",
"Failed to get token from Keychain. Will provision a new token for this execution but please make sure to fix the issue afterwards.",
memory.get,
darwin.getFromKeychain,
),
store: defaultOnError(
"Failed to store token into Keychain",
"Failed to store token into Keychain. Will keep it in memory for the duration of this execution but please make sure to fix the issue afterwards.",
memory.store,
darwin.storeInKeyChain,
),
Expand All @@ -30,7 +30,25 @@ if (Deno.build.os === "darwin") {
),
};
} else {
module = await import("./token_storage/memory.ts");
const fs = await import("./token_storage/fs.ts");
const memory = await import("./token_storage/memory.ts");
module = {
get: defaultOnError(
"Failed to get token from credentials file. Will provision a new token for this execution but please make sure to fix the issue afterwards.",
memory.get,
fs.get,
),
store: defaultOnError(
"Failed to store token in credentials file. Will keep it in memory for the duration of this execution but please make sure to fix the issue afterwards.",
memory.store,
fs.store,
),
remove: defaultOnError(
"Failed to remove token from credentials file",
memory.remove,
fs.remove,
),
};
}
export default module;

Expand All @@ -50,9 +68,9 @@ function defaultOnError<
.catch((err) => {
const spinnerInterrupt = interruptSpinner();
wait("").start().warn(notification);
let errStr = err.toString();
if (errStr.length > 80) {
errStr = errStr.slice(0, 80) + "...";
let errStr = err.message;
if (errStr.length > 90) {
errStr = errStr.slice(0, 90) + "...";
}
wait({ text: "", indent: 3 }).start().fail(errStr);
spinnerInterrupt.resume();
Expand Down
45 changes: 45 additions & 0 deletions src/utils/token_storage/fs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { getConfigPaths } from "../info.ts";

export async function get(): Promise<string | null> {
const { credentialsPath } = getConfigPaths();
try {
const info = await Deno.lstat(credentialsPath);
if (!info.isFile || (info.mode !== null && (info.mode & 0o777) !== 0o600)) {
throw new Error(
"The credentials file has been tampered with and will be ignored. Please delete it.",
);
}
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
return null;
} else {
throw e;
}
}
try {
const token = JSON.parse(await Deno.readTextFile(credentialsPath)).token;
return token || null;
} catch (_) {
throw new Error(
`The credentials file has been tampered with and will be ignored. Please delete it.`,
);
}
}

export async function store(token: string): Promise<void> {
const { credentialsPath, configDir } = getConfigPaths();
await Deno.mkdir(configDir, { recursive: true });
await Deno.writeTextFile(
credentialsPath,
JSON.stringify({ token }, null, 2),
{ mode: 0o600 },
);
return Promise.resolve();
}

export async function remove(): Promise<void> {
const { credentialsPath, configDir } = getConfigPaths();
await Deno.mkdir(configDir, { recursive: true });
await Deno.writeTextFile(credentialsPath, "{}", { mode: 0o600 });
return Promise.resolve();
}

1 comment on commit a4be425

@deno-deploy
Copy link

@deno-deploy deno-deploy bot commented on a4be425 Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An internal server error occurred.

Please sign in to comment.