-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support file storage for provisioned tokens in OSes != darwin
- Loading branch information
1 parent
48b6a35
commit f0f46c2
Showing
4 changed files
with
65 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { error } from "../../error.ts"; | ||
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 & 0o777) !== 0o600) { | ||
throw new Error("The credentials file have have been tampered with."); | ||
} | ||
} 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 (e) { | ||
throw new Error(`The credentials file has been tampered with (${e}).`); | ||
} | ||
} | ||
|
||
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(); | ||
} |