Skip to content

Commit 53c5eec

Browse files
committed
adjust to new xssec; test sendMail
1 parent b8d872e commit 53c5eec

File tree

9 files changed

+294
-195
lines changed

9 files changed

+294
-195
lines changed

HTML5Module/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HTML5Module/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html5userapi-ui",
3-
"version": "1.1.20",
3+
"version": "1.1.22",
44
"description": "",
55
"engines": {
66
"node": "^20"

mta.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ID: html5userapi
2-
_schema-version: "2.1"
3-
version: 0.4.4
2+
_schema-version: "3.1"
3+
version: 0.5.0
44
parameters:
55
enable-parallel-deployments: true
66

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html5userapi-cf",
3-
"version": "0.4.5",
3+
"version": "0.5.0",
44
"description": "HTML5 User API for Cloud Foundry",
55
"repository": "https://github.com/gregorwolf/HTML5UserAPIforCF.git ",
66
"license": "Apache 2.0",
@@ -23,11 +23,11 @@
2323
"update:srv": "cd srv && npm update",
2424
"build:cf": "mbt build -p=cf",
2525
"build:cf:docker": "docker run -it --rm -v \"$(pwd):/project\" devxci/mbtci:latest mbt build -p=cf",
26-
"deploy:cf": "cf deploy mta_archives/html5userapi_0.4.1.mtar",
27-
"deploy:cf:approuter": "cf deploy mta_archives/html5userapi_0.4.1.mtar -m html5userapi-ui",
28-
"deploy:cf:srv": "cf deploy mta_archives/html5userapi_0.4.1.mtar -m html5userapi-srv",
29-
"deploy:cf:srv-java": "cf deploy mta_archives/html5userapi_0.4.1.mtar -m html5userapi-srv-java",
30-
"deploy:cf:html5repo": "cf deploy mta_archives/html5userapi_0.4.1.mtar -m html5userapi-deployer",
26+
"deploy:cf": "cf deploy mta_archives/html5userapi_0.5.0.mtar",
27+
"deploy:cf:approuter": "cf deploy mta_archives/html5userapi_0.5.0.mtar -m html5userapi-ui",
28+
"deploy:cf:srv": "cf deploy mta_archives/html5userapi_0.5.0.mtar -m html5userapi-srv",
29+
"deploy:cf:srv-java": "cf deploy mta_archives/html5userapi_0.5.0.mtar -m html5userapi-srv-java",
30+
"deploy:cf:html5repo": "cf deploy mta_archives/html5userapi_0.5.0.mtar -m html5userapi-deployer",
3131
"html5:build": "cd HTML5Module && rm -rf dist && npm run build:ui5 && cd ..",
3232
"html5:push": "cf html5-push -r HTML5Module/dist",
3333
"debug:cf": "cf ssh html5userapi-srv -N -T -L 9229:127.0.0.1:9229",

srv/express.js

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,27 @@ const express = require("express");
66
const passport = require("passport");
77
const xsenv = require("@sap/xsenv");
88
xsenv.loadEnv();
9-
const JWTStrategy = require("@sap/xssec").JWTStrategy;
9+
const { XssecPassportStrategy, XsuaaService } = require("@sap/xssec");
1010
const services = xsenv.getServices({ xsuaa: { tags: "xsuaa" } });
11+
const authService = new XsuaaService(services.xsuaa);
12+
// console.log(services.xsuaa);
1113
const { jwtDecode } = require("jwt-decode");
12-
passport.use(new JWTStrategy(services.xsuaa));
1314
const { executeHttpRequest } = require("@sap-cloud-sdk/http-client");
15+
const { sendMail } = require("@sap-cloud-sdk/mail-client");
16+
const { retrieveJwt, getDestination } = require("@sap-cloud-sdk/connectivity");
1417

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

19-
function getDestination(req) {
20-
return {
21-
destinationName: process.env.DESTINATION || "SAP_ABAP_BACKEND",
22-
jwt: getJWT(req),
23-
};
24-
}
25-
26-
function getJWT(req) {
27-
const jwt = /^Bearer (.*)$/.exec(req.headers.authorization)[1];
28-
return jwt;
29-
}
30-
3122
(async () => {
3223
// create new app
3324
const app = express();
3425
// fesr.registerFesrEndpoint(app);
3526
// Authentication using JWT
36-
await app.use(passport.initialize());
37-
await app.use(passport.authenticate("JWT", { session: false }));
27+
passport.use(new XssecPassportStrategy(authService));
28+
app.use(passport.initialize());
29+
app.use(passport.authenticate("JWT", { session: false }));
3830

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

4436
await app.get("/api/jwt", function (req, res) {
4537
res.header("Content-Type", "application/json");
46-
res.send(JSON.stringify({ JWT: getJWT(req) }));
38+
res.send(JSON.stringify({ JWT: retrieveJwt(req) }));
4739
});
4840
await app.get("/api/jwtdecode", function (req, res) {
4941
if (!req.user) {
@@ -52,7 +44,7 @@ function getJWT(req) {
5244
} else {
5345
res.statusCode = 200;
5446
res.header("Content-Type", "application/json");
55-
res.end(`${JSON.stringify(jwtDecode(getJWT(req)))}`);
47+
res.end(`${JSON.stringify(jwtDecode(retrieveJwt(req)))}`);
5648
}
5749
});
5850

@@ -78,7 +70,10 @@ function getJWT(req) {
7870
await app.get("/api/bc/ping", async function (req, res) {
7971
try {
8072
const resultServiceCollection = await executeHttpRequest(
81-
getDestination(req),
73+
{
74+
destinationName: process.env.DESTINATION || "SAP_ABAP_BACKEND",
75+
jwt: retrieveJwt(req),
76+
},
8277
{
8378
method: "get",
8479
url: "/sap/bc/ping",
@@ -94,6 +89,46 @@ function getJWT(req) {
9489
}
9590
});
9691

92+
await app.get("/api/sendmail", async function (req, res) {
93+
const from = req.query.from || "[email protected]";
94+
const to = req.query.to || "[email protected]";
95+
const destination = req.query.destination || "inbucket";
96+
console.log("Destination to send mail: " + destination);
97+
try {
98+
const mailConfig = {
99+
from,
100+
to,
101+
subject: "Test from HTML5UserAPIforCF",
102+
text: "Test Body from HTML5UserAPIforCF",
103+
};
104+
console.log("Mail Config: " + JSON.stringify(mailConfig));
105+
const resolvedDestination = await getDestination({
106+
destinationName: destination,
107+
jwt: retrieveJwt(req),
108+
});
109+
110+
const mailClientOptions = {};
111+
// mail. properties allow only lowercase
112+
if (
113+
resolvedDestination.originalProperties[
114+
"mail.clientoptions.ignoretls"
115+
] === "true"
116+
) {
117+
mailClientOptions.ignoreTLS = true;
118+
}
119+
console.log("Mail Client Options: " + JSON.stringify(mailClientOptions));
120+
// use sendmail as you should use it in nodemailer
121+
const result = await sendMail(
122+
{ destinationName: destination, jwt: retrieveJwt(req) },
123+
[mailConfig],
124+
mailClientOptions
125+
);
126+
res.send(JSON.stringify(result));
127+
} catch (error) {
128+
res.send(error);
129+
}
130+
});
131+
97132
// start server
98133
const server = app.listen(port, host, () => {
99134
console.info(`app is listing at ${port}`);

0 commit comments

Comments
 (0)