Skip to Content
1
Jan 07, 2021 at 06:38 PM

Return JWT with Sap CAP SDK Javascript consume external services

16859 Views Last edit Jan 08, 2021 at 08:42 AM 2 rev

Hello,

I created a Java servicie and deploy in one subaccount, this services it is published in sap api mangament that is in other subaccount.

I could consume the Java services thought the api managment for an other third services node js that is ther third subaccount when I was not security the Java.

After security de Java services I am not able to consume.

Part of my test.js is: ( The function getJWT return undefined )

const cds = require('@sap/cds')
const passport = require('passport');
const xsenv = require('@sap/xsenv');
const JWTStrategy = require('@sap/xssec').JWTStrategy;
const express = require('express');
const axios = require('axios');
const consumeDestination = require('consume-destination-scp-cf');
const callDestination = require('sap-cf-destination');
const { retrieveJwt, desc } = require('@sap-cloud-sdk/core')


function getJWT(req) {

    if (typeof (req._) !== 'undefined') {
        console.log("JWT INSIDE=>****************:");
        let s = retrieveJwt(req._.req)
        console.log(s);
        return retrieveJwt(req._.req)
    } else {
        return ""
    }
}

module.exports = ordersService => {

      test.after('READ', 'Test', async (test, req) => {

         var jwt = getJWT(req)
         const sdkDemo = require('./odata-client/api-service-3').Demo;
            const data = await sdkDemo.requestBuilder()
                .getAll()
                .withCustomServicePath('/')
                .execute({ destinationName: 'DEMO_DEST', jwt: jwt });

      });

}

In my xs-security.json

{
  "xsappname": "nodeCap",
  "tenant-mode": "dedicated",
   "description": "Security profile of called application",
    "scopes": [
        {
            "name": "uaa.user",
            "description": "UAA"
        }
    ],
    "role-templates": [
        {
            "name": "Token_Exchange",
            "description": "UAA",
            "scope-references": [
                "uaa.user"
            ]
        }
    ]
}

My xs-app.json

{
    "authenticationMethod":"route",
    "routes": [
        {
            "source": "^/(.*)$",
            "target": "$1",
            "authenticationType": "xsuaa",
            "destination": "DEMO_DEST",           
            "csrfProtection": false
            
        }
    ]
}

My destination is inside of the instance dest-instance and I created with the type: OAuth2ClientCredentials and the details from the xsuaa enviroment from the Java service.

Part of my mta:

 - name:nodeCap
    type: nodejs
    path: gen/srv
    build-parameters:
      ignore: ["node_modules/"]
    parameters:
      memory: 512M
      disk-quota: 512M
    provides:
      - name: srv_api
        properties:
          url: ${default-url}
          forwardAuthToken: true
    requires:
      - name: HDIIN_NODE
      - name: dest-instance
      - name: uaa-instance
resources:
  - name: HDIIN_NODE
    type: com.sap.xs.hdi-container
    properties:
      hdi-container-name: HDIIN_NODE
    parameters:
      config:
        schema: SCHIN_NODE
  - name: dest-instance
    type: org.cloudfoundry.existing-service
  - name: uaa-instance
    type: org.cloudfoundry.existing-service

Did I miss out something? How can I retrieve the jwt?

Best