-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass storing private key (#32)
- Loading branch information
Showing
9 changed files
with
203 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
import { EXTENSION_ID } from './constants'; | ||
|
||
export const CONNECT_TO_SERVER_CMD = `${EXTENSION_ID}.connectToServer`; | ||
export const CREATE_NEW_TEXT_DOC_CMD = `${EXTENSION_ID}.createNewTextDoc`; | ||
export const DISCONNECT_EDITOR_CMD = `${EXTENSION_ID}.disconnectEditor`; | ||
export const DISCONNECT_FROM_SERVER_CMD = `${EXTENSION_ID}.disconnectFromServer`; | ||
export const DOWNLOAD_LOGS_CMD = `${EXTENSION_ID}.downloadLogs`; | ||
export const OPEN_IN_BROWSER_CMD = `${EXTENSION_ID}.openInBrowser`; | ||
export const OPEN_VARIABLE_PANELS_CMD = `${EXTENSION_ID}.openVariablePanels`; | ||
export const REFRESH_SERVER_TREE_CMD = `${EXTENSION_ID}.refreshServerTree`; | ||
export const REFRESH_SERVER_CONNECTION_TREE_CMD = `${EXTENSION_ID}.refreshServerConnectionTree`; | ||
export const REFRESH_VARIABLE_PANELS_CMD = `${EXTENSION_ID}.refreshVariablePanels`; | ||
export const REQUEST_DHE_USER_CREDENTIALS_CMD = `${EXTENSION_ID}.requestDheUserCredentials`; | ||
export const RUN_CODE_COMMAND = `${EXTENSION_ID}.runCode`; | ||
export const RUN_SELECTION_COMMAND = `${EXTENSION_ID}.runSelection`; | ||
export const SELECT_CONNECTION_COMMAND = `${EXTENSION_ID}.selectConnection`; | ||
export const START_SERVER_CMD = `${EXTENSION_ID}.startServer`; | ||
export const STOP_SERVER_CMD = `${EXTENSION_ID}.stopServer`; | ||
/** | ||
* Create a command string prefixed with the extension id. | ||
* @param cmd The command string suffix. | ||
*/ | ||
function cmd<T extends string>(cmd: T): `${typeof EXTENSION_ID}.${T}` { | ||
return `${EXTENSION_ID}.${cmd}`; | ||
} | ||
|
||
export const CONNECT_TO_SERVER_CMD = cmd('connectToServer'); | ||
export const CREATE_NEW_TEXT_DOC_CMD = cmd('createNewTextDoc'); | ||
export const DISCONNECT_EDITOR_CMD = cmd('disconnectEditor'); | ||
export const DISCONNECT_FROM_SERVER_CMD = cmd('disconnectFromServer'); | ||
export const DOWNLOAD_LOGS_CMD = cmd('downloadLogs'); | ||
export const GENERATE_DHE_KEY_PAIR_CMD = cmd('generateDHEKeyPair'); | ||
export const OPEN_IN_BROWSER_CMD = cmd('openInBrowser'); | ||
export const OPEN_VARIABLE_PANELS_CMD = cmd('openVariablePanels'); | ||
export const REFRESH_SERVER_TREE_CMD = cmd('refreshServerTree'); | ||
export const REFRESH_SERVER_CONNECTION_TREE_CMD = cmd( | ||
'refreshServerConnectionTree' | ||
); | ||
export const REFRESH_VARIABLE_PANELS_CMD = cmd('refreshVariablePanels'); | ||
export const REQUEST_DHE_USER_CREDENTIALS_CMD = cmd( | ||
'requestDheUserCredentials' | ||
); | ||
export const RUN_CODE_COMMAND = cmd('runCode'); | ||
export const RUN_SELECTION_COMMAND = cmd('runSelection'); | ||
export const SELECT_CONNECTION_COMMAND = cmd('selectConnection'); | ||
export const START_SERVER_CMD = cmd('startServer'); | ||
export const STOP_SERVER_CMD = cmd('stopServer'); |
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,39 @@ | ||
import type { SecretStorage } from 'vscode'; | ||
import type { DHPrivateKey } from '../types'; | ||
|
||
class Key { | ||
static privateKey(userName: string, serverUrl: URL): string { | ||
return `privateKey.${userName}.${serverUrl.toString()}`; | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper around `vscode.SecretStorage` for storing and retrieving secrets. | ||
* NOTE: For debugging, the secret store contents can be dumped to devtools | ||
* console via: | ||
* > Developer: Log Storage Database Contents | ||
*/ | ||
export class SecretService { | ||
constructor(secrets: SecretStorage) { | ||
this._secrets = secrets; | ||
} | ||
|
||
private readonly _secrets: SecretStorage; | ||
|
||
getPrivateKey = async ( | ||
userName: string, | ||
serverUrl: URL | ||
): Promise<DHPrivateKey | undefined> => { | ||
return this._secrets.get(Key.privateKey(userName, serverUrl)) as Promise< | ||
DHPrivateKey | undefined | ||
>; | ||
}; | ||
|
||
storePrivateKey = async ( | ||
userName: string, | ||
serverUrl: URL, | ||
privateKey: DHPrivateKey | ||
): Promise<void> => { | ||
await this._secrets.store(Key.privateKey(userName, serverUrl), privateKey); | ||
}; | ||
} |
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,45 @@ | ||
import { generateKeyPairSync } from 'node:crypto'; | ||
import type { DHKeyPair, DHPrivateKey, DHPublicKey } from '../types'; | ||
|
||
// synonymous with secp256r1 | ||
const NAMED_CURVE = 'prime256v1' as const; | ||
|
||
/** | ||
* Generate a new keypair in Deephaven format. | ||
* @param userName The userName to generate the keypair for. | ||
*/ | ||
export function generateKeyPairForUser<TUser extends string>( | ||
userName: TUser | ||
): DHKeyPair { | ||
const { publicKey: publicKeyBuffer, privateKey: privateKeyBuffer } = | ||
generateKeyPairSync('ec', { | ||
namedCurve: NAMED_CURVE, | ||
publicKeyEncoding: { type: 'spki', format: 'der' }, | ||
privateKeyEncoding: { type: 'pkcs8', format: 'der' }, | ||
}); | ||
|
||
const publicBase64Str = toEcBase64String(publicKeyBuffer); | ||
const privateBase64Str = toEcBase64String(privateKeyBuffer); | ||
|
||
const publicKey = `${userName} ${publicBase64Str}` as DHPublicKey; | ||
const privateKey = [ | ||
`user ${userName}`, | ||
`operateas ${userName}`, | ||
`public ${publicBase64Str}`, | ||
`private ${privateBase64Str}`, | ||
].join('\n') as DHPrivateKey; | ||
|
||
return { | ||
publicKey, | ||
privateKey, | ||
}; | ||
} | ||
|
||
/** | ||
* Prepend 'EC:' to the key and convert to a base64 string. | ||
* @param keyBuffer | ||
*/ | ||
export function toEcBase64String(keyBuffer: Buffer): string { | ||
const ecSentinel = Buffer.from('EC:'); | ||
return Buffer.concat([ecSentinel, keyBuffer]).toString('base64'); | ||
} |
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