1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-17 00:50:05 +00:00

fix(file-upload): maintain separate CSRF token for file uploads

This commit is contained in:
Krishna Acondy
2020-09-22 09:26:16 +01:00
parent f764f1f22f
commit 5343ca00d8

View File

@@ -36,6 +36,7 @@ export class SASViyaApiClient {
} }
private csrfToken: CsrfToken | null = null private csrfToken: CsrfToken | null = null
private fileUploadCsrfToken: CsrfToken | null = null
private sessionManager = new SessionManager( private sessionManager = new SessionManager(
this.serverUrl, this.serverUrl,
this.contextName, this.contextName,
@@ -1335,7 +1336,9 @@ export class SASViyaApiClient {
const uploadResponse = await this.request<any>( const uploadResponse = await this.request<any>(
`${this.serverUrl}/files/files#rawUpload`, `${this.serverUrl}/files/files#rawUpload`,
createFileRequest createFileRequest,
'json',
'fileUpload'
) )
uploadedFiles.push({ tableName, file: uploadResponse.result }) uploadedFiles.push({ tableName, file: uploadResponse.result })
@@ -1490,22 +1493,36 @@ export class SASViyaApiClient {
this.setCsrfToken(csrfToken) this.setCsrfToken(csrfToken)
} }
setFileUploadCsrfToken = (csrfToken: CsrfToken) => {
this.fileUploadCsrfToken = csrfToken
}
private async request<T>( private async request<T>(
url: string, url: string,
options: RequestInit, options: RequestInit,
contentType: 'text' | 'json' = 'json' contentType: 'text' | 'json' = 'json',
type: 'fileUpload' | 'other' = 'other'
) { ) {
if (this.csrfToken) { const callback =
options.headers = { type === 'fileUpload'
...options.headers, ? this.setFileUploadCsrfToken
[this.csrfToken.headerName]: this.csrfToken.value : this.setCsrfTokenLocal
if (type === 'other') {
if (this.csrfToken) {
options.headers = {
...options.headers,
[this.csrfToken.headerName]: this.csrfToken.value
}
}
} else {
if (this.fileUploadCsrfToken) {
options.headers = {
...options.headers,
[this.fileUploadCsrfToken.headerName]: this.fileUploadCsrfToken.value
}
} }
} }
return await makeRequest<T>( return await makeRequest<T>(url, options, callback, contentType)
url,
options,
this.setCsrfTokenLocal,
contentType
)
} }
} }