Skip to content

Commit

Permalink
feat(auth): add content type and length to signature headers (#804)
Browse files Browse the repository at this point in the history
  • Loading branch information
njlie authored Nov 30, 2022
1 parent 3a3f9e8 commit 55d11d1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
25 changes: 14 additions & 11 deletions packages/auth/src/tests/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ export async function createContextWithSigHeaders(
container?: IocContract<AppServices>
): Promise<AppContext> {
const { headers, url, method } = reqOpts
const { signature, sigInput, contentDigest } = await generateSigHeaders({
privateKey,
keyId,
url,
method,
optionalComponents: {
body: requestBody,
authorization: headers.Authorization as string
}
})
const { signature, sigInput, contentDigest, contentLength, contentType } =
await generateSigHeaders({
privateKey,
keyId,
url,
method,
optionalComponents: {
body: requestBody,
authorization: headers.Authorization as string
}
})

const ctx = createContext(
{
Expand All @@ -64,7 +65,9 @@ export async function createContextWithSigHeaders(
...headers,
'Content-Digest': contentDigest,
Signature: signature,
'Signature-Input': sigInput
'Signature-Input': sigInput,
'Content-Type': contentType,
'Content-Length': contentLength
}
},
params,
Expand Down
27 changes: 24 additions & 3 deletions packages/auth/src/tests/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,33 @@ export async function generateSigHeaders({
body?: unknown
authorization?: string
}
}): Promise<{ sigInput: string; signature: string; contentDigest?: string }> {
}): Promise<{
sigInput: string
signature: string
contentDigest?: string
contentLength?: string
contentType?: string
}> {
let sigInputComponents = 'sig1=("@method" "@target-uri"'
const { body, authorization } = optionalComponents ?? {}
if (body) sigInputComponents += ' "content-digest"'
if (body)
sigInputComponents += ' "content-digest" "content-length" "content-type"'

if (authorization) sigInputComponents += ' "authorization"'

const sigInput = sigInputComponents + `);created=1618884473;keyid="${keyId}"`
let challenge = `"@method": ${method}\n"@target-uri": ${url}\n`
let contentDigest
let contentLength
let contentType
if (body) {
contentDigest = createContentDigestHeader(JSON.stringify(body), ['sha-512'])
challenge += `"content-digest": ${contentDigest}\n`

contentLength = Buffer.from(JSON.stringify(body), 'utf-8').length
challenge += `"content-length": ${contentLength}\n`
contentType = 'application/json'
challenge += `"content-type": ${contentType}\n`
}

if (authorization) {
Expand All @@ -92,5 +107,11 @@ export async function generateSigHeaders({
const privateJwk = (await importJWK(privateKey)) as crypto.KeyLike
const signature = crypto.sign(null, Buffer.from(challenge), privateJwk)

return { signature: signature.toString('base64'), sigInput, contentDigest }
return {
signature: signature.toString('base64'),
sigInput,
contentDigest,
contentLength,
contentType
}
}

0 comments on commit 55d11d1

Please sign in to comment.