smartssh/ts/smartssh.classes.sshkey.ts

96 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-05-31 17:00:52 +00:00
import "typings-global";
import * as plugins from "./smartssh.plugins";
import * as helpers from "./smartssh.classes.helpers";
2016-04-25 02:06:20 +00:00
export class SshKey {
2016-06-25 00:29:34 +00:00
private _privKey:string;
private _pubKey:string;
private _hostVar:string;
private _authorized:boolean;
2016-06-25 00:10:53 +00:00
constructor(optionsArg:{private?:string,public?:string,host?:string,authorized?:boolean}={}){
2016-06-25 00:29:34 +00:00
this._privKey = optionsArg.private;
this._pubKey = optionsArg.public;
this._hostVar = optionsArg.host;
this._authorized = optionsArg.authorized;
2016-04-25 02:06:20 +00:00
};
2016-06-25 00:29:34 +00:00
// this.host
2016-04-26 02:44:50 +00:00
get host(){
2016-06-25 00:29:34 +00:00
return this._hostVar;
2016-04-26 02:44:50 +00:00
};
2016-06-25 00:29:34 +00:00
set host(hostArg:string){
this._hostVar = hostArg;
};
// this.privKey
get privKey(){
return this._privKey;
2016-04-25 02:06:20 +00:00
};
2016-06-25 00:29:34 +00:00
set privKey(privateKeyArg:string){
this._privKey = privateKeyArg;
};
// this.privKeyBase64
get privKeyBase64(){
return plugins.base64.encode(this._privKey);
2016-04-25 02:06:20 +00:00
}
2016-06-25 00:29:34 +00:00
set privKeyBase64(privateKeyArg:string) {
this._privKey = plugins.base64.decode(privateKeyArg);
2016-04-25 02:06:20 +00:00
}
2016-06-25 00:29:34 +00:00
// this.pubKey
get pubKey(){
return this._pubKey;
2016-04-25 02:06:20 +00:00
}
2016-06-25 00:29:34 +00:00
set pubKey(publicKeyArg:string){
this._pubKey = publicKeyArg;
};
// this.pubKeyBase64
get pubKeyBase64(){
return plugins.base64.encode(this._pubKey);
}
set pubKeyBase64(publicKeyArg:string) {
this._pubKey = plugins.base64.decode(publicKeyArg);
}
2016-06-25 13:30:57 +00:00
get authorized(){
return this._authorized;
}
set authorized(authorizedArg:boolean){
this._authorized = authorizedArg;
}
2016-04-25 02:06:20 +00:00
get type(){
2016-06-25 00:29:34 +00:00
if(this._privKey && this._pubKey){
2016-04-25 02:06:20 +00:00
return "duplex";
2016-06-25 00:29:34 +00:00
} else if(this._privKey){
2016-04-25 02:06:20 +00:00
return "private";
2016-06-25 00:29:34 +00:00
} else if(this._pubKey){
2016-04-25 02:06:20 +00:00
return "public";
}
};
2016-06-25 00:29:34 +00:00
set type(someVlueArg:any){
console.log("the type of an SshKey connot be set. This value is autpcomputed.")
2016-06-13 22:11:20 +00:00
}
2016-06-25 00:29:34 +00:00
// methods
2016-06-24 18:58:55 +00:00
read(filePathArg){
}
2016-06-26 02:07:03 +00:00
store(dirPathArg:string){
2016-06-25 12:13:26 +00:00
plugins.fs.ensureDirSync(dirPathArg);
let fileNameBase = this.host;
if(this._privKey){
2016-06-26 02:23:45 +00:00
let filePath = plugins.path.join(dirPathArg,fileNameBase);
plugins.smartfile.memory.toFsSync(this._privKey,filePath);
plugins.shelljs.chmod(600,filePath);
2016-06-25 12:13:26 +00:00
};
if (this._pubKey){
2016-06-26 02:23:45 +00:00
let filePath = plugins.path.join(dirPathArg,fileNameBase + ".pub");
plugins.smartfile.memory.toFsSync(this._pubKey,filePath);
plugins.shelljs.chmod(600,filePath);
2016-04-25 02:06:20 +00:00
}
}
2016-06-25 00:29:34 +00:00
}
let testKey = new SshKey();