-
Notifications
You must be signed in to change notification settings - Fork 747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a CryptoProvider interface and NodeCryptoProvider implementation. #1237
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('CryptoProviderTestSuite', () => { | ||
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; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be evil, but for async support:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. I think this could potentially simplify the implementation over having two separate methods... let's think about it and see how the need comes up? Either change would require updating the types though I imagine changing
constructEvent
's return type fromStripe.Event
toStripe.Event | Promise<Stripe.Event>
wouldn't technically be backwards compatible?Think we can merge this and handle async in a separate PR.