Files
smartssh/ts/smartssh.classes.sshkey.ts
T

154 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-10-11 13:05:29 +02:00
import * as plugins from './smartssh.plugins.js';
import * as helpers from './smartssh.classes.helpers.js';
2016-04-25 04:06:20 +02:00
export type TSshKeyType = 'duplex' | 'private' | 'public';
2016-05-31 19:16:45 +02:00
export class SshKey {
2018-09-17 22:32:31 +02:00
private _privKey: string;
private _pubKey: string;
private _hostVar: string;
private _authorized: boolean;
2016-11-23 12:38:38 +01:00
2018-09-17 22:32:31 +02:00
/**
* the constructor for class SshKey
*/
constructor(
optionsArg: { private?: string; public?: string; host?: string; authorized?: boolean } = {}
) {
this._privKey = optionsArg.private ?? '';
this._pubKey = optionsArg.public ?? '';
if (optionsArg.host) {
helpers.assertSafeHost(optionsArg.host);
}
this._hostVar = optionsArg.host ?? '';
this._authorized = optionsArg.authorized ?? false;
2018-09-17 22:32:31 +02:00
}
2016-06-25 02:29:34 +02:00
2018-09-17 22:32:31 +02:00
// this.host
get host() {
return this._hostVar;
}
set host(hostArg: string) {
if (hostArg) {
helpers.assertSafeHost(hostArg);
}
2018-09-17 22:32:31 +02:00
this._hostVar = hostArg;
}
2016-06-25 02:29:34 +02:00
2018-09-17 22:32:31 +02:00
// this.privKey
get privKey() {
return this._privKey;
}
set privKey(privateKeyArg: string) {
this._privKey = privateKeyArg;
}
2016-06-25 02:29:34 +02:00
2018-09-17 22:32:31 +02:00
// this.privKeyBase64
get privKeyBase64() {
return plugins.smartstring.base64.encode(this._privKey);
}
set privKeyBase64(privateKeyArg: string) {
this._privKey = plugins.smartstring.base64.decode(privateKeyArg);
}
2016-06-25 02:29:34 +02:00
2018-09-17 22:32:31 +02:00
// this.pubKey
get pubKey() {
return this._pubKey;
}
set pubKey(publicKeyArg: string) {
this._pubKey = publicKeyArg;
}
2016-06-25 02:29:34 +02:00
2018-09-17 22:32:31 +02:00
// this.pubKeyBase64
get pubKeyBase64() {
return plugins.smartstring.base64.encode(this._pubKey);
}
set pubKeyBase64(publicKeyArg: string) {
this._pubKey = plugins.smartstring.base64.decode(publicKeyArg);
}
2016-06-14 00:11:20 +02:00
2018-09-17 22:32:31 +02:00
get authorized() {
return this._authorized;
}
set authorized(authorizedArg: boolean) {
this._authorized = authorizedArg;
}
2016-11-23 12:38:38 +01:00
2018-09-17 22:32:31 +02:00
/**
* returns wether there is a private, a public or both keys
*/
get type(): TSshKeyType | undefined {
2018-09-17 22:32:31 +02:00
if (this._privKey && this._pubKey) {
return 'duplex';
} else if (this._privKey) {
return 'private';
} else if (this._pubKey) {
return 'public';
2016-06-24 20:58:55 +02:00
}
2018-09-17 22:32:31 +02:00
}
2016-11-23 12:38:38 +01:00
2018-09-17 22:32:31 +02:00
// methods
read(filePathArg: string) {
const resolvedPath = plugins.path.resolve(filePathArg);
const fileName = plugins.path.basename(resolvedPath);
const isPublicKey = fileName.endsWith('.pub');
const host = isPublicKey ? fileName.slice(0, -4) : fileName;
helpers.assertSafeHost(host);
this._hostVar = host;
if (isPublicKey) {
this._pubKey = plugins.fs.readFileSync(resolvedPath, 'utf8');
} else {
this._privKey = plugins.fs.readFileSync(resolvedPath, 'utf8');
}
}
static fromFile(filePathArg: string) {
const sshKey = new SshKey();
sshKey.read(filePathArg);
return sshKey;
}
static fromFiles(optionsArg: { privateKeyPath?: string; publicKeyPath?: string; host?: string }) {
const sshKey = new SshKey({ host: optionsArg.host });
if (optionsArg.privateKeyPath) {
sshKey.privKey = plugins.fs.readFileSync(plugins.path.resolve(optionsArg.privateKeyPath), 'utf8');
if (!sshKey.host) {
const fileName = plugins.path.basename(optionsArg.privateKeyPath);
helpers.assertSafeHost(fileName);
sshKey.host = fileName;
}
}
if (optionsArg.publicKeyPath) {
sshKey.pubKey = plugins.fs.readFileSync(plugins.path.resolve(optionsArg.publicKeyPath), 'utf8');
if (!sshKey.host) {
const fileName = plugins.path.basename(optionsArg.publicKeyPath).replace(/\.pub$/, '');
helpers.assertSafeHost(fileName);
sshKey.host = fileName;
}
}
return sshKey;
}
2018-09-17 22:32:31 +02:00
async store(dirPathArg: string) {
this.storeSync(dirPathArg);
}
storeSync(dirPathArg: string) {
helpers.assertSafeHost(this.host);
const resolvedDir = helpers.resolveSshDirPath(dirPathArg);
helpers.ensureSshDirSync(resolvedDir);
const fileNameBase = this.host;
2018-09-17 22:32:31 +02:00
if (this._privKey) {
const filePath = plugins.path.join(resolvedDir, fileNameBase);
plugins.fs.writeFileSync(filePath, this._privKey);
plugins.fs.chmodSync(filePath, 0o600);
2018-09-17 22:32:31 +02:00
}
if (this._pubKey) {
const filePath = plugins.path.join(resolvedDir, fileNameBase + '.pub');
plugins.fs.writeFileSync(filePath, this._pubKey);
plugins.fs.chmodSync(filePath, 0o644);
2016-04-25 04:06:20 +02:00
}
2018-09-17 22:32:31 +02:00
}
2016-06-25 02:29:34 +02:00
}