Skip to content

Commit

Permalink
adjust to new xssec; test sendMail
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorwolf committed Nov 11, 2024
1 parent b8d872e commit 53c5eec
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 195 deletions.
4 changes: 2 additions & 2 deletions HTML5Module/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion HTML5Module/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html5userapi-ui",
"version": "1.1.20",
"version": "1.1.22",
"description": "",
"engines": {
"node": "^20"
Expand Down
4 changes: 2 additions & 2 deletions mta.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ID: html5userapi
_schema-version: "2.1"
version: 0.4.4
_schema-version: "3.1"
version: 0.5.0
parameters:
enable-parallel-deployments: true

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html5userapi-cf",
"version": "0.4.5",
"version": "0.5.0",
"description": "HTML5 User API for Cloud Foundry",
"repository": "https://github.com/gregorwolf/HTML5UserAPIforCF.git ",
"license": "Apache 2.0",
Expand All @@ -23,11 +23,11 @@
"update:srv": "cd srv && npm update",
"build:cf": "mbt build -p=cf",
"build:cf:docker": "docker run -it --rm -v \"$(pwd):/project\" devxci/mbtci:latest mbt build -p=cf",
"deploy:cf": "cf deploy mta_archives/html5userapi_0.4.1.mtar",
"deploy:cf:approuter": "cf deploy mta_archives/html5userapi_0.4.1.mtar -m html5userapi-ui",
"deploy:cf:srv": "cf deploy mta_archives/html5userapi_0.4.1.mtar -m html5userapi-srv",
"deploy:cf:srv-java": "cf deploy mta_archives/html5userapi_0.4.1.mtar -m html5userapi-srv-java",
"deploy:cf:html5repo": "cf deploy mta_archives/html5userapi_0.4.1.mtar -m html5userapi-deployer",
"deploy:cf": "cf deploy mta_archives/html5userapi_0.5.0.mtar",
"deploy:cf:approuter": "cf deploy mta_archives/html5userapi_0.5.0.mtar -m html5userapi-ui",
"deploy:cf:srv": "cf deploy mta_archives/html5userapi_0.5.0.mtar -m html5userapi-srv",
"deploy:cf:srv-java": "cf deploy mta_archives/html5userapi_0.5.0.mtar -m html5userapi-srv-java",
"deploy:cf:html5repo": "cf deploy mta_archives/html5userapi_0.5.0.mtar -m html5userapi-deployer",
"html5:build": "cd HTML5Module && rm -rf dist && npm run build:ui5 && cd ..",
"html5:push": "cf html5-push -r HTML5Module/dist",
"debug:cf": "cf ssh html5userapi-srv -N -T -L 9229:127.0.0.1:9229",
Expand Down
73 changes: 54 additions & 19 deletions srv/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,27 @@ const express = require("express");
const passport = require("passport");
const xsenv = require("@sap/xsenv");
xsenv.loadEnv();
const JWTStrategy = require("@sap/xssec").JWTStrategy;
const { XssecPassportStrategy, XsuaaService } = require("@sap/xssec");
const services = xsenv.getServices({ xsuaa: { tags: "xsuaa" } });
const authService = new XsuaaService(services.xsuaa);
// console.log(services.xsuaa);
const { jwtDecode } = require("jwt-decode");
passport.use(new JWTStrategy(services.xsuaa));
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
const { sendMail } = require("@sap-cloud-sdk/mail-client");
const { retrieveJwt, getDestination } = require("@sap-cloud-sdk/connectivity");

// config
const host = process.env.HOST || "0.0.0.0";
const port = process.env.PORT || 4004;

function getDestination(req) {
return {
destinationName: process.env.DESTINATION || "SAP_ABAP_BACKEND",
jwt: getJWT(req),
};
}

function getJWT(req) {
const jwt = /^Bearer (.*)$/.exec(req.headers.authorization)[1];
return jwt;
}

(async () => {
// create new app
const app = express();
// fesr.registerFesrEndpoint(app);
// Authentication using JWT
await app.use(passport.initialize());
await app.use(passport.authenticate("JWT", { session: false }));
passport.use(new XssecPassportStrategy(authService));
app.use(passport.initialize());
app.use(passport.authenticate("JWT", { session: false }));

await app.get("/api/userInfo", function (req, res) {
res.header("Content-Type", "application/json");
Expand All @@ -43,7 +35,7 @@ function getJWT(req) {

await app.get("/api/jwt", function (req, res) {
res.header("Content-Type", "application/json");
res.send(JSON.stringify({ JWT: getJWT(req) }));
res.send(JSON.stringify({ JWT: retrieveJwt(req) }));
});
await app.get("/api/jwtdecode", function (req, res) {
if (!req.user) {
Expand All @@ -52,7 +44,7 @@ function getJWT(req) {
} else {
res.statusCode = 200;
res.header("Content-Type", "application/json");
res.end(`${JSON.stringify(jwtDecode(getJWT(req)))}`);
res.end(`${JSON.stringify(jwtDecode(retrieveJwt(req)))}`);
}
});

Expand All @@ -78,7 +70,10 @@ function getJWT(req) {
await app.get("/api/bc/ping", async function (req, res) {
try {
const resultServiceCollection = await executeHttpRequest(
getDestination(req),
{
destinationName: process.env.DESTINATION || "SAP_ABAP_BACKEND",
jwt: retrieveJwt(req),
},
{
method: "get",
url: "/sap/bc/ping",
Expand All @@ -94,6 +89,46 @@ function getJWT(req) {
}
});

await app.get("/api/sendmail", async function (req, res) {
const from = req.query.from || "sender@dummy.com";
const to = req.query.to || "receiver@dummy.com";
const destination = req.query.destination || "inbucket";
console.log("Destination to send mail: " + destination);
try {
const mailConfig = {
from,
to,
subject: "Test from HTML5UserAPIforCF",
text: "Test Body from HTML5UserAPIforCF",
};
console.log("Mail Config: " + JSON.stringify(mailConfig));
const resolvedDestination = await getDestination({
destinationName: destination,
jwt: retrieveJwt(req),
});

const mailClientOptions = {};
// mail. properties allow only lowercase
if (
resolvedDestination.originalProperties[
"mail.clientoptions.ignoretls"
] === "true"
) {
mailClientOptions.ignoreTLS = true;
}
console.log("Mail Client Options: " + JSON.stringify(mailClientOptions));
// use sendmail as you should use it in nodemailer
const result = await sendMail(
{ destinationName: destination, jwt: retrieveJwt(req) },
[mailConfig],
mailClientOptions
);
res.send(JSON.stringify(result));
} catch (error) {
res.send(error);
}
});

// start server
const server = app.listen(port, host, () => {
console.info(`app is listing at ${port}`);
Expand Down
Loading

0 comments on commit 53c5eec

Please sign in to comment.