这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@rakeshkky
Copy link
Member

@rakeshkky rakeshkky commented Dec 20, 2019

Description

Some providers don't let users add custom JWT claims. In such a case, the server will provide a JWT configuration option to specify a mapping of hasura session variables to values in existing claims via JSONPaths. Now JWT config supports an extra optional field claims_map which is a JSON object which maps from session variables to jsonpaths.

Example:-

Consider the following JWT claim:

  {
    "sub": "1234567890",
    "name": "John Doe",
    "admin": true,
    "iat": 1516239022,
    "user": {
      "id": "ujdh739kd",
       "appRoles": ["user", "editor"]
    }
  }

The mapping for x-hasura-allowed-roles, x-hasura-default-role and x-hasura-user-id session variables can be specified in claims_map configuration as follows.

--jwt-secret or HASURA_GRAPHQL_JWT_SECRET

  {
    "type":"RS512",
    "key": "<The public Key>",
    "claims_map": {
      "x-hasura-allowed-roles": {"path":"$.user.appRoles"},
      "x-hasura-default-role": {"path":"$.user.appRoles[0]","default":"user"},
      "x-hasura-user-id": {"path":"$.user.id"}
    }
  }

The syntax to provide literal values in the JWT claims_map:

  {
    "type":"RS512",
    "key": "<The public Key>",
    "claims_map": {
      "x-hasura-allowed-roles": ["user","editor"],
      "x-hasura-default-role": "user",
      "x-hasura-user-id": "1"
    }
  }

Affected components

  • Server
  • Console
  • CLI
  • Docs
  • Community Content
  • Build System
  • Tests
  • Other (list it)

Related Issues

closes #3485

Solution and Design

  • Since the claims_map accepts JSON paths, the parser in Data.JSONPath.Parser module is improved to accept keys having special characters.
  • Property tests for above said parser.
  • The JWTConfig data type is improved to remove few Maybe fields.
  • The core logic change is present in Hasura.Server.Auth.JWT module, where resolving the hasura claims considers the claims_map configuration.

Server checklist

Catalog upgrade

Does this PR change Hasura Catalog version?

  • No
  • Yes

Metadata

Does this PR add a new Metadata feature?

  • No
  • Yes

GraphQL

  • No new GraphQL schema is generated
  • New GraphQL schema is being generated:

Breaking changes

  • No Breaking changes
  • There are breaking changes

Steps to test and verify

Limitations, known bugs & workarounds

@rakeshkky rakeshkky self-assigned this Dec 20, 2019
@netlify
Copy link

netlify bot commented Dec 20, 2019

Deploy preview for hasura-docs ready!

Built with commit 37c841d

https://deploy-preview-3575--hasura-docs.netlify.com

@hasura-bot
Copy link
Contributor

Review app for commit 2ddbdb8 deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-2ddbdb8b

@hasura-bot
Copy link
Contributor

Review app for commit c371218 deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-c3712180

@hasura-bot
Copy link
Contributor

Review app for commit fd94924 deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-fd949247

@hasura-bot
Copy link
Contributor

Review app for commit bb4fe73 deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-bb4fe73b

@hasura-bot
Copy link
Contributor

Review app for commit 36a3373 deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-36a3373b

@hasura-bot
Copy link
Contributor

Review app for commit 37c841d deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-37c841d2

@dsandip
Copy link
Member

dsandip commented Jan 16, 2020

@Oxymoron290 Could you please help us validate this? The docker image to be used for testing this is hasura/graphql-engine:pull3575-37c841d2. we can fast-track the release as soon as we have some validation from the community.

@tracetechnical
Copy link

@dsandip Can we help in any way to validate this? As we also very much would like this functionality.

@dsandip
Copy link
Member

dsandip commented Feb 17, 2020

@pheonixtechnical Sorry about the late response. Yes, of course, we could use your help. You can use the docker image from #3575 (comment) and test it with your Azure AD account. Please confirm that you are able to get the Hasura ACL rules to work off of your JWT claims by posting a comment here? Thank you!

@matjazrak
Copy link

Hello,
I will test the solution with Azure B2C. I will get back with the result ASAP.
Thanx.

Resolve Conflicts:
	docs/graphql/manual/auth/authentication/jwt.rst
	server/graphql-engine.cabal
	server/src-lib/Hasura/Server/Auth.hs
	server/src-lib/Hasura/Server/Auth/JWT.hs
	server/src-test/Main.hs
@netlify
Copy link

netlify bot commented Apr 9, 2020

Deploy preview for hasura-docs ready!

Built with commit b2eb72e

https://deploy-preview-3575--hasura-docs.netlify.app

@tirumaraiselvan
Copy link
Contributor

@rakeshkky Updated spec: #3485 (comment)

Resolve Conflicts:
	.circleci/test-server.sh
	server/graphql-engine.cabal
	server/src-lib/Data/Parser/JSONPath.hs
	server/src-lib/Hasura/RQL/Types/Error.hs
	server/src-lib/Hasura/Server/Auth/JWT.hs
	server/src-lib/Hasura/Server/Config.hs
	server/src-lib/Hasura/Server/Utils.hs
	server/src-test/Main.hs
	server/tests-py/queries/v1/select/boolexp/postgis/query_illegal_cast_is_not_allowed.yaml
	server/tests-py/queries/v1/update/permissions/user_cannot_update_id_col_article.yaml
@rakeshkky rakeshkky changed the title support for jwt azure active directory (close #3485) support customizing JWT claims (close #3485) Apr 20, 2020
@tirumaraiselvan tirumaraiselvan added this to the v1.3 milestone Apr 21, 2020
@coco98 coco98 removed this from the v1.3 milestone Apr 24, 2020
@hasura-bot
Copy link
Contributor

Review app for commit 94282ed deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-94282ed5

@hasura-bot
Copy link
Contributor

Review app for commit 703e2b9 deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-703e2b97

CHANGELOG.md Outdated
- server: treat the absence of `backend_only` configuration and `backend_only: false` equally (closing #5059) (#4111)
- server: support customizing JWT claims (close #3485)

Some providers don't let users add custom JWT claims. In such a case, the server will provide a JWT configuration option to specify a mapping of hasura session variables to values in existing claims via JSONPath/literal values. The JWT config now supports an extra optional field `claims_map` which is an JSON object which maps from session variables to JSON paths.
Copy link
Contributor

@tirumaraiselvan tirumaraiselvan Aug 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Some providers don't let users add custom JWT claims. In such a case, the server will provide a JWT configuration option to specify a mapping of hasura session variables to values in existing claims via JSONPath/literal values. The JWT config now supports an extra optional field `claims_map` which is an JSON object which maps from session variables to JSON paths.
Some auth providers do not let users add custom claims in JWT. In such cases, the server can take a JWT configuration option called `claims_map` to specify a mapping of Hasura session variables to values in existing claims via JSONPath or literal values.

CHANGELOG.md Outdated
}
```

The corresponding JWT config should be:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The corresponding JWT config should be:
The corresponding JWT config can be:

@hasura-bot
Copy link
Contributor

Review app for commit 3e987ec deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-3e987ecb

CHANGELOG.md Outdated

## Next release

### Server - Support for custom JWT Claims
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support for mapping session variables to default JWT claims

@hasura-bot
Copy link
Contributor

Review app for commit 973750f deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-973750f0

Copy link
Contributor

@tirumaraiselvan tirumaraiselvan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approving changelog (barring small comment) and docs content (needs approval from @marionschleifer )

@hasura-bot
Copy link
Contributor

Review app for commit baac239 deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-baac2395

@hasura-bot
Copy link
Contributor

Review app for commit b2eb72e deployed to Heroku: https://hge-ci-pull-3575.herokuapp.com
Docker image for server: hasura/graphql-engine:pull3575-b2eb72ec

Copy link
Collaborator

@jberryman jberryman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the comments!

@codingkarthik codingkarthik merged commit 4ce6002 into hasura:master Aug 31, 2020
stevefan1999-personal pushed a commit to stevefan1999-personal/graphql-engine that referenced this pull request Sep 12, 2020
* improve jsonpath parser to accept special characters and property tests for the same

* make the JWTClaimsMapValueG parametrizable

* add documentation in the JWT file

* modify processAuthZHeader

Co-authored-by: Karthikeyan Chinnakonda <karthikeyan@hasura.io>
Co-authored-by: Marion Schleifer <marion@hasura.io>
@tirumaraiselvan tirumaraiselvan added this to the v1.3.3 milestone Nov 3, 2020
codingkarthik added a commit to codingkarthik/graphql-engine that referenced this pull request Nov 6, 2020
* improve jsonpath parser to accept special characters and property tests for the same

* make the JWTClaimsMapValueG parametrizable

* add documentation in the JWT file

* modify processAuthZHeader

Co-authored-by: Karthikeyan Chinnakonda <karthikeyan@hasura.io>
Co-authored-by: Marion Schleifer <marion@hasura.io>
@tirumaraiselvan tirumaraiselvan changed the title support customizing JWT claims (close #3485) support mapping JWT claims (close #3485) Nov 10, 2020
rikinsk pushed a commit that referenced this pull request Nov 18, 2020
* improve jsonpath parser to accept special characters and property tests for the same

* make the JWTClaimsMapValueG parametrizable

* add documentation in the JWT file

* modify processAuthZHeader

Co-authored-by: Karthikeyan Chinnakonda <karthikeyan@hasura.io>
Co-authored-by: Marion Schleifer <marion@hasura.io>
hasura-bot pushed a commit that referenced this pull request Jan 21, 2021
fixes #6449

A while back we added [support for customizing JWT claims](#3575) and this enabled to map a session variable to any value within the unregistered claims, but as reported in #6449 , users aren't able to map the `x-hasura-user-id` session variable to the `sub` standard JWT claim.

This PR fixes the above issue by allowing mapping session variables to standard JWT claims as well.

GitOrigin-RevId: d3e63d7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c/server Related to server

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC] support JWT claims mapping (e.g. to work with Azure Active Directory)