smartssh/ts/smartssh.classes.sshkey.ts

100 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-11-23 11:38:38 +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-11-23 11:38:38 +00:00
private _privKey: string
private _pubKey: string
private _hostVar: string
private _authorized: boolean
/**
* 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-04-25 02:06:20 +00:00
};
2016-11-23 11:38:38 +00:00
2016-06-25 00:29:34 +00:00
// this.host
2016-04-26 02:44:50 +00:00
get host(){
2016-11-23 11:38:38 +00:00
return this._hostVar
2016-04-26 02:44:50 +00:00
};
2016-11-23 11:38:38 +00:00
set host(hostArg: string){
this._hostVar = hostArg
2016-06-25 00:29:34 +00:00
};
// this.privKey
get privKey(){
2016-11-23 11:38:38 +00:00
return this._privKey
2016-04-25 02:06:20 +00:00
};
2016-11-23 11:38:38 +00:00
set privKey(privateKeyArg: string){
this._privKey = privateKeyArg
2016-06-25 00:29:34 +00:00
};
// this.privKeyBase64
get privKeyBase64(){
2016-11-23 11:38:38 +00:00
return plugins.smartstring.base64.encode(this._privKey)
2016-04-25 02:06:20 +00:00
}
2016-11-23 11:38:38 +00:00
set privKeyBase64(privateKeyArg: string) {
this._privKey = plugins.smartstring.base64.decode(privateKeyArg)
2016-04-25 02:06:20 +00:00
}
2016-06-25 00:29:34 +00:00
// this.pubKey
get pubKey(){
2016-11-23 11:38:38 +00:00
return this._pubKey
2016-04-25 02:06:20 +00:00
}
2016-11-23 11:38:38 +00:00
set pubKey(publicKeyArg: string){
this._pubKey = publicKeyArg
2016-06-25 00:29:34 +00:00
};
// this.pubKeyBase64
get pubKeyBase64(){
2016-11-23 11:38:38 +00:00
return plugins.smartstring.base64.encode(this._pubKey)
2016-06-25 00:29:34 +00:00
}
2016-11-23 11:38:38 +00:00
set pubKeyBase64(publicKeyArg: string) {
this._pubKey = plugins.smartstring.base64.decode(publicKeyArg)
2016-06-25 00:29:34 +00:00
}
2016-06-25 13:30:57 +00:00
get authorized(){
2016-11-23 11:38:38 +00:00
return this._authorized
2016-06-25 13:30:57 +00:00
}
2016-11-23 11:38:38 +00:00
set authorized(authorizedArg: boolean){
this._authorized = authorizedArg
2016-06-25 13:30:57 +00:00
}
2016-04-25 02:06:20 +00:00
get type(){
2016-11-23 11:38:38 +00:00
if (this._privKey && this._pubKey) {
return 'duplex'
} else if (this._privKey) {
return 'private'
} else if (this._pubKey) {
return 'public'
2016-04-25 02:06:20 +00:00
}
};
2016-11-23 11:38:38 +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-11-23 11:38:38 +00:00
read(filePathArg) {
2016-06-24 18:58:55 +00:00
}
2016-11-23 11:38:38 +00:00
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)
plugins.shelljs.chmod(600,filePath)
2016-06-25 12:13:26 +00:00
};
2016-11-23 11:38:38 +00:00
if (this._pubKey) {
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
}