1 | import { ok as assert } from 'assert';
|
2 | import { decode as base64url } from './base64url.js';
|
3 | import { decoder } from '../lib/buffer_utils.js';
|
4 | import isObject from '../lib/is_object.js';
|
5 | function decodeProtectedHeader(token) {
|
6 | let protectedB64u;
|
7 | if (typeof token === 'string') {
|
8 | const parts = token.split('.');
|
9 | if (parts.length === 3 || parts.length === 5) {
|
10 | ;
|
11 | [protectedB64u] = parts;
|
12 | }
|
13 | }
|
14 | else if (typeof token === 'object' && token) {
|
15 | if ('protected' in token) {
|
16 | protectedB64u = token.protected;
|
17 | }
|
18 | else {
|
19 | throw new TypeError('Token does not contain a Protected Header');
|
20 | }
|
21 | }
|
22 | try {
|
23 | assert(typeof protectedB64u === 'string' && protectedB64u);
|
24 | const result = JSON.parse(decoder.decode(base64url(protectedB64u)));
|
25 | assert(isObject(result));
|
26 | return result;
|
27 | }
|
28 | catch (err) {
|
29 | throw new TypeError('Invalid Token or Protected Header formatting');
|
30 | }
|
31 | }
|
32 | export { decodeProtectedHeader };
|
33 | export default decodeProtectedHeader;
|