Dynamic User Per Request
  • 16 Nov 2022
  • Dark
    Light
  • PDF

Dynamic User Per Request

  • Dark
    Light
  • PDF

Article summary

Scenario

This system has authentication based on the person making the request (it does not use a system user).

The user credentials must be passed into the adapter method (in this case, there are two variables to pass in – dynuser and dynpass). However, we would rather pass them in a single object because that allows for easier expansion in the future.

Set the callProperties object in the ReqObj. Be sure to set username to the user variable (dynuser) and password to the password variable (dynpass) in the authentication section.

The adapter library will use these credentials instead of the credentials defined in the IAP Service Instance Configuration for the adapter. However, you should still input values into the IAP Service Instance Configuration for the adapter, or the adapter may not start up.

This requires a code change because you need additional data passed down to the adapter, and that data needs to be added to the request object passed down to the adapter library. It does not require any additional coding in the adapter library; the adapter library is prepared to handle this type of action.

Changes to the adapter.js File:

  • You will need to make changes to allow for the dynamic username and password to be passed into the adapter methods.
    • Adding the credential object which will contain the two fields.
    • Add the username and password information into the authentication section of the callProperties so it will override the IAP Service Instance Configuration for the adapter.

adapter.js Example

getMyData(query, credentials, callback) {

....

const reqObj = {
  payload: { garbage: 'need since post' },
  uriPathVars: [groupId, deviceId],
  uriQuery: { name: 'anyname' }, 
  uriOptions: { page: 2 },
  addlHeaders: { audit: 'turnOn' }, 
  authData: {},
  callProperties: {
    authentication: {
      username: credentials.dynuser,
      password: credentials.dynpass
    }
  },
  filter: '[*name=doggie]'
};

Changes to the pronghorn.json File:

  • You will need to make changes to allow for the credential object to be passed into the adapter methods.
    • Adding the input into every method defined in the pronghorn.json file.

pronghorn.json Method Example

    {
      "name": "getMyData",
      "summary": "get my data from blah",
      "description": "get my data from blah",
      "input": [
        {
          "name": "query",
          "type": "object",
          "info": "object containing query fields",
          "required": false,
            "schema": {
                "title": "query",
                "type": "object"
            }
        },
        {
          "name": "credentials",
          "type": "object",
          "info": "Should contain 2 fields - dynuser and dynpass",
          "required": true,
            "schema": {
                "title": "credentials",
                "type": "object"
            }
        }
      ],
      "output": {
        "name": "result",
        "type": "object",
        "description": "A JSON Object containing status, code and the result"
      },
      "route": {
        "verb": "POST",
        "path": "/getMyData"
      },
      "roles": [
        "admin"
      ],
      "task": true
    },

Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.