Skip to content

Comments

[New feature] Support claims to path transformation#968

Merged
thiagoloureiro merged 2 commits intoThreeMammals:developfrom
riftgg:claim-to-path
Aug 12, 2019
Merged

[New feature] Support claims to path transformation#968
thiagoloureiro merged 2 commits intoThreeMammals:developfrom
riftgg:claim-to-path

Conversation

@vicmaeg
Copy link
Contributor

@vicmaeg vicmaeg commented Jul 23, 2019

Add the option to transform a claim to the downstream path. Similar to the Claims To Query String forwarding. We are starting to use Ocelot in our backend and we need this feature, to transform the user id from the claims to the downstream backend. This way we can do things like this:

  • Get /api/users/me (defined only in the API gateway)
  • Get /api/users/{userId} (defined in the users microservice backend)

The request contains the user_id in the claims, so it can then change the downstream path with the user_id to the users microservice backend /api/users/{userId}

Proposed Changes

  • Add a new ClaimsToDownstreamPathMiddleware, that using the new ChangeDownstreamPathTemplate substitutes the placeholder that matches the ClaimToThing existing key.
  • Add unit tests and acceptance tests

An example usage ocelot.json:

{
      "UpstreamPathTemplate": "/api/users/me",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 63048
        }
      ],
      "DownstreamPathTemplate": "/api/users/{userId}",
      "DownstreamScheme": "http",
      "AuthenticationOptions": {
        "AuthenticationProviderKey": "AuthJWTKey",
        "AllowedScopes": []
      },
      "ChangeDownstreamPathTemplate": {
        "userId": "Claims[user_id] > value"
      }
}

if the DownstreamPathTemplate contains a placeholder that matches ChangeDownstreamPathTemplate existing key (in the example userId) it substitutes the value so will end up calling the downstream host with /api/users/123.

if ChangeDownstreamPathTemplate contains a claim to things, but there is no match in the DownstreamPathTemplate it will respond with an error.

The middleware basically adds or changes a PlaceholderNameAndValue from the DownstreamContext.TemplatePlaceholderNameAndValues, so when the DownstreamUrlCreatorMiddleware executes it will replace the placeholder with the values from the claims.

@vicmaeg
Copy link
Contributor Author

vicmaeg commented Jul 26, 2019

any update on this? it has sense to merge, do you thing it is useful?

@djrhodes
Copy link

Hi, thanks for adding this functionality, we need exactly this in our project. Do you have any idea when this will be merged in and released?

@thiagoloureiro thiagoloureiro added the in progress Someone is working on the issue. Could be someone on the team or off. label Aug 12, 2019
@thiagoloureiro thiagoloureiro merged commit 8117366 into ThreeMammals:develop Aug 12, 2019
@thiagoloureiro
Copy link
Contributor

@vmartos just merged, a new package will be generated as pre-release, @djrhodes you can try from your project too using latest pre-release and then we will release the latest stable soon.

@raman-m raman-m added feature A new feature Claims Transform Ocelot feature: Claims Transformation and removed in progress Someone is working on the issue. Could be someone on the team or off. labels Dec 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Claims Transform Ocelot feature: Claims Transformation feature A new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants