-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
98 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,41 +1,41 @@ | ||
{ | ||
"scripts": { | ||
"dev": "IS_DEVELOPMENT_CLI_ENV=true deno run src/index.ts", | ||
"release": "deno run --allow-all src/scripts/release.ts", | ||
"prod": "deno run --allow-all src/index.ts", | ||
"schema": "npx openapi-typescript https://api.sfcompute.com/docs/json -o src/schema.ts" | ||
}, | ||
"dependencies": { | ||
"@inquirer/prompts": "^5.1.2", | ||
"@types/ms": "^0.7.34", | ||
"axios": "^1.7.2", | ||
"boxen": "^8.0.1", | ||
"chalk": "^5.3.0", | ||
"chrono-node": "^2.7.6", | ||
"cli-table3": "^0.6.5", | ||
"commander": "^12.1.0", | ||
"dayjs": "^1.11.13", | ||
"dotenv": "^16.4.5", | ||
"ink": "^5.0.1", | ||
"ink-confirm-input": "^2.0.0", | ||
"ink-spinner": "^5.0.0", | ||
"ink-text-input": "^6.0.0", | ||
"inquirer": "^10.1.2", | ||
"ms": "^2.1.3", | ||
"node-fetch": "^3.3.2", | ||
"openapi-fetch": "^0.11.1", | ||
"ora": "^8.1.0", | ||
"parse-duration": "^1.1.0", | ||
"react": "^18.3.1", | ||
"semver": "^7.6.3", | ||
"tiny-invariant": "^1.3.3", | ||
"yaml": "^2.6.1" | ||
}, | ||
"devDependencies": { | ||
"@types/semver": "^7.5.8" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.6.2" | ||
}, | ||
"version": "0.0.70" | ||
} | ||
"scripts": { | ||
"devv": "IS_DEVELOPMENT_CLI_ENV=true deno run --allow-all src/index.ts", | ||
"release": "deno run --allow-all src/scripts/release.ts", | ||
"prod": "deno run --allow-all src/index.ts", | ||
"schema": "npx openapi-typescript https://api.sfcompute.com/docs/json -o src/schema.ts" | ||
}, | ||
"dependencies": { | ||
"@inquirer/prompts": "^5.1.2", | ||
"@types/ms": "^0.7.34", | ||
"axios": "^1.7.2", | ||
"boxen": "^8.0.1", | ||
"chalk": "^5.3.0", | ||
"chrono-node": "^2.7.6", | ||
"cli-table3": "^0.6.5", | ||
"commander": "^12.1.0", | ||
"dayjs": "^1.11.13", | ||
"dotenv": "^16.4.5", | ||
"ink": "^5.0.1", | ||
"ink-confirm-input": "^2.0.0", | ||
"ink-spinner": "^5.0.0", | ||
"ink-text-input": "^6.0.0", | ||
"inquirer": "^10.1.2", | ||
"ms": "^2.1.3", | ||
"node-fetch": "^3.3.2", | ||
"openapi-fetch": "^0.11.1", | ||
"ora": "^8.1.0", | ||
"parse-duration": "^1.1.0", | ||
"react": "^18.3.1", | ||
"semver": "^7.6.3", | ||
"tiny-invariant": "^1.3.3", | ||
"yaml": "^2.6.1" | ||
}, | ||
"devDependencies": { | ||
"@types/semver": "^7.5.8" | ||
}, | ||
"peerDependencies": { | ||
"typescript": "^5.6.2" | ||
}, | ||
"version": "0.0.70" | ||
} |
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,90 @@ | ||
import * as crypto from "node:crypto"; | ||
import * as path from "node:path"; | ||
import * as os from "node:os"; | ||
|
||
export async function getKeys(): Promise<{ publicKey: string; privateKey: string }> { | ||
const keys = await loadKeys(); | ||
if (keys && typeof keys.privateKey === 'string' && typeof keys.publicKey === 'string') { | ||
return { | ||
publicKey: keys.publicKey, | ||
privateKey: keys.privateKey | ||
}; | ||
} | ||
const newKeys = generateKeyPair(); | ||
console.error("generating new keys") | ||
await saveKeys(newKeys); | ||
return newKeys; | ||
} | ||
|
||
function generateKeyPair() { | ||
// Generate RSA key pair | ||
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { | ||
modulusLength: 2048, | ||
publicKeyEncoding: { | ||
type: 'spki', | ||
format: 'pem' | ||
}, | ||
privateKeyEncoding: { | ||
type: 'pkcs8', | ||
format: 'pem' | ||
} | ||
}); | ||
|
||
return { | ||
publicKey, | ||
privateKey, | ||
}; | ||
} | ||
|
||
export function decryptSecret(secret: string, privateKey: string) { | ||
const decrypted = crypto.privateDecrypt({ | ||
key: privateKey, | ||
padding: crypto.constants.RSA_PKCS1_PADDING, | ||
}, secret); | ||
return decrypted; | ||
} | ||
|
||
async function saveKeys(keys: { publicKey: string; privateKey: string }) { | ||
const { publicKey, privateKey } = keys; | ||
const publicKeyPath = path.join(os.homedir(), ".sf", "public.pem"); | ||
const privateKeyPath = path.join(os.homedir(), ".sf", "private.pem"); | ||
|
||
try { | ||
// Create .sf directory if it doesn't exist | ||
await Deno.mkdir(path.dirname(publicKeyPath), { recursive: true }); | ||
|
||
// Write keys to files | ||
await Deno.writeTextFile(publicKeyPath, publicKey); | ||
await Deno.writeTextFile(privateKeyPath, privateKey); | ||
|
||
// Set private key permissions to be readable only by owner | ||
await Deno.chmod(privateKeyPath, 0o600); | ||
} catch (err) { | ||
throw new Error(`Failed to store keys: ${err}`); | ||
} | ||
} | ||
|
||
async function loadKeys() { | ||
const publicKeyPath = path.join(os.homedir(), ".sf", "public.pem"); | ||
const privateKeyPath = path.join(os.homedir(), ".sf", "private.pem"); | ||
|
||
let publicKey = null; | ||
let privateKey = null; | ||
|
||
try { | ||
publicKey = await Deno.readTextFile(publicKeyPath); | ||
} catch (err) { | ||
// Leave publicKey as null if read fails | ||
} | ||
|
||
try { | ||
privateKey = await Deno.readTextFile(privateKeyPath); | ||
} catch (err) { | ||
// Leave privateKey as null if read fails | ||
} | ||
|
||
return { | ||
publicKey, | ||
privateKey, | ||
}; | ||
} |