smartssh/ts/smartssh.classes.sshkey.ts

107 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-10-11 11:05:29 +00:00
import * as plugins from './smartssh.plugins.js';
import * as helpers from './smartssh.classes.helpers.js';
2016-04-25 02:06:20 +00:00
export class SshKey {
2018-09-17 20:32:31 +00:00
private _privKey: string;
private _pubKey: string;
private _hostVar: string;
private _authorized: boolean;
2016-11-23 11:38:38 +00:00
2024-02-09 17:21:33 +00:00
private _smarthshellInstance = new plugins.smartshell.Smartshell({
2022-10-11 11:05:29 +00:00
executor: 'bash',
2018-09-17 20:32:31 +00:00
});
2016-11-23 11:38:38 +00:00
2018-09-17 20:32:31 +00:00
/**
* the constructor for class SshKey
*/
constructor(
optionsArg: { private?: string; public?: string; host?: string; authorized?: boolean } = {}
) {
this._privKey = optionsArg.private;
this._pubKey = optionsArg.public;
this._hostVar = optionsArg.host;
this._authorized = optionsArg.authorized;
}
2016-06-25 00:29:34 +00:00
2018-09-17 20:32:31 +00:00
// this.host
get host() {
return this._hostVar;
}
set host(hostArg: string) {
this._hostVar = hostArg;
}
2016-06-25 00:29:34 +00:00
2018-09-17 20:32:31 +00:00
// this.privKey
get privKey() {
return this._privKey;
}
set privKey(privateKeyArg: string) {
this._privKey = privateKeyArg;
}
2016-06-25 00:29:34 +00:00
2018-09-17 20:32:31 +00: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 00:29:34 +00:00
2018-09-17 20:32:31 +00:00
// this.pubKey
get pubKey() {
return this._pubKey;
}
set pubKey(publicKeyArg: string) {
this._pubKey = publicKeyArg;
}
2016-06-25 00:29:34 +00:00
2018-09-17 20:32:31 +00: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-13 22:11:20 +00:00
2018-09-17 20:32:31 +00:00
get authorized() {
return this._authorized;
}
set authorized(authorizedArg: boolean) {
this._authorized = authorizedArg;
}
2016-11-23 11:38:38 +00:00
2018-09-17 20:32:31 +00:00
/**
* returns wether there is a private, a public or both keys
*/
get type() {
if (this._privKey && this._pubKey) {
return 'duplex';
} else if (this._privKey) {
return 'private';
} else if (this._pubKey) {
return 'public';
2016-06-24 18:58:55 +00:00
}
2018-09-17 20:32:31 +00:00
}
set type(someVlueArg: any) {
console.log('the type of an SshKey connot be set. This value is autocomputed.');
}
2016-11-23 11:38:38 +00:00
2018-09-17 20:32:31 +00:00
// methods
read(filePathArg) {}
async store(dirPathArg: string) {
plugins.fs.ensureDirSync(dirPathArg);
let fileNameBase = this.host;
if (this._privKey) {
let filePath = plugins.path.join(dirPathArg, fileNameBase);
plugins.smartfile.memory.toFsSync(this._privKey, filePath);
await this._smarthshellInstance.exec(`chmod 0600 ${filePath}`);
}
if (this._pubKey) {
let filePath = plugins.path.join(dirPathArg, fileNameBase + '.pub');
plugins.smartfile.memory.toFsSync(this._pubKey, filePath);
await this._smarthshellInstance.exec(`chmod 0600 ${filePath}`);
2016-04-25 02:06:20 +00:00
}
2018-09-17 20:32:31 +00:00
}
2016-06-25 00:29:34 +00:00
}