Skip to content

Commit

Permalink
Separate function to get Auth headers
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed Jan 8, 2024
1 parent 9f58a37 commit ed5eee6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions openeo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2462,6 +2462,13 @@ declare module OpenEO {
* @throws {Error}
*/
download(url: string, authorize: boolean): Promise<Readable | Blob>;
/**
* Get the authorization header for requests.
*
* @protected
* @returns {object.<string, string>}
*/
protected _getAuthHeaders() : object<string, string>;
/**
* Sends a HTTP request.
*
Expand Down
18 changes: 16 additions & 2 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,20 @@ class Connection {
return result.data;
}

/**
* Get the authorization header for requests.
*
* @protected
* @returns {object.<string, string>}
*/
_getAuthHeaders() {
const headers = {};
if (this.isAuthenticated()) {
headers.Authorization = 'Bearer ' + this.authProvider.getToken();
}
return headers;
}

/**
* Sends a HTTP request.
*
Expand All @@ -1237,11 +1251,11 @@ class Connection {
*/
async _send(options, abortController = null) {
options.baseURL = this.baseUrl;
if (this.isAuthenticated() && (typeof options.authorization === 'undefined' || options.authorization === true)) {
if (typeof options.authorization === 'undefined' || options.authorization === true) {
if (!options.headers) {
options.headers = {};
}
options.headers.Authorization = 'Bearer ' + this.authProvider.getToken();
Object.assign(options.headers, this._getAuthHeaders());
}
if (!options.responseType) {
options.responseType = 'json';
Expand Down

0 comments on commit ed5eee6

Please sign in to comment.