LnC
LnC

Reputation: 19

Get user info from ui5 app within Cloud Foundry's Fiori Launchpad

As the title says, I'm trying to retrieve user info from an application which is in the Cloud Foundry's FLP.

I've followed the following blog: https://blogs.sap.com/2019/05/23/how-to-get-the-email-of-the-logged-in-user-in-cloud-foundry/

However my application structure is different and I don't know how to make it work.

First, I can sum up the project creation to these two links:

  1. https://developers.sap.com/tutorials/cp-ui5-webide-new-app.html
  2. https://developers.sap.com/tutorials/cp-portal-cloud-foundry-create-launchpad.html

Here's my application's structure:

enter image description here

With the associated mta.yaml:

ID: mta_ztransport_appointment
_schema-version: '2.1'
parameters:
  deploy_mode: html5-repo
version: 0.0.1
modules:
  - name: mta-ztransport-appointment-approuter
    type: approuter.nodejs
    path: mta-ztransport-appointment-approuter
    parameters:
      disk-quota: 256M
      memory: 128M
    requires:
      - name: mta_ztransport_appointment_html5_repo_runtime
      - name: uaa_mta_ztransport_appointment
      - name: portal_resources_mta_ztransport_appointment
      - name: dest_mta_ztransport_appointment
      - name: conn_mta_ztransport_appointment
  - name: mta_ztransport_appointment_ui_deployer
    type: com.sap.html5.application-content
    path: mta_ztransport_appointment_ui_deployer
    requires:
      - name: mta_ztransport_appointment_html5_repo_host
    build-parameters:
      requires:
        - name: ztransport.appointment
          artifacts:
            - './*'
          target-path: resources/ztransport.appointment
  - name: ztransport.appointment
    type: html5
    path: ztransport.appointment
    build-parameters:
      builder: custom
      commands:
        - npm install
        - npm run build
      supported-platforms: []
      build-result: dist
  - name: flp
    type: com.sap.portal.content
    path: flp
    parameters:
      stack: cflinuxfs3
      memory: 128M
      buildpack: 'https://github.com/cloudfoundry/nodejs-buildpack/releases/download/v1.6.39/nodejs-buildpack-cflinuxfs3-v1.6.39.zip'
    requires:
      - name: portal_resources_mta_ztransport_appointment
      - name: uaa_mta_ztransport_appointment
      - name: mta_ztransport_appointment_html5_repo_host
      - name: mta_ztransport_appointment_ui_deployer

  

resources:
  - name: mta_ztransport_appointment_html5_repo_runtime
    parameters:
      service-plan: app-runtime
      service: html5-apps-repo
    type: org.cloudfoundry.managed-service
  - name: mta_ztransport_appointment_html5_repo_host
    parameters:
      service-plan: app-host
      service: html5-apps-repo
    type: org.cloudfoundry.managed-service
  - name: uaa_mta_ztransport_appointment
    parameters:
      path: ./xs-security.json
      service-plan: application
      service: xsuaa
    type: org.cloudfoundry.managed-service
  - name: dest_mta_ztransport_appointment
    parameters:
      service-plan: lite
      service: destination
    type: org.cloudfoundry.managed-service
  - name: portal_resources_mta_ztransport_appointment
    parameters:
      service-plan: standard
      service: portal
    type: org.cloudfoundry.managed-service
  - name: conn_mta_ztransport_appointment
    parameters:
      service-plan: lite
      service: connectivity
    type: org.cloudfoundry.managed-service

The xs-security.json file:

{
	"xsappname": "mta_ztransport_appointment",
	"tenant-mode": "dedicated",
	"description": "Security profile of called application",
	"scopes": [{
		"name": "$XSAPPNAME.Display",
		"description": "display"
	}, {
		"name": "$XSAPPNAME.Update",
		"description": "update"
	}, {
		"name": "uaa.user",
		"description": "UAA"
	}],
	"role-templates": [{
		"name": "Token_Exchange",
		"description": "UAA",
		"scope-references": [
			"uaa.user"
		]
	}, {
		"name": "Viewer",
		"description": "View Users",
		"scope-references": [
			"$XSAPPNAME.Display"
		]
	}, {
		"name": "Manager",
		"description": "Maintain Users",
		"scope-references": [
			"$XSAPPNAME.Display",
			"$XSAPPNAME.Update"
		]
	}]
}

The xs-app.json in approuter folder:

{
  "welcomeFile": "/cp.portal",
  "authenticationMethod": "route",
  "logout": {
    "logoutEndpoint": "/do/logout"
  },
  "routes": [
    {
      "source": "^/Dirickx_DE1/(.*)$",
      "target": "$1",
      "authenticationType": "none",
      "destination": "Dirickx_DE1",
      "csrfProtection": false
    },
    {
      "source": "^(.*)$",
      "target": "$1",
      "service": "html5-apps-repo-rt",
      "authenticationType": "xsuaa"
    }
  ]
}

Quite the same approuter-start.js than the blog put in the approuter folder.

var approuter = require('@sap/approuter');
var ar = approuter();
ar.beforeRequestHandler.use('/getuserinfo', function (req, res, next) {
	if (!req.user) {
		res.statusCode = 403;
		res.end("Missing JWT Token");
	} else {
		res.statusCode = 200;
		res.end("My name is ${JSON.stringify(req.user.name, null, 2)}");
	}
});
ar.start();

And package.json in approuter folder as follows:

{
	"name": "html5-apps-approuter",
	"description": "Node.js based application router service for html5-apps",
	"engines": {
		"node": "^8.0.0 || ^10.0.0"
	},
	"dependencies": {
		"@sap/approuter": "6.8.0"
	},
	"scripts": {
		"start": "node ./approuter-start.js"
	}
}

My xs-app.json file is different from the one from the blog and I think the error comes from it but if I stick to the blog, the deployment fails. And with the one I'm using, the deployment works but the request isn't made.

Any idea? Thanks

Upvotes: 1

Views: 4688

Answers (1)

orogers
orogers

Reputation: 577

The blog you mentioned creates a specific route in the approuter /getuserinfo so that when you deploy the approuter and go to https://<app_router_domain_from_SCP>.hana.ondemand.com/getuserinfo it decodes the JWT token you've got from logging in and returns the details.

You need to make the request yourself to get the data.

If you want to use the data in a UI5 app, I would just make the /getuserinfo handler return the data in JSON format and use a JSONModel on that URL. You can then bind to the new model as you would any other model.

Hope this helps!

Upvotes: 1

Related Questions