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
|
|
|
|
2016-05-31 17:16:45 +00:00
|
|
|
export class SshKey {
|
2016-04-25 02:06:20 +00:00
|
|
|
private privKey:string;
|
|
|
|
private pubKey:string;
|
2016-04-26 02:44:50 +00:00
|
|
|
private hostVar:string;
|
|
|
|
constructor(optionsArg:{private?:string,public?:string,host?:string}={}){
|
2016-04-25 02:06:20 +00:00
|
|
|
this.privKey = optionsArg.private;
|
|
|
|
this.pubKey = optionsArg.public;
|
2016-04-26 02:44:50 +00:00
|
|
|
this.hostVar = optionsArg.host;
|
2016-04-25 02:06:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// getters
|
2016-04-26 02:44:50 +00:00
|
|
|
get host(){
|
|
|
|
return this.hostVar;
|
|
|
|
};
|
2016-04-25 02:06:20 +00:00
|
|
|
get privateKey(){
|
|
|
|
return this.privKey;
|
|
|
|
};
|
|
|
|
get privateKeyBase64(){
|
|
|
|
return plugins.base64.encode(this.privKey);
|
|
|
|
}
|
|
|
|
get publicKey(){
|
|
|
|
return this.publicKey;
|
|
|
|
}
|
|
|
|
get publicKeyBase64(){
|
|
|
|
return plugins.base64.encode(this.pubKey);
|
|
|
|
}
|
|
|
|
get type(){
|
|
|
|
if(this.privKey && this.pubKey){
|
|
|
|
return "duplex";
|
|
|
|
} else if(this.privKey){
|
|
|
|
return "private";
|
|
|
|
} else if(this.pubKey){
|
|
|
|
return "public";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// setters
|
2016-04-26 02:44:50 +00:00
|
|
|
set host(hostArg:string){
|
|
|
|
this.hostVar = hostArg;
|
|
|
|
};
|
2016-04-25 02:06:20 +00:00
|
|
|
set privateKey(privateKeyArg:string){
|
|
|
|
this.privKey = privateKeyArg;
|
|
|
|
};
|
|
|
|
// setters
|
|
|
|
set publicKey(publicKeyArg:string){
|
|
|
|
this.pubKey = publicKeyArg;
|
|
|
|
};
|
|
|
|
|
|
|
|
store(filePathArg?:string){
|
|
|
|
let filePathObj = plugins.path.parse(filePathArg);
|
|
|
|
if(filePathObj.ext = ".priv"){
|
|
|
|
plugins.smartfile.memory.toFsSync(this.privKey,{fileName:filePathObj.name + filePathObj.ext,filePath:filePathObj.dir});
|
|
|
|
} else if (filePathObj.ext = ".pub"){
|
|
|
|
plugins.smartfile.memory.toFsSync(this.pubKey,{fileName:filePathObj.name + filePathObj.ext,filePath:filePathObj.dir});
|
|
|
|
} else { //we assume we are given a directory as filePathArg, so we store the whole key
|
|
|
|
plugins.fs.ensureDirSync(filePathObj.dir);
|
|
|
|
this.store(plugins.path.join(filePathObj.dir,"key.priv")); // call this function recursivly
|
|
|
|
this.store(plugins.path.join(filePathObj.dir,"key.priv")); // call this function recursivly
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|