-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #514 from snyk/refactoring/unify-hash-token-function
refactor: unify hash token token
- Loading branch information
Showing
5 changed files
with
57 additions
and
31 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
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
import crypto = require('crypto'); | ||
|
||
export function hashToken(token: string): string { | ||
const shasum = crypto.createHash('sha256'); | ||
shasum.update(token); | ||
return shasum.digest('hex'); | ||
} | ||
|
||
export function maskToken(token) { | ||
if (!token || token === '') { | ||
return ''; | ||
} | ||
|
||
return token.slice(0, 4) + '-...-' + token.slice(-4); | ||
} |
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,36 @@ | ||
import { hashToken, maskToken } from '../../lib/token'; | ||
|
||
describe('token', () => { | ||
describe('hashToken', () => { | ||
it('should hash input tokens using sha256', async () => { | ||
const expectedSha256ForEmptyString = | ||
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'; | ||
const expectedSha256ForTokenWord = | ||
'3c469e9d6c5875d37a43f353d4f88e61fcf812c66eee3457465a40b0da4153e0'; | ||
|
||
const hashedTokenForEmptyString = hashToken(''); | ||
const hashedTokenForTokenWord = hashToken('token'); | ||
|
||
expect(hashedTokenForEmptyString).toEqual(expectedSha256ForEmptyString); | ||
expect(hashedTokenForTokenWord).toEqual(expectedSha256ForTokenWord); | ||
}); | ||
}); | ||
|
||
describe('maskToken', () => { | ||
it('should return empty string if token is empty or null', async () => { | ||
const maskedTokenForEmpty = maskToken(''); | ||
const maskedTokenForNull = maskToken(''); | ||
|
||
expect(maskedTokenForEmpty).toEqual(''); | ||
expect(maskedTokenForNull).toEqual(''); | ||
}); | ||
|
||
it('should return four first and last characters when masking', async () => { | ||
const maskedToken = maskToken('12345'); | ||
const maskedUUIDToken = maskToken('aaaabbbb-0160-4126-a00d-ccccccccdddd'); | ||
|
||
expect(maskedToken).toEqual('1234-...-2345'); | ||
expect(maskedUUIDToken).toEqual('aaaa-...-dddd'); | ||
}); | ||
}); | ||
}); |