Skip to content

Commit decbdfe

Browse files
authored
Added IMS token support to workfront-api (#1485)
1 parent c9685dd commit decbdfe

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/Api.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
import {INTERNAL_PREFIX} from '@workfront/api-constants'
2323

24+
export interface IImsCredentials {
25+
imsToken: string
26+
imsOrgId: string
27+
}
28+
2429
export interface IHttpOptions {
2530
path?: string
2631
method?: string
@@ -30,6 +35,8 @@ export interface IHttpOptions {
3035
sessionID?: string
3136
'X-XSRF-TOKEN'?: string
3237
apiKey?: string
38+
Authorization?: string
39+
'x-gw-ims-org-id'?: string
3340
}
3441
}
3542
export interface IApiConfig {
@@ -575,6 +582,20 @@ export class Api {
575582
delete this._httpOptions.headers['X-XSRF-TOKEN']
576583
}
577584
}
585+
/**
586+
* Sets the 'x-gw-ims-org-id' and 'Authorization' header
587+
* @memberof Api
588+
* @param {IImsCredentials} imsCredentials ims token and ims org id to set
589+
*/
590+
setImsCredentials(imsCredentials?: IImsCredentials) {
591+
if (imsCredentials) {
592+
this._httpOptions.headers.Authorization = 'Bearer ' + imsCredentials.imsToken
593+
this._httpOptions.headers['x-gw-ims-org-id'] = imsCredentials.imsOrgId
594+
} else {
595+
delete this._httpOptions.headers.Authorization
596+
delete this._httpOptions.headers['x-gw-ims-org-id']
597+
}
598+
}
578599

579600
uploadFileContent(fileContent, filename: string) {
580601
const data = new FormData()
@@ -591,6 +612,12 @@ export class Api {
591612
headers.append('X-XSRF-TOKEN', this._httpOptions.headers['X-XSRF-TOKEN'])
592613
} else if (this._httpOptions.headers.apiKey) {
593614
headers.append('apiKey', this._httpOptions.headers.apiKey)
615+
} else if (
616+
this._httpOptions.headers.Authorization &&
617+
this._httpOptions.headers['x-gw-ims-org-id']
618+
) {
619+
headers.append('Authorization', this._httpOptions.headers.Authorization)
620+
headers.append('x-gw-ims-org-id', this._httpOptions.headers['x-gw-ims-org-id'])
594621
}
595622
return headers
596623
}

test/Api.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('Create new instance for API', function () {
3838
should(api.uploadFileContent).be.a.Function().and.has.lengthOf(2)
3939
should(api.setApiKey).be.a.Function().and.has.lengthOf(1)
4040
should(api.clearApiKey).be.a.Function().and.has.lengthOf(0)
41+
should(api.setImsCredentials).be.a.Function().and.has.lengthOf(1)
4142
})
4243

4344
it('should set correct API path based on passed configuration (version is passed)', function () {
@@ -54,4 +55,16 @@ describe('Create new instance for API', function () {
5455
const api = new Api({url: 'http://localhost', version: 'asp'})
5556
should(api._httpOptions.path).equal('/attask/api-asp')
5657
})
58+
59+
it('should store IMS headers when credentials are set', function () {
60+
const api = new Api({url: 'http://localhost'})
61+
api.setImsCredentials({imsToken: 'abc123', imsOrgId: 'org-1'})
62+
63+
should(api._httpOptions.headers.Authorization).equal('Bearer abc123')
64+
should(api._httpOptions.headers['x-gw-ims-org-id']).equal('org-1')
65+
66+
const headers = (api as any).getHeaders()
67+
should(headers.get('Authorization')).equal('Bearer abc123')
68+
should(headers.get('x-gw-ims-org-id')).equal('org-1')
69+
})
5770
})

0 commit comments

Comments
 (0)