(token: string | object)
| 28 | * @param token JWE/JWS/JWT token in any JOSE serialization. |
| 29 | */ |
| 30 | export function decodeProtectedHeader(token: string | object): ProtectedHeaderParameters { |
| 31 | let protectedB64u!: unknown |
| 32 | |
| 33 | if (typeof token === 'string') { |
| 34 | const parts = token.split('.') |
| 35 | if (parts.length === 3 || parts.length === 5) { |
| 36 | ;[protectedB64u] = parts |
| 37 | } |
| 38 | } else if (typeof token === 'object' && token) { |
| 39 | if ('protected' in token) { |
| 40 | protectedB64u = token.protected |
| 41 | } else { |
| 42 | throw new TypeError('Token does not contain a Protected Header') |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | try { |
| 47 | if (typeof protectedB64u !== 'string' || !protectedB64u) { |
| 48 | throw new Error() |
| 49 | } |
| 50 | const result = JSON.parse(decoder.decode(b64u(protectedB64u!))) |
| 51 | if (!isObject(result)) { |
| 52 | throw new Error() |
| 53 | } |
| 54 | return result as ProtectedHeaderParameters |
| 55 | } catch { |
| 56 | throw new TypeError('Invalid Token or Protected Header formatting') |
| 57 | } |
| 58 | } |
no test coverage detected
searching dependent graphs…