Skip to content

Commit

Permalink
feat: add CRN to request signature (#324)
Browse files Browse the repository at this point in the history
* feat: add crn to signature

* fix: make crn optional

* test: added assertion for x-contentful-signed-headers
  • Loading branch information
andipaetzold authored Aug 29, 2023
1 parent e85a567 commit 01e9d09
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/requests/sign-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,28 @@ describe('create-signature', () => {

assert.ok(Object.values(result).every((h) => typeof h !== 'undefined'))
})

it('CRN header is optional', () => {
const result = signRequest(VALID_SECRET, VALID_REQUEST, undefined, {
appId: 'appId',
spaceId: 'spaceId',
envId: 'envId',
})

assert.ok(!result['x-contentful-signed-headers'].includes('x-contentful-crn'))
assert.equal(result['x-contentful-crn'], undefined)
})

it('includes CRN header', () => {
const result = signRequest(VALID_SECRET, VALID_REQUEST, undefined, {
crn: 'this-is-a-crn',
appId: 'appId',
spaceId: 'spaceId',
envId: 'envId',
})

assert.ok(result['x-contentful-signed-headers'].includes('x-contentful-crn'))
assert.equal(result['x-contentful-crn'], 'this-is-a-crn')
})
})
})
3 changes: 3 additions & 0 deletions src/requests/typings/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum ContentfulHeader {
}

export enum ContentfulContextHeader {
CRN = 'x-contentful-crn',
SpaceId = 'x-contentful-space-id',
EnvironmentId = 'x-contentful-environment-id',
UserId = 'x-contentful-user-id',
Expand All @@ -29,11 +30,13 @@ export type SubjectHeadersUser = { userId: string }
export type UserContextSignedHeaders = { [ContentfulContextHeader.UserId]: string }

export type Context<SubjectContext> = {
crn?: string
spaceId: string
envId: string
} & SubjectContext

type SignedHeadersWithoutSubject = {
[ContentfulContextHeader.CRN]?: string
[ContentfulContextHeader.SpaceId]: string
[ContentfulContextHeader.EnvironmentId]: string
}
Expand Down
1 change: 1 addition & 0 deletions src/requests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export const pickHeaders = (headers?: Record<string, string>, keys?: string[]) =
}

const contextHeadersMap: Record<string, ContentfulContextHeader> = {
crn: ContentfulContextHeader.CRN,
spaceId: ContentfulContextHeader.SpaceId,
envId: ContentfulContextHeader.EnvironmentId,
appId: ContentfulContextHeader.AppId,
Expand Down

0 comments on commit 01e9d09

Please sign in to comment.