summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/decodeClientDataJSON.ts
blob: 7c09b0d2516aaec78b11e7e39a386eaaedb369b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import base64url from 'base64url';

/**
 * Decode an authenticator's base64url-encoded clientDataJSON to JSON
 */
export default function decodeClientDataJSON(data: string): ClientDataJSON {
  const toString = base64url.decode(data);
  const clientData: ClientDataJSON = JSON.parse(toString);

  // `challenge` will be Base64URL-encoded here. Decode it for easier comparisons with what is provided
  // as the expected value
  clientData.challenge = Buffer.from(clientData.challenge, 'base64').toString('ascii');

  return clientData;
}

type ClientDataJSON = {
  type: string;
  challenge: string;
  origin: string;
};