summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/authentication/generateAuthenticationOptions.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/server/src/authentication/generateAuthenticationOptions.test.ts')
-rw-r--r--packages/server/src/authentication/generateAuthenticationOptions.test.ts140
1 files changed, 140 insertions, 0 deletions
diff --git a/packages/server/src/authentication/generateAuthenticationOptions.test.ts b/packages/server/src/authentication/generateAuthenticationOptions.test.ts
new file mode 100644
index 0000000..93cc398
--- /dev/null
+++ b/packages/server/src/authentication/generateAuthenticationOptions.test.ts
@@ -0,0 +1,140 @@
+jest.mock('../helpers/generateChallenge');
+
+import generateAssertionOptions from './generateAuthenticationOptions';
+
+test('should generate credential request options suitable for sending via JSON', () => {
+ const challenge = 'totallyrandomvalue';
+
+ const options = generateAssertionOptions({
+ allowCredentials: [
+ {
+ id: Buffer.from('1234', 'ascii'),
+ type: 'public-key',
+ transports: ['usb', 'nfc'],
+ },
+ {
+ id: Buffer.from('5678', 'ascii'),
+ type: 'public-key',
+ transports: ['internal'],
+ },
+ ],
+ timeout: 1,
+ challenge,
+ });
+
+ expect(options).toEqual({
+ // base64url-encoded
+ challenge: 'dG90YWxseXJhbmRvbXZhbHVl',
+ allowCredentials: [
+ {
+ id: 'MTIzNA',
+ type: 'public-key',
+ transports: ['usb', 'nfc'],
+ },
+ {
+ id: 'NTY3OA',
+ type: 'public-key',
+ transports: ['internal'],
+ },
+ ],
+ timeout: 1,
+ });
+});
+
+test('defaults to 60 seconds if no timeout is specified', () => {
+ const options = generateAssertionOptions({
+ challenge: 'totallyrandomvalue',
+ allowCredentials: [
+ { id: Buffer.from('1234', 'ascii'), type: 'public-key' },
+ { id: Buffer.from('5678', 'ascii'), type: 'public-key' },
+ ],
+ });
+
+ expect(options.timeout).toEqual(60000);
+});
+
+test('should not set userVerification if not specified', () => {
+ const options = generateAssertionOptions({
+ challenge: 'totallyrandomvalue',
+ allowCredentials: [
+ { id: Buffer.from('1234', 'ascii'), type: 'public-key' },
+ { id: Buffer.from('5678', 'ascii'), type: 'public-key' },
+ ],
+ });
+
+ expect(options.userVerification).toEqual(undefined);
+});
+
+test('should not set allowCredentials if not specified', () => {
+ const options = generateAssertionOptions({ rpID: 'test' });
+
+ expect(options.allowCredentials).toEqual(undefined);
+});
+
+test('should generate without params', () => {
+ const options = generateAssertionOptions();
+ const { challenge, ...otherFields } = options;
+ expect(otherFields).toEqual({
+ allowCredentials: undefined,
+ extensions: undefined,
+ rpId: undefined,
+ timeout: 60000,
+ userVerification: undefined,
+ });
+ expect(typeof challenge).toEqual('string');
+});
+
+test('should set userVerification if specified', () => {
+ const options = generateAssertionOptions({
+ challenge: 'totallyrandomvalue',
+ allowCredentials: [
+ { id: Buffer.from('1234', 'ascii'), type: 'public-key' },
+ { id: Buffer.from('5678', 'ascii'), type: 'public-key' },
+ ],
+ userVerification: 'required',
+ });
+
+ expect(options.userVerification).toEqual('required');
+});
+
+test('should set extensions if specified', () => {
+ const options = generateAssertionOptions({
+ challenge: 'totallyrandomvalue',
+ allowCredentials: [
+ { id: Buffer.from('1234', 'ascii'), type: 'public-key' },
+ { id: Buffer.from('5678', 'ascii'), type: 'public-key' },
+ ],
+ extensions: { appid: 'simplewebauthn' },
+ });
+
+ expect(options.extensions).toEqual({
+ appid: 'simplewebauthn',
+ });
+});
+
+test('should generate a challenge if one is not provided', () => {
+ const opts = {
+ allowCredentials: [
+ { id: Buffer.from('1234', 'ascii'), type: 'public-key' },
+ { id: Buffer.from('5678', 'ascii'), type: 'public-key' },
+ ],
+ };
+
+ // @ts-ignore 2345
+ const options = generateAssertionOptions(opts);
+
+ // base64url-encoded 16-byte buffer from mocked `generateChallenge()`
+ expect(options.challenge).toEqual('AQIDBAUGBwgJCgsMDQ4PEA');
+});
+
+test('should set rpId if specified', () => {
+ const rpID = 'simplewebauthn.dev';
+
+ const opts = generateAssertionOptions({
+ allowCredentials: [],
+ rpID,
+ });
+
+ expect(opts.rpId).toBeDefined();
+ expect(opts.rpId).toEqual(rpID);
+});