0

I want to force the add of a filed in the req.body, according to the scope of the credentials. I have 2 Apps (App1 and App2), and based on who is using my API, I want to programmatically add a field in the req. So credentials of App1 has scope app1, and app2 in App2's scopes.

Moreover, I have 2 Environments, with different endpoints. Both App has access to both Ends (using different credentials). So I first choose the Env (using dev_env or my_env scope), then I verify which App is accessing (checking app1 or app2 scope).

To do that, I use expression apiEndpoint.scopes.indexOf('app1')>=0. that actually is not working, since the condition is always false. So for debugging purpose, I put the content of apiEndpoint.scopes as additional field in the req.body, to see what there is in that.

And I see that apiEndpoint.scopes has just ["my_env"], not "app1". Why?

So I have

http:
  port: ${PORT:-8080}
  host: ${HOST:-localhost} 
apiEndpoints:
  myEndpoint:
    host: "*"
    scopes: ["my_env"] # I explain just this one here
  devEndpoint:
    host: "*"
    scopes: ["dev_env"] 
serviceEndpoints:
  myEndpoint:
    url:  'https://myserver'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - request-transformer
  - rewrite
  - oauth2
  - proxy
  - rate-limit 
pipelines:
  myEndpoint: 
    apiEndpoints:
      - myEndpoint
    policies:
      - request-transformer:  
        - 
           condition:
             name: allOf
             conditions:      
                 - # check if scope 'app1' is present. expression not working
                   #name: expression
                   #expression: "apiEndpoint.scopes.indexOf('app1')>=0"
          action:
            body:
              add:
                available_scopes: "apiEndpoint.scopes" # debug of available scopes.    

And the content of req.body is

{"available_scopes": ["my_env"]}

'app1' is missing!

==== update 1 If in req.body.available_scopes field I put "consumer", I got this:

{
"type": "application",
"isActive": true,
"id": "....",
"userId": "...",
"name": "...",
"company": "...",
"authorizedScopes": [
      "my_env"
    ]
}

So it talks about "authorizedScopes", where are the others? How could I see them? Thanks

DeLac
  • 1,068
  • 13
  • 43

1 Answers1

0

You have specified the scopes my_env and dev_env for the apiEndpoints myEndpoint and devEndpoint (respectively), and these are the only scopes Express Gateway expects you to care about, so the other scopes associated with the user/app credential are not exposed.

You could add the app1 and app2 scopes to each path in the config file and then act based on whichever scope is set for the credentials of the connecting app:

apiEndpoints:
  myEndpoint:
    host: "*"
    scopes: ["my_env","app1","app2"]

  devEndpoint:
    host: "*"
    scopes: ["dev_env","app1","app2"] 
James McLeod
  • 2,381
  • 1
  • 17
  • 19