MCPcopy
hub / github.com/panva/jose / decodeProtectedHeader

Function decodeProtectedHeader

src/util/decode_protected_header.ts:30–58  ·  view source on GitHub ↗
(token: string | object)

Source from the content-addressed store, hash-verified

28 * @param token JWE/JWS/JWT token in any JOSE serialization.
29 */
30export 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}

Calls 2

isObjectFunction · 0.85
decodeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…