-
Notifications
You must be signed in to change notification settings - Fork 123
Add a user-data class for generating intercomSettings #173
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,62 @@ | ||
| import { IdentityVerification } from './index'; | ||
| import htmlencode from 'htmlencode'; | ||
|
|
||
| export default class UserData { | ||
| constructor(settings) { | ||
| this.loggedOut = !settings.user_id && !settings.email; | ||
|
|
||
| if (!settings.app_id) { | ||
| throw new Error('You must provide an app_id in your Intercom settings'); | ||
| } | ||
| if (!this.loggedOut && !settings.verificationSecret) { | ||
| throw new Error('You must provide your verification secret in your Intercom settings'); | ||
| } | ||
|
|
||
| this.settings = settings; | ||
| } | ||
| json() { | ||
| this.setUserHash(); | ||
| return this.escapedSettings(this.settings); | ||
| } | ||
| getVerificationSecret() { | ||
| const { verificationSecret } = this.settings; | ||
| delete this.settings.verificationSecret; | ||
| return verificationSecret; | ||
| } | ||
| setUserHash() { | ||
| if (this.loggedOut || this.settings.user_hash !== undefined) { | ||
| return; | ||
| } | ||
|
|
||
| const { verificationSecret } = this.settings; | ||
| delete this.settings.verificationSecret; | ||
| const identifier = this.settings.user_id ? this.settings.user_id.toString() : this.settings.email; | ||
|
|
||
|
|
||
| const userHash = IdentityVerification.userHash({ | ||
| secretKey: verificationSecret, | ||
| identifier | ||
| }); | ||
|
|
||
| this.settings.user_hash = userHash; | ||
| } | ||
| escapedSettings(settings) { | ||
| const intercomSettings = {}; | ||
| Object.keys(settings).map(key => { | ||
| if (typeof settings[key] === 'object' && settings[key] !== null) { | ||
| intercomSettings[key] = this.escapedSettings(settings[key]); | ||
| } else { | ||
| const escapedKey = this.escapeString(key); | ||
| const value = this.escapeString(settings[key]); | ||
| intercomSettings[escapedKey] = value; | ||
| } | ||
| }); | ||
| return intercomSettings; | ||
| } | ||
| escapeString(string) { | ||
| if (typeof string === 'string') { | ||
| string = htmlencode.htmlEncode(string).replace(/\"/gi, '\\"'); | ||
| } | ||
| return string; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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,166 @@ | ||
| import UserData from '../lib/user-data'; | ||
| import { IdentityVerification } from '../lib/index'; | ||
| import assert from 'assert'; | ||
| import sinon from 'sinon'; | ||
|
|
||
| describe('userData', () => { | ||
| it('should be able to grab the verification secret', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| user_id: 1 | ||
| }; | ||
| const userData = new UserData(settings); | ||
| assert.equal(userData.getVerificationSecret(), 'abc123'); | ||
| }); | ||
|
|
||
| it('should grab the user_id as the identifier', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| user_id: 1, | ||
| email: 'jess@intercom.io' | ||
| }; | ||
| const userData = new UserData(settings); | ||
| const userHash = sinon.spy(IdentityVerification, 'userHash'); | ||
| userData.json(); | ||
| userHash.restore(); | ||
| sinon.assert.calledWith(userHash, { | ||
| secretKey: 'abc123', | ||
| identifier: '1' | ||
| }); | ||
| }); | ||
|
|
||
| it('should grab the email as the identifier if no user_id', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| email: 'jess@intercom.io' | ||
| }; | ||
| const userData = new UserData(settings); | ||
| const userHash = sinon.spy(IdentityVerification, 'userHash'); | ||
| userData.json(); | ||
| userHash.restore(); | ||
| sinon.assert.calledWith(userHash, { | ||
| secretKey: 'abc123', | ||
| identifier: 'jess@intercom.io' | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw an error if there\'s no verification secret', () => { | ||
| const settings = { | ||
| app_id: 'xyz789', | ||
| user_id: 1 | ||
| }; | ||
| assert.throws(() => new UserData(settings), Error); | ||
| }); | ||
|
|
||
| it('should error if there\'s no app_id', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| user_id: 1 | ||
| }; | ||
| assert.throws(() => new UserData(settings), Error); | ||
| }); | ||
|
|
||
| it('should return the logged out userData if no identifier', () => { | ||
| const settings = { | ||
| app_id: 'xyz789' | ||
| }; | ||
| const userData = new UserData(settings); | ||
| assert.equal(JSON.stringify(userData.json()), JSON.stringify({ app_id: 'xyz789'})); | ||
| }); | ||
|
|
||
| it('should escape bad stuff in values', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| email: 'jess@"<script>alert(1)</script>intercom.io' | ||
| }; | ||
| const userData = new UserData(settings); | ||
| assert.equal(userData.json().email, 'jess@\\"<script>alert(1)</script>intercom.io'); | ||
| }); | ||
|
|
||
| it('should escape bad stuff in object keys', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| user_id: '1' | ||
| }; | ||
| settings['<script>doSomethingEvil();</script>'] = 'jess@"<script>alert(1)</script>intercom.io'; | ||
| const userData = new UserData(settings); | ||
| const result = userData.json(); | ||
| assert.equal(result['<script>doSomethingEvil();</script>'], 'jess@\\\"<script>alert(1)</script>intercom.io'); | ||
| }); | ||
|
|
||
| it('should escape bad stuff in next object keys and values', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| user_id: '1' | ||
| }; | ||
| settings.maliciousSettings = { | ||
| '<script>doSomethingEvil();</script>': 'jess@"<script>alert(1)</script>intercom.io' | ||
| }; | ||
| const userData = new UserData(settings); | ||
| const result = userData.json(); | ||
| assert.equal(result.maliciousSettings['<script>doSomethingEvil();</script>'], 'jess@\\"<script>alert(1)</script>intercom.io'); | ||
| }); | ||
|
|
||
| it('should not include the verification secret in the userData', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| user_id: 1, | ||
| email: 'jess@intercom.io', | ||
| name: 'Jess OB', | ||
| company: { | ||
| id: 123, | ||
| name: 'Intercom' | ||
| } | ||
| }; | ||
| const userData = new UserData(settings); | ||
| const result = userData.json(); | ||
| assert.equal(Object.keys(result).indexOf(settings.verificationSecret), -1); | ||
| }); | ||
|
|
||
| it('should skip setUserHash if user_hash is already defined', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| user_id: 1 | ||
| }; | ||
| const userData = new UserData(settings); | ||
| const userHash = sinon.spy(IdentityVerification, 'userHash'); | ||
| userData.json(); | ||
| userData.json(); | ||
| userHash.restore(); | ||
| sinon.assert.calledOnce(userHash); | ||
| }); | ||
|
|
||
| it('should return the userData', () => { | ||
| const settings = { | ||
| verificationSecret: 'abc123', | ||
| app_id: 'xyz789', | ||
| user_id: 1, | ||
| email: 'jess@intercom.io', | ||
| name: 'Jess OB', | ||
| company: { | ||
| id: 123, | ||
| name: 'Intercom' | ||
| } | ||
| }; | ||
| const userData = new UserData(settings); | ||
| assert.equal(JSON.stringify(userData.json()), JSON.stringify({ | ||
| app_id: 'xyz789', | ||
| user_id: 1, | ||
| email: 'jess@intercom.io', | ||
| name: 'Jess OB', | ||
| company: { | ||
| id: 123, | ||
| name: 'Intercom' | ||
| }, | ||
| user_hash: 'f02877f24c9dd37542268a28627ebaf2e07d0d114d9482abcdc20f60874b40b3' | ||
| })); | ||
| }); | ||
| }); |
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.
If we're going to delete the secret, we should ensure json can be called multiple times by skipping
setUserHashifuser_hashis already set inthis.settingsThere 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.
Updated!