@@ -6,35 +6,27 @@ const express = require("express");
66const passport = require ( "passport" ) ;
77const xsenv = require ( "@sap/xsenv" ) ;
88xsenv . loadEnv ( ) ;
9- const JWTStrategy = require ( "@sap/xssec" ) . JWTStrategy ;
9+ const { XssecPassportStrategy , XsuaaService } = require ( "@sap/xssec" ) ;
1010const services = xsenv . getServices ( { xsuaa : { tags : "xsuaa" } } ) ;
11+ const authService = new XsuaaService ( services . xsuaa ) ;
12+ // console.log(services.xsuaa);
1113const { jwtDecode } = require ( "jwt-decode" ) ;
12- passport . use ( new JWTStrategy ( services . xsuaa ) ) ;
1314const { 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
1619const host = process . env . HOST || "0.0.0.0" ;
1720const 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 = / ^ B e a r e r ( .* ) $ / . 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