2016-05-31 19:00:52 +02:00
|
|
|
import "typings-global";
|
|
|
|
import * as plugins from "./smartssh.plugins";
|
|
|
|
import * as helpers from "./smartssh.classes.helpers";
|
2016-04-25 04:06:20 +02:00
|
|
|
|
2016-05-31 19:16:45 +02:00
|
|
|
export class SshKey {
|
2016-04-25 04:06:20 +02:00
|
|
|
private privKey:string;
|
|
|
|
private pubKey:string;
|
2016-04-26 04:44:50 +02:00
|
|
|
private hostVar:string;
|
2016-06-25 02:10:53 +02:00
|
|
|
private authorized:boolean;
|
|
|
|
constructor(optionsArg:{private?:string,public?:string,host?:string,authorized?:boolean}={}){
|
2016-04-25 04:06:20 +02:00
|
|
|
this.privKey = optionsArg.private;
|
|
|
|
this.pubKey = optionsArg.public;
|
2016-04-26 04:44:50 +02:00
|
|
|
this.hostVar = optionsArg.host;
|
2016-06-25 02:10:53 +02:00
|
|
|
this.authorized = optionsArg.authorized;
|
2016-04-25 04:06:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// getters
|
2016-04-26 04:44:50 +02:00
|
|
|
get host(){
|
|
|
|
return this.hostVar;
|
|
|
|
};
|
2016-04-25 04:06:20 +02:00
|
|
|
get privateKey(){
|
|
|
|
return this.privKey;
|
|
|
|
};
|
|
|
|
get privateKeyBase64(){
|
|
|
|
return plugins.base64.encode(this.privKey);
|
|
|
|
}
|
|
|
|
get publicKey(){
|
2016-06-01 04:18:31 +02:00
|
|
|
return this.pubKey;
|
2016-04-25 04:06:20 +02:00
|
|
|
}
|
|
|
|
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 04:44:50 +02:00
|
|
|
set host(hostArg:string){
|
|
|
|
this.hostVar = hostArg;
|
|
|
|
};
|
2016-04-25 04:06:20 +02:00
|
|
|
set privateKey(privateKeyArg:string){
|
|
|
|
this.privKey = privateKeyArg;
|
|
|
|
};
|
2016-06-14 00:11:20 +02:00
|
|
|
|
|
|
|
set privateKeyBase64(privateKeyArg:string) {
|
|
|
|
this.privKey = plugins.base64.decode(privateKeyArg);
|
|
|
|
}
|
|
|
|
|
2016-04-25 04:06:20 +02:00
|
|
|
set publicKey(publicKeyArg:string){
|
|
|
|
this.pubKey = publicKeyArg;
|
|
|
|
};
|
2016-06-14 00:11:20 +02:00
|
|
|
|
|
|
|
set publicKeyBase64(publicKeyArg:string) {
|
|
|
|
this.pubKey = plugins.base64.decode(publicKeyArg);
|
|
|
|
}
|
2016-06-24 20:58:55 +02:00
|
|
|
read(filePathArg){
|
|
|
|
|
|
|
|
}
|
2016-04-25 04:06:20 +02:00
|
|
|
store(filePathArg?:string){
|
|
|
|
let filePathObj = plugins.path.parse(filePathArg);
|
|
|
|
if(filePathObj.ext = ".priv"){
|
2016-06-24 20:58:55 +02:00
|
|
|
plugins.smartfile.memory.toFsSync(this.privKey,filePathArg);
|
2016-04-25 04:06:20 +02:00
|
|
|
} else if (filePathObj.ext = ".pub"){
|
2016-06-24 20:58:55 +02:00
|
|
|
plugins.smartfile.memory.toFsSync(this.pubKey,filePathArg);
|
2016-04-25 04:06:20 +02:00
|
|
|
} 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
|
2016-06-14 00:27:55 +02:00
|
|
|
this.store(plugins.path.join(filePathObj.dir,"key.pub")); // call this function recursivly
|
2016-04-25 04:06:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|