Skip to content

Commit 753ee23

Browse files
added support for custom headers for api key auths
1 parent 8840d89 commit 753ee23

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ lib/
5656
.DS_Store
5757
deploy-script.sh
5858
coverage/
59+
.nyc_output/

src/serializers/paw/Serializer.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -442,15 +442,15 @@ methods.convertAuthIntoDynamicValue = (auth) => {
442442
* @param {Environment} environment: the environment in which this auth value is applicable.
443443
* @param {Auth} auth: the auth to add to the domain
444444
* @param {string} key: the name of the auth
445-
* @returns {EnvironmentVariable} the newly created environment variable.
445+
* @returns {{ variable: EnvironmentVariable, auth: Auth }} the newly created environment variable.
446446
*/
447447
methods.addAuthToDomain = (domain, environment, auth, key) => {
448448
const variable = domain.createEnvironmentVariable(key)
449449
const dv = methods.convertAuthIntoDynamicValue(auth)
450450
const ds = methods.wrapDV(dv)
451451
variable.setValue(ds, environment)
452452

453-
return variable
453+
return { variable, auth }
454454
}
455455

456456
/**
@@ -1041,26 +1041,33 @@ methods.convertAuthFromReference = (store, reference) => {
10411041
* converts a reference or an auth into a DynamicString Entry.
10421042
* @param {Store} store: the store used to resolve references
10431043
* @param {Auth|Reference} authOrReference: the record to convert into a DynamicString
1044-
* @returns {DynamicString} the corresponding DynamicString
1044+
* @returns {{ variable: DynamicString, auth: Auth }} the corresponding DynamicString
10451045
*/
10461046
methods.convertReferenceOrAuthToDsEntry = (store, authOrReference) => {
10471047
if (authOrReference instanceof Reference) {
10481048
return methods.convertAuthFromReference(store, authOrReference)
10491049
}
10501050

10511051
const dv = methods.convertAuthIntoDynamicValue(authOrReference)
1052-
return methods.wrapDV(dv)
1052+
return { variable: methods.wrapDV(dv), auth: authOrReference }
10531053
}
10541054

10551055
// TODO create Variable DS that has enum with all auth possible
10561056
/**
10571057
* sets the Auth DynamicString as am Authorization Header.
10581058
* @param {PawRequest} pawRequest: the paw request to update
1059-
* @param {DynamicString} auth: the DynamicString representing an auth
1059+
* @param {Objecti} authData: the object containing the auth representation as a DynamicString and
1060+
* the original auth Record.
1061+
* @param {DynamicString} variable: the DynamicString representing an auth
1062+
* @param {Auth} auth: the original auth
10601063
* @returns {PawRequest} the update paw request
10611064
*/
1062-
methods.addAuthToRequest = (pawRequest, auth) => {
1063-
pawRequest.setHeader('Authorization', auth)
1065+
methods.addAuthToRequest = (pawRequest, { variable, auth }) => {
1066+
let authName = 'Authorization'
1067+
if (auth instanceof Auth.ApiKey) {
1068+
authName = auth.get('name')
1069+
}
1070+
pawRequest.setHeader(authName, variable)
10641071
return pawRequest
10651072
}
10661073

src/serializers/paw/__tests__/Serializer.spec.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ describe('serializers/paw/Serializer.js', () => {
746746
spyOn(__internals__, 'convertAuthIntoDynamicValue').andReturn(123)
747747
spyOn(__internals__, 'wrapDV').andReturn('123')
748748

749-
const expected = variable
749+
const expected = { variable, auth }
750750
const actual = __internals__.addAuthToDomain(domain, environment, auth, key)
751751

752752
expect(domain.createEnvironmentVariable).toHaveBeenCalledWith(key)
@@ -2040,7 +2040,7 @@ describe('serializers/paw/Serializer.js', () => {
20402040
const store = new Store()
20412041
const auth = new Auth.Basic()
20422042

2043-
const expected = '123'
2043+
const expected = { variable: '123', auth }
20442044
const actual = __internals__.convertReferenceOrAuthToDsEntry(store, auth)
20452045

20462046
expect(actual).toEqual(expected)
@@ -2051,12 +2051,27 @@ describe('serializers/paw/Serializer.js', () => {
20512051
it('should work', () => {
20522052
const pawReq = { setHeader: () => {} }
20532053
spyOn(pawReq, 'setHeader').andReturn(123)
2054-
const auth = 'some dynamic string'
2054+
const authData = { variable: 'some dynamic string', auth: new Auth.Basic() }
2055+
2056+
const expected = pawReq
2057+
const actual = __internals__.addAuthToRequest(pawReq, authData)
2058+
2059+
expect(pawReq.setHeader).toHaveBeenCalledWith('Authorization', authData.variable)
2060+
expect(actual).toEqual(expected)
2061+
})
2062+
2063+
it('should work with ApiKeys custom headers', () => {
2064+
const pawReq = { setHeader: () => {} }
2065+
spyOn(pawReq, 'setHeader').andReturn(123)
2066+
const authData = {
2067+
variable: 'some dynamic string',
2068+
auth: new Auth.ApiKey({ name: 'X-Auth-Token' })
2069+
}
20552070

20562071
const expected = pawReq
2057-
const actual = __internals__.addAuthToRequest(pawReq, auth)
2072+
const actual = __internals__.addAuthToRequest(pawReq, authData)
20582073

2059-
expect(pawReq.setHeader).toHaveBeenCalledWith('Authorization', auth)
2074+
expect(pawReq.setHeader).toHaveBeenCalledWith('X-Auth-Token', authData.variable)
20602075
expect(actual).toEqual(expected)
20612076
})
20622077
})

0 commit comments

Comments
 (0)