@neoskop/ethereal-secrets-client
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -6,2 +6,10 @@ export interface EtherealSecretsClientConfig { | ||
} | ||
export interface RemoteSaveResult { | ||
fragmentIdentifier: string; | ||
expiryDate?: Date; | ||
} | ||
export interface RemoteRetrieveResult { | ||
clearText: string; | ||
expiryDate?: Date; | ||
} | ||
export declare class EtherealSecretsClient { | ||
@@ -17,5 +25,5 @@ private _storage; | ||
saveLocal(key: string, clearText: string): Promise<void>; | ||
getRemote(fragmentIdentifier: string): Promise<string>; | ||
getRemote(fragmentIdentifier: string): Promise<RemoteRetrieveResult>; | ||
removeRemote(fragmentIdentifier: string): Promise<void>; | ||
saveRemote(clearText: string): Promise<string>; | ||
saveRemote(clearText: string): Promise<RemoteSaveResult>; | ||
private parseFragmentIdentifier(fragmentIdentifier); | ||
@@ -22,0 +30,0 @@ private generateLocalSecret(); |
@@ -38,29 +38,43 @@ "use strict"; | ||
EtherealSecretsClient.prototype.getRemote = function (fragmentIdentifier) { | ||
var keys = this.parseFragmentIdentifier(fragmentIdentifier); | ||
return this._agent | ||
.get(this._endpoint + keys.remoteKey) | ||
.accept("application/json") | ||
.then(function (res) { | ||
if (!res.body.hasOwnProperty('data')) { | ||
return Promise.reject('The server did not answer with any data'); | ||
} | ||
try { | ||
var clearText = sjcl.decrypt(keys.localKey, res.body.data); | ||
return Promise.resolve(clearText); | ||
} | ||
catch (err) { | ||
return Promise.reject(err.message); | ||
} | ||
}); | ||
try { | ||
var keys_1 = this.parseFragmentIdentifier(fragmentIdentifier); | ||
return this._agent | ||
.get(this._endpoint + keys_1.remoteKey) | ||
.accept("application/json") | ||
.then(function (res) { | ||
if (!res.body.hasOwnProperty('data')) { | ||
return Promise.reject('The server did not answer with any data'); | ||
} | ||
try { | ||
var clearText = sjcl.decrypt(keys_1.localKey, res.body.data); | ||
var result = { | ||
clearText: clearText | ||
}; | ||
if (res.body.hasOwnProperty('expiryDate')) { | ||
result.expiryDate = new Date(res.body.expiryDate); | ||
} | ||
return Promise.resolve(result); | ||
} | ||
catch (err) { | ||
return Promise.reject(err.message); | ||
} | ||
}); | ||
} | ||
catch (err) { | ||
return Promise.reject(err.message); | ||
} | ||
}; | ||
EtherealSecretsClient.prototype.removeRemote = function (fragmentIdentifier) { | ||
var keys = this.parseFragmentIdentifier(fragmentIdentifier); | ||
return this._agent | ||
.del(this._endpoint + keys.remoteKey) | ||
.accept("application/json") | ||
.then(function () { | ||
return Promise.resolve(); | ||
}).catch(function (err) { | ||
try { | ||
var keys = this.parseFragmentIdentifier(fragmentIdentifier); | ||
return this._agent | ||
.del(this._endpoint + keys.remoteKey) | ||
.accept("application/json") | ||
.then(function () { | ||
return Promise.resolve(); | ||
}); | ||
} | ||
catch (err) { | ||
return Promise.reject(err.message); | ||
}); | ||
} | ||
}; | ||
@@ -78,3 +92,11 @@ EtherealSecretsClient.prototype.saveRemote = function (clearText) { | ||
} | ||
return Promise.resolve(res.body.key + ';' + secret); | ||
var result = { | ||
fragmentIdentifier: res.body.key + ';' + secret | ||
}; | ||
if (res.body.hasOwnProperty('expiryDate')) { | ||
result.expiryDate = new Date(res.body.expiryDate); | ||
} | ||
return Promise.resolve(result); | ||
}).catch(function (err) { | ||
return Promise.reject(err.message); | ||
}); | ||
@@ -95,3 +117,11 @@ }; | ||
if (typeof window !== 'undefined') { | ||
window.crypto.getRandomValues(randomValues); | ||
if ('crypto' in window) { | ||
window.crypto.getRandomValues(randomValues); | ||
} | ||
else if ('msCrypto' in window) { | ||
window.msCrypto.getRandomValues(randomValues); | ||
} | ||
else { | ||
throw new Error('No secure source of randomness'); | ||
} | ||
} | ||
@@ -125,3 +155,7 @@ else { | ||
} | ||
return this._agent.get(this._endpoint).withCredentials().accept("application/json").then(function (res) { | ||
return this._agent | ||
.get(this._endpoint) | ||
.withCredentials() | ||
.accept("application/json") | ||
.then(function (res) { | ||
if (!res.body.hasOwnProperty('key')) { | ||
@@ -134,2 +168,4 @@ return Promise.reject('The server did not answer with a key'); | ||
return Promise.resolve(res.body.key); | ||
}).catch(function (err) { | ||
return Promise.reject(err.message); | ||
}); | ||
@@ -136,0 +172,0 @@ }; |
@@ -64,6 +64,6 @@ "use strict"; | ||
var sut = createClient(); | ||
return sut.saveRemote('foo').should.eventually.be.fulfilled.and.then(function (fragmentIdentifier) { | ||
return sut.saveRemote('foo').should.eventually.be.fulfilled.and.then(function (result) { | ||
return Promise.all([ | ||
chai.expect(fragmentIdentifier).should.not.be.empty, | ||
chai.expect(fragmentIdentifier).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12};[0-9a-f]{64}$/) | ||
chai.expect(result.fragmentIdentifier).should.not.be.empty, | ||
chai.expect(result.fragmentIdentifier).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12};[0-9a-f]{64}$/) | ||
]); | ||
@@ -74,13 +74,21 @@ }); | ||
var sut = createClient(); | ||
return sut.saveRemote('foo').should.eventually.be.fulfilled.and.then(function (fragmentIdentifier) { | ||
return sut.getRemote(fragmentIdentifier).should.eventually.be.fulfilled.and.then(function (clearText) { | ||
return clearText.should.equal('foo'); | ||
return sut.saveRemote('foo').should.eventually.be.fulfilled.and.then(function (result) { | ||
return sut.getRemote(result.fragmentIdentifier).should.eventually.be.fulfilled.and.then(function (result) { | ||
return result.clearText.should.equal('foo'); | ||
}); | ||
}); | ||
}); | ||
it('should reject invalid fragment identifier', function () { | ||
var sut = createClient(); | ||
return sut.getRemote('invalididentifier').should.eventually.be.rejectedWith('invalid'); | ||
}); | ||
it('should reject unknown fragment identifier', function () { | ||
var sut = createClient(); | ||
return sut.getRemote('decafbad-dead-dead-dead-decafbadadad;decafbaddecafbaddecafbaddecafbaddecafbaddecafbaddecafbaddecafbad').should.eventually.be.rejectedWith('Not Found'); | ||
}); | ||
it('should not return data when the data was deleted beforehand', function () { | ||
var sut = createClient(); | ||
return sut.saveRemote('foo').should.eventually.be.fulfilled.and.then(function (fragmentIdentifier) { | ||
return sut.removeRemote(fragmentIdentifier).then(function () { | ||
return sut.getRemote(fragmentIdentifier).should.eventually.be.rejected; | ||
return sut.saveRemote('foo').should.eventually.be.fulfilled.and.then(function (result) { | ||
return sut.removeRemote(result.fragmentIdentifier).then(function () { | ||
return sut.getRemote(result.fragmentIdentifier).should.eventually.be.rejected; | ||
}); | ||
@@ -87,0 +95,0 @@ }); |
{ | ||
"name": "@neoskop/ethereal-secrets-client", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "typings": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
25691
297