summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/authentication/generateAuthenticationOptions.test.ts
blob: 65dc66e1a856fdfbd5f8297a853eefad8015391f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
jest.mock('../helpers/generateChallenge');

import { isoBase64URL } from '../helpers/iso';

import { generateAuthenticationOptions } from './generateAuthenticationOptions';

const challengeString = 'dG90YWxseXJhbmRvbXZhbHVl';
const challengeBuffer = isoBase64URL.toBuffer(challengeString)

test('should generate credential request options suitable for sending via JSON', () => {

  const options = generateAuthenticationOptions({
    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: challengeBuffer,
  });

  expect(options).toEqual({
    // base64url-encoded
    challenge: challengeString,
    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 = generateAuthenticationOptions({
    challenge: challengeBuffer,
    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 = generateAuthenticationOptions({
    challenge: challengeBuffer,
    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 = generateAuthenticationOptions({ rpID: 'test' });

  expect(options.allowCredentials).toEqual(undefined);
});

test('should generate without params', () => {
  const options = generateAuthenticationOptions();
  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 = generateAuthenticationOptions({
    challenge: challengeBuffer,
    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 = generateAuthenticationOptions({
    challenge: challengeBuffer,
    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 = generateAuthenticationOptions(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 = generateAuthenticationOptions({
    allowCredentials: [],
    rpID,
  });

  expect(opts.rpId).toBeDefined();
  expect(opts.rpId).toEqual(rpID);
});