-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
138 additions
and
25 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 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,12 @@ | ||
import crypto from 'crypto'; | ||
import randomBytes from './randomBytes'; | ||
|
||
/** | ||
* Crypto replacement for expo | ||
* | ||
* @module otplib-expo/crypto | ||
*/ | ||
export default { | ||
createHmac: crypto.createHmac, | ||
randomBytes | ||
}; |
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 @@ | ||
import crypto from './crypto'; | ||
|
||
describe('crypto', () => { | ||
it('should expose an object with a used method', () => { | ||
expect(typeof crypto).toBe('object'); | ||
expect(typeof crypto.createHmac).toBe('function'); | ||
expect(typeof crypto.randomBytes).toBe('function'); | ||
}); | ||
}); |
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,22 @@ | ||
import hotp from 'otplib-hotp'; | ||
import totp from 'otplib-totp'; | ||
import authenticator from 'otplib-authenticator'; | ||
import crypto from './crypto'; | ||
|
||
/** | ||
* otplib-expo | ||
* | ||
* One-Time Password Library for use with https://expo.io | ||
* | ||
* @module otplib-expo | ||
* @since 10.0.0 | ||
*/ | ||
authenticator.options = { crypto }; | ||
hotp.options = { crypto }; | ||
totp.options = { crypto }; | ||
|
||
module.exports = { | ||
authenticator, | ||
hotp, | ||
totp | ||
}; |
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,31 @@ | ||
/** | ||
* randomBytes naive implementation. | ||
* | ||
* Required for expo applications that don't offer a native | ||
* secure random implementaton | ||
* | ||
* @module otplib-expo/randomBytes | ||
* @param {string} size - the size | ||
* @return {Buffer} | ||
*/ | ||
function randomBytes(size) { | ||
if (size > 65536) { | ||
throw new Error('Requested size of random bytes is too large'); | ||
} | ||
|
||
if (size < 1) { | ||
throw new Error('Requested size must be more than 0'); | ||
} | ||
|
||
const rawBytes = new Uint8Array(size).map(() => randomUint8()); | ||
|
||
return new Buffer(rawBytes.buffer); | ||
} | ||
|
||
function randomUint8() { | ||
const low = 0; | ||
const high = 255; | ||
return Math.floor(Math.random() * (high - low + 1) + low); | ||
} | ||
|
||
export default randomBytes; |
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 @@ | ||
import randomBytes from './randomBytes'; | ||
|
||
describe('randomBytes', () => { | ||
const tooLarge = 'Requested size of random bytes is too large'; | ||
const wrongSize = 'Requested size must be more than 0'; | ||
|
||
it('should return a buffer', () => { | ||
const result = randomBytes(10); | ||
expect(result).toBeInstanceOf(Buffer); | ||
expect(result).toHaveLength(10); | ||
}); | ||
|
||
it('should throw when size is too big', () => { | ||
expect(() => randomBytes(65537)).toThrowError(tooLarge); | ||
}); | ||
|
||
it('should throw when size is < 1', () => { | ||
expect(() => randomBytes(0)).toThrowError(wrongSize); | ||
}); | ||
}); |