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

feat: support file storage for provisioned tokens in OSes != darwin #202

Merged
merged 4 commits into from
Nov 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}