-
Notifications
You must be signed in to change notification settings - Fork 751
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a CryptoProvider interface and NodeCryptoProvider implementation.
- Loading branch information
1 parent
b4298e6
commit 8f1d3a9
Showing
11 changed files
with
223 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Interface encapsulating the various crypto computations used by the library, | ||
* allowing pluggable underlying crypto implementations. | ||
*/ | ||
class CryptoProvider { | ||
/** | ||
* Computes a SHA-256 HMAC given a secret and a payload (encoded in UTF-8). | ||
* The output HMAC should be encoded in hexadecimal. | ||
* | ||
* Sample values for implementations: | ||
* - computeHMACSignature('', 'test_secret') => 'f7f9bd47fb987337b5796fdc1fdb9ba221d0d5396814bfcaf9521f43fd8927fd' | ||
* - computeHMACSignature('\ud83d\ude00', 'test_secret') => '837da296d05c4fe31f61d5d7ead035099d9585a5bcde87de952012a78f0b0c43 | ||
*/ | ||
computeHMACSignature(payload, secret) { | ||
throw new Error('computeHMACSignature not implemented.'); | ||
} | ||
} | ||
|
||
module.exports = CryptoProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const crypto = require('crypto'); | ||
|
||
const CryptoProvider = require('./CryptoProvider'); | ||
|
||
/** | ||
* `CryptoProvider which uses the Node `crypto` package for its computations. | ||
*/ | ||
class NodeCryptoProvider extends CryptoProvider { | ||
/** @override */ | ||
computeHMACSignature(payload, secret) { | ||
return crypto | ||
.createHmac('sha256', secret) | ||
.update(payload, 'utf8') | ||
.digest('hex'); | ||
} | ||
} | ||
|
||
module.exports = NodeCryptoProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
const NodeCryptoProvider = require('../../lib/crypto/NodeCryptoProvider'); | ||
|
||
const {createCryptoProviderTestSuite} = require('./helpers'); | ||
|
||
describe('NodeCryptoProvider', () => { | ||
createCryptoProviderTestSuite(new NodeCryptoProvider()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
|
||
const expect = require('chai').expect; | ||
|
||
const SECRET = 'test_secret'; | ||
|
||
/** | ||
* Test runner which runs a common set of tests for a given CryptoProvider to | ||
* make sure it satisfies the expected contract. | ||
*/ | ||
const createCryptoProviderTestSuite = (cryptoProvider) => { | ||
describe('common tests', () => { | ||
describe('computeHMACSignature', () => { | ||
it('empty payload', () => { | ||
expect(cryptoProvider.computeHMACSignature('', SECRET)).to.equal( | ||
'f7f9bd47fb987337b5796fdc1fdb9ba221d0d5396814bfcaf9521f43fd8927fd' | ||
); | ||
}); | ||
|
||
it('sample payload', () => { | ||
expect( | ||
cryptoProvider.computeHMACSignature( | ||
JSON.stringify({obj1: 'hello', obj2: 'world'}), | ||
SECRET | ||
) | ||
).to.equal( | ||
'bebb1a643997f419b315ddba19e6f5411e1ce7f810ba6d3617ce72823092f363' | ||
); | ||
}); | ||
|
||
it('payload with utf-8', () => { | ||
expect( | ||
cryptoProvider.computeHMACSignature('\ud83d\ude00', SECRET) | ||
).to.equal( | ||
'837da296d05c4fe31f61d5d7ead035099d9585a5bcde87de952012a78f0b0c43' | ||
); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports = {createCryptoProviderTestSuite}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
declare module 'stripe' { | ||
namespace Stripe { | ||
/** | ||
* Interface encapsulating the various crypto computations used by the library, | ||
* allowing pluggable underlying crypto implementations. | ||
*/ | ||
export interface CryptoProvider { | ||
/** | ||
* Computes a SHA-256 HMAC given a secret and a payload (encoded in UTF-8). | ||
* The output HMAC should be encoded in hexadecimal. | ||
* | ||
* Sample values for implementations: | ||
* - computeHMACSignature('', 'test_secret') => 'f7f9bd47fb987337b5796fdc1fdb9ba221d0d5396814bfcaf9521f43fd8927fd' | ||
* - computeHMACSignature('\ud83d\ude00', 'test_secret') => '837da296d05c4fe31f61d5d7ead035099d9585a5bcde87de952012a78f0b0c43 | ||
*/ | ||
computeHMACSignature: (payload: string, secret: string) => string; | ||
} | ||
} | ||
} |