Skip to content

Commit

Permalink
Merge pull request #10 from mark-tesobe/develop
Browse files Browse the repository at this point in the history
FIX: empty authorization header
  • Loading branch information
simonredfern authored Aug 30, 2023
2 parents 9b13500 + 9f39b4e commit 11284ae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obp-typescript",
"version": "1.0.33",
"version": "1.0.36",
"license": "Apache-2.0",
"scripts": {
"build": "tsc --declaration && tsc-alias",
Expand Down
29 changes: 23 additions & 6 deletions src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,20 @@ const getDirectLoginToken = async (
config: APIClientConfig
): Promise<string> => {
if (!config.authentication) {
console.warn("Authentication is not set.");
return "";
}
const loginUri = config.baseUri + "/my/logins/direct";
const username = config.authentication.username;
const password = config.authentication.password;
const consumerKey = config.authentication.consumerKey;
const directLogin = `DirectLogin username=${username},password=${password},consumer_key=${consumerKey}`;
const authorization = directLogin ? { Authorization: directLogin } : {};
const response = JSON.parse(
(
await superagent
.post(loginUri)
.set("Content-Type", "application/json")
.set("Authorization", directLogin)
.set(authorization)
).text
);
return "DirectLogin token=" + response.token;
Expand Down Expand Up @@ -284,6 +284,23 @@ const getOauthHeader = async (
return header;
};

/**
* Get the Authorization header.
*
* @param header - The OAuth header value
* @returns An {object} value
*
*
* @private
*/
const getValidHeader = (header: string): any => {
if (header) {
return { Authorization: header, "Content-Type": "application/json" };
} else {
return { "Content-Type": "application/json" };
}
};

/**
* Send a GET HTTP request and returns a response.
*
Expand All @@ -303,7 +320,7 @@ export const getRequest = async (
return (
await superagent
.get(pathUri)
.set("Authorization", header)
.set(getValidHeader(header))
.catch((error) => error.response)
).body;
};
Expand All @@ -330,7 +347,7 @@ export const postRequest = async (
return (
await superagent
.post(pathUri)
.set("Authorization", header)
.set(getValidHeader(header))
.send(body)
.catch((error) => error.response)
).body;
Expand Down Expand Up @@ -358,7 +375,7 @@ export const putRequest = async (
return (
await superagent
.put(pathUri)
.set("Authorization", header)
.set(getValidHeader(header))
.send(body)
.catch((error) => error.response)
).body;
Expand All @@ -385,7 +402,7 @@ export const deleteRequest = async (
return (
await superagent
.delete(pathUri)
.set("Authorization", header)
.set(getValidHeader(header))
.catch((error) => error.response)
).body;
};
Expand Down
2 changes: 0 additions & 2 deletions src/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ export class OAuth {
* @public
*/
authHeader(pathUri: string, method: string): string {
if (!this.config.accessToken) console.warn("Access token is not set.");

if (this.config.accessToken) {
return this.instance.authHeader(
pathUri,
Expand Down

0 comments on commit 11284ae

Please sign in to comment.