Skip to content

Commit

Permalink
improve logging and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorwolf committed Jan 19, 2024
1 parent 76e1e70 commit 4e3f63a
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions srv/AuthClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Requests to SAP BTP are authenticated using a SAML assertion obtained from Azure AD (see details below).
*
*/

const cds = require("@sap/cds");
const LOG = cds.log("auth-client");
const qs = require("qs");
const axios = require("axios");
const xsenv = require("@sap/xsenv");
Expand All @@ -22,8 +23,8 @@ class AuthClient {
xsuaa: { tag: "xsuaa" },
});
} catch (error) {
console.error(chalk.red("[azure-ad-auth-client] - " + error.message));
console.error(
LOG.error(chalk.red("[azure-ad-auth-client] - " + error.message));
LOG.error(
"[azure-ad-auth-client] - maintain default-env.json or provide the environment variable VCAP_SERVICES"
);
throw new Error(error.message);
Expand All @@ -36,11 +37,12 @@ class AuthClient {
this.appId = services.azuread.clientID;
this.appSecret = services.azuread.clientSecret;

this.ApplicationIDuri = services.azuread.IdentifierEntityID;
this.ApplicationIDuri = services.xsuaa.url + "/.default";

// V2 AAD path for On-behalf-of flow
this.pathOAuth = `/${this.aadTenantId}/oauth2/v2.0/token`;

// this._xsuaaACSURLSuffix = "aws-live-eu10";
this._xsuaaACSURLSuffix = "aws-live";
this.xsuaaUrl = services.xsuaa.url;
this.btpTokenEndpoint = `/oauth/token/alias/${services.xsuaa.identityzone}.${this._xsuaaACSURLSuffix}`;
Expand Down Expand Up @@ -113,13 +115,13 @@ class AuthClient {
});
return resp;
} catch (err) {
console.error(err);
LOG.error(err.response.data.error_description);
}
})();

// The access token can now be extracted from the result
if (
res.data &&
res?.data &&
res.headers["content-type"].includes("application/json")
) {
const responseBody = res.data;
Expand All @@ -128,13 +130,13 @@ class AuthClient {
accessToken = responseBody["access_token"].toString();
return accessToken;
} catch (err) {
console.error("No JSON response. Access Token request failed");
LOG.error("No JSON response. Access Token request failed");
}
} else {
console.error("HTTP Response was invalid and cannot be deserialized.");
LOG.error("HTTP Response was invalid and cannot be deserialized.");
}
} catch (err) {
console.error(err);
LOG.error(err);

if (
err.error === "invalid_grant" ||
Expand Down Expand Up @@ -183,26 +185,29 @@ class AuthClient {
});
return resp;
} catch (err) {
console.error(err);
LOG.error(err);
}
})();

if (res.fstatus == 200) {
// test for status you want, etc
console.log(res.status);
LOG.log(res.status);
}

if (res.data && res.headers["content-type"].includes("application/json")) {
const responseBody = res.data;
let samlAssertion = " ";
let samlAssertionBase64 = " ";
try {
samlAssertion = responseBody["access_token"].toString();
return samlAssertion;
samlAssertionBase64 = responseBody["access_token"].toString();
var samlAssertionBuffer = Buffer.from(samlAssertionBase64, "base64");
var samlAssertion = samlAssertionBuffer.toString("utf-8");
LOG._debug && LOG.debug("SAML Assertion: " + samlAssertion);
return samlAssertionBase64;
} catch (err) {
console.error("No JSON response. SAML Token request failed");
LOG.error("No JSON response. SAML Token request failed");
}
} else {
console.error("HTTP Response was invalid and cannot be deserialized.");
LOG.error("HTTP Response was invalid and cannot be deserialized.");
}
}

Expand Down

0 comments on commit 4e3f63a

Please sign in to comment.