2023-04-19 13:28:49 +03:00
|
|
|
from datetime import datetime, timedelta
|
2020-08-31 19:40:01 +03:00
|
|
|
import math
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
import jwt
|
|
|
|
import pytest
|
2022-01-17 10:39:59 +03:00
|
|
|
from ruamel.yaml import YAML
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
from validate import check_query
|
2020-08-31 19:40:01 +03:00
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
yaml = YAML(typ='safe', pure=True)
|
2020-08-31 19:40:01 +03:00
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
basic_claims_map = {
|
|
|
|
'x-hasura-user-id': {
|
|
|
|
'path': "$.['https://myapp.com/jwt/claims'].user.id"
|
|
|
|
},
|
|
|
|
'x-hasura-allowed-roles': {
|
|
|
|
'path': "$.['https://myapp.com/jwt/claims'].role.allowed"
|
|
|
|
},
|
|
|
|
'x-hasura-default-role': {
|
|
|
|
'path': "$.['https://myapp.com/jwt/claims'].role.default"
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 19:40:01 +03:00
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
basic_claims_map_with_default_values = {
|
|
|
|
'x-hasura-user-id': {
|
|
|
|
'path': "$.['https://myapp.com/jwt/claims'].user.id",
|
|
|
|
'default': '1'
|
|
|
|
},
|
|
|
|
'x-hasura-allowed-roles': {
|
|
|
|
'path': "$.['https://myapp.com/jwt/claims'].role.allowed",
|
|
|
|
'default': ['user', 'editor']
|
|
|
|
},
|
|
|
|
'x-hasura-default-role': {
|
|
|
|
'path': "$.['https://myapp.com/jwt/claims'].role.default",
|
|
|
|
'default': 'user'
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 19:40:01 +03:00
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
static_claims_map = {
|
|
|
|
'x-hasura-user-id': {
|
|
|
|
'path': "$.['https://myapp.com/jwt/claims'].user.id"
|
|
|
|
},
|
|
|
|
'x-hasura-allowed-roles': ['user','editor'],
|
|
|
|
'x-hasura-default-role': 'user',
|
|
|
|
'x-hasura-custom-header': 'custom-value'
|
|
|
|
}
|
2020-08-31 19:40:01 +03:00
|
|
|
|
|
|
|
def clean_null_terms(d):
|
|
|
|
clean = {}
|
|
|
|
for k, v in d.items():
|
|
|
|
if isinstance(v, dict):
|
|
|
|
nested = clean_null_terms(v)
|
|
|
|
if len(nested.keys()) > 0:
|
|
|
|
clean[k] = nested
|
|
|
|
elif v is not None:
|
|
|
|
clean[k] = v
|
|
|
|
return clean
|
|
|
|
|
|
|
|
# TestJWTClaimsMapBasic will be called using two different JWT configs
|
|
|
|
# one with default values and the other without default values. The
|
|
|
|
# default values here is referred to the default value that's being
|
|
|
|
# used when a value is not found while looking up the JWT token using
|
|
|
|
# the JSON Path provided
|
2023-04-19 13:28:49 +03:00
|
|
|
@pytest.mark.admin_secret
|
2020-08-31 19:40:01 +03:00
|
|
|
@pytest.mark.parametrize('endpoint', ['/v1/graphql', '/v1alpha1/graphql'])
|
2023-04-19 13:28:49 +03:00
|
|
|
class AbstractTestJWTClaimsMapBasic:
|
2020-08-31 19:40:01 +03:00
|
|
|
def mk_claims(self, user_id=None, allowed_roles=None, default_role=None):
|
|
|
|
self.claims['https://myapp.com/jwt/claims'] = clean_null_terms({
|
|
|
|
'user': {
|
|
|
|
'id': user_id
|
|
|
|
},
|
|
|
|
'role': {
|
|
|
|
'allowed': allowed_roles,
|
|
|
|
'default': default_role
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_valid_claims_success(self, hge_ctx, jwt_configuration, endpoint):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims('1', ['user', 'editor'], 'user')
|
2023-04-19 13:28:49 +03:00
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
self.conf['status'] = 200
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_invalid_role_in_request_header(self, hge_ctx, jwt_configuration, endpoint):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims('1', ['contractor', 'editor'], 'contractor')
|
2023-04-19 13:28:49 +03:00
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
self.conf['response'] = {
|
|
|
|
'errors': [{
|
|
|
|
'extensions': {
|
|
|
|
'code': 'access-denied',
|
|
|
|
'path': '$'
|
|
|
|
},
|
|
|
|
'message': 'Your requested role is not in allowed roles'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
if endpoint == '/v1/graphql':
|
|
|
|
self.conf['status'] = 200
|
|
|
|
if endpoint == '/v1alpha1/graphql':
|
|
|
|
self.conf['status'] = 400
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_no_allowed_roles_in_claim(self, hge_ctx, jwt_configuration, endpoint):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims('1', None, 'user')
|
2023-04-19 13:28:49 +03:00
|
|
|
default_allowed_roles = jwt_configuration.server_configuration['claims_map']['x-hasura-allowed-roles'].get('default')
|
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
if default_allowed_roles is None:
|
|
|
|
self.conf['response'] = {
|
|
|
|
'errors': [{
|
|
|
|
'extensions': {
|
|
|
|
'code': 'jwt-missing-role-claims',
|
|
|
|
'path': '$'
|
|
|
|
},
|
|
|
|
'message': 'JWT claim does not contain x-hasura-allowed-roles'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
if endpoint == '/v1/graphql':
|
|
|
|
self.conf['status'] = 200
|
|
|
|
if endpoint == '/v1alpha1/graphql':
|
|
|
|
self.conf['status'] = 400
|
|
|
|
else:
|
|
|
|
self.conf['status'] = 200
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_invalid_allowed_roles_in_claim(self, hge_ctx, jwt_configuration, endpoint):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims('1', 'user', 'user')
|
2023-04-19 13:28:49 +03:00
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
self.conf['response'] = {
|
|
|
|
'errors': [{
|
|
|
|
'extensions': {
|
|
|
|
'code': 'jwt-invalid-claims',
|
|
|
|
'path': '$'
|
|
|
|
},
|
|
|
|
'message': 'invalid x-hasura-allowed-roles; should be a list of roles: parsing [] failed, expected Array, but encountered String'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
if endpoint == '/v1/graphql':
|
|
|
|
self.conf['status'] = 200
|
|
|
|
if endpoint == '/v1alpha1/graphql':
|
|
|
|
self.conf['status'] = 400
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_no_default_role(self, hge_ctx, jwt_configuration, endpoint):
|
2020-08-31 19:40:01 +03:00
|
|
|
# default_default_role is the default default role set in the JWT config
|
|
|
|
# when the lookup with the JSONPath fails, this is the value that will
|
|
|
|
# be used for the `x-hasura-default-role` claim
|
2023-04-19 13:28:49 +03:00
|
|
|
default_default_role = jwt_configuration.server_configuration['claims_map']['x-hasura-default-role'].get('default')
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims('1', ['user'])
|
2023-04-19 13:28:49 +03:00
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
if default_default_role is None:
|
|
|
|
self.conf['response'] = {
|
|
|
|
'errors': [{
|
|
|
|
'extensions': {
|
|
|
|
'code': 'jwt-missing-role-claims',
|
|
|
|
'path': '$'
|
|
|
|
},
|
|
|
|
'message': 'JWT claim does not contain x-hasura-default-role'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
if endpoint == '/v1/graphql':
|
|
|
|
self.conf['status'] = 200
|
|
|
|
if endpoint == '/v1alpha1/graphql':
|
|
|
|
self.conf['status'] = 400
|
|
|
|
else:
|
|
|
|
self.conf['status'] = 200
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_claim_not_found(self, hge_ctx, jwt_configuration, endpoint):
|
|
|
|
default_user_id = jwt_configuration.server_configuration['claims_map']['x-hasura-user-id'].get('default')
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims(None, ['user', 'editor'], 'user')
|
2023-04-19 13:28:49 +03:00
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
if default_user_id is None:
|
|
|
|
self.conf['response'] = {
|
|
|
|
'errors': [{
|
|
|
|
'extensions': {
|
|
|
|
'code': 'jwt-invalid-claims',
|
|
|
|
'path': '$'
|
|
|
|
},
|
|
|
|
'message': 'JWT claim from claims_map, x-hasura-user-id not found'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
if endpoint == '/v1/graphql':
|
|
|
|
self.conf['status'] = 200
|
|
|
|
if endpoint == '/v1alpha1/graphql':
|
|
|
|
self.conf['status'] = 400
|
|
|
|
else:
|
|
|
|
self.conf['status'] = 200
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def transact(self, setup):
|
|
|
|
self.dir = 'queries/graphql_query/permissions'
|
|
|
|
with open(self.dir + '/user_select_query_unpublished_articles.yaml') as c:
|
2022-01-17 10:39:59 +03:00
|
|
|
self.conf = yaml.load(c)
|
2020-08-31 19:40:01 +03:00
|
|
|
curr_time = datetime.now()
|
|
|
|
exp_time = curr_time + timedelta(hours=1)
|
|
|
|
self.claims = {
|
|
|
|
'sub': '1234567890',
|
|
|
|
'name': 'John Doe',
|
|
|
|
'iat': math.floor(curr_time.timestamp()),
|
|
|
|
'exp': math.floor(exp_time.timestamp())
|
|
|
|
}
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class')
|
2022-08-15 17:29:02 +03:00
|
|
|
def setup(self, postgis, hge_ctx):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.dir = 'queries/graphql_query/permissions'
|
2022-07-05 21:00:08 +03:00
|
|
|
hge_ctx.v1q_f(self.dir + '/setup.yaml')
|
2020-08-31 19:40:01 +03:00
|
|
|
yield
|
2022-07-05 21:00:08 +03:00
|
|
|
hge_ctx.v1q_f(self.dir + '/teardown.yaml')
|
2020-08-31 19:40:01 +03:00
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
|
|
|
|
@pytest.mark.jwt('rsa', { 'claims_map': basic_claims_map })
|
|
|
|
class TestJWTClaimsMapBasicWithRSA(AbstractTestJWTClaimsMapBasic):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.jwt('ed25519', { 'claims_map': basic_claims_map })
|
|
|
|
class TestJWTClaimsMapBasicWithEd25519(AbstractTestJWTClaimsMapBasic):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.jwt('rsa', { 'claims_map': basic_claims_map_with_default_values })
|
|
|
|
class TestJWTClaimsMapBasicWithRSAAndDefaultValues(AbstractTestJWTClaimsMapBasic):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.jwt('ed25519', { 'claims_map': basic_claims_map_with_default_values })
|
|
|
|
class TestJWTClaimsMapBasicWithEd25519AndDefaultValues(AbstractTestJWTClaimsMapBasic):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-08-31 19:40:01 +03:00
|
|
|
# The values of 'x-hasura-allowed-roles' and 'x-hasura-default-role' has
|
|
|
|
# been set in the JWT config
|
2023-04-19 13:28:49 +03:00
|
|
|
@pytest.mark.admin_secret
|
2020-08-31 19:40:01 +03:00
|
|
|
@pytest.mark.parametrize('endpoint', ['/v1/graphql', '/v1alpha1/graphql'])
|
2023-04-19 13:28:49 +03:00
|
|
|
class AbstractTestJWTClaimsMapWithStaticHasuraClaimsMapValues:
|
2020-08-31 19:40:01 +03:00
|
|
|
def mk_claims(self, user_id=None):
|
|
|
|
self.claims['https://myapp.com/jwt/claims'] = clean_null_terms({
|
|
|
|
'user': {
|
|
|
|
'id': user_id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_valid_claims_success(self, hge_ctx, jwt_configuration, endpoint):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims('1')
|
2023-04-19 13:28:49 +03:00
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
self.conf['headers']['x-hasura-custom-header'] = 'custom-value'
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
self.conf['status'] = 200
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_invalid_role_in_request_header(self, hge_ctx, jwt_configuration, endpoint):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims('1')
|
2023-04-19 13:28:49 +03:00
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
self.conf['headers']['X-Hasura-Role'] = 'random_string'
|
|
|
|
self.conf['response'] = {
|
|
|
|
'errors': [{
|
|
|
|
'extensions': {
|
|
|
|
'code': 'access-denied',
|
|
|
|
'path': '$'
|
|
|
|
},
|
|
|
|
'message': 'Your requested role is not in allowed roles'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
if endpoint == '/v1/graphql':
|
|
|
|
self.conf['status'] = 200
|
|
|
|
if endpoint == '/v1alpha1/graphql':
|
|
|
|
self.conf['status'] = 400
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
2023-04-19 13:28:49 +03:00
|
|
|
def test_jwt_claims_map_claim_not_found(self, hge_ctx, jwt_configuration, endpoint):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.mk_claims(None)
|
2023-04-19 13:28:49 +03:00
|
|
|
token = jwt.encode(self.claims, jwt_configuration.private_key, algorithm=jwt_configuration.algorithm)
|
2020-08-31 19:40:01 +03:00
|
|
|
self.conf['headers']['Authorization'] = 'Bearer ' + token
|
|
|
|
self.conf['response'] = {
|
|
|
|
'errors': [{
|
|
|
|
'extensions': {
|
|
|
|
'code': 'jwt-invalid-claims',
|
|
|
|
'path': '$'
|
|
|
|
},
|
|
|
|
'message': 'JWT claim from claims_map, x-hasura-user-id not found'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
self.conf['url'] = endpoint
|
|
|
|
if endpoint == '/v1/graphql':
|
|
|
|
self.conf['status'] = 200
|
|
|
|
if endpoint == '/v1alpha1/graphql':
|
|
|
|
self.conf['status'] = 400
|
|
|
|
check_query(hge_ctx, self.conf, add_auth=False)
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def transact(self, setup):
|
|
|
|
self.dir = 'queries/graphql_query/permissions'
|
|
|
|
with open(self.dir + '/user_select_query_unpublished_articles.yaml') as c:
|
2022-01-17 10:39:59 +03:00
|
|
|
self.conf = yaml.load(c)
|
2020-08-31 19:40:01 +03:00
|
|
|
curr_time = datetime.now()
|
|
|
|
exp_time = curr_time + timedelta(hours=1)
|
|
|
|
self.claims = {
|
|
|
|
'sub': '1234567890',
|
|
|
|
'name': 'John Doe',
|
|
|
|
'iat': math.floor(curr_time.timestamp()),
|
|
|
|
'exp': math.floor(exp_time.timestamp())
|
|
|
|
}
|
|
|
|
|
|
|
|
@pytest.fixture(scope='class')
|
2022-08-15 17:29:02 +03:00
|
|
|
def setup(self, postgis, hge_ctx):
|
2020-08-31 19:40:01 +03:00
|
|
|
self.dir = 'queries/graphql_query/permissions'
|
2022-07-05 21:00:08 +03:00
|
|
|
hge_ctx.v1q_f(self.dir + '/setup.yaml')
|
2020-08-31 19:40:01 +03:00
|
|
|
yield
|
2022-07-05 21:00:08 +03:00
|
|
|
hge_ctx.v1q_f(self.dir + '/teardown.yaml')
|
2023-04-19 13:28:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.jwt('rsa', { 'claims_map': static_claims_map })
|
|
|
|
class TestJWTClaimsMapWithStaticHasuraClaimsMapValuesWithRSA(AbstractTestJWTClaimsMapWithStaticHasuraClaimsMapValues):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.jwt('ed25519', { 'claims_map': static_claims_map })
|
|
|
|
class TestJWTClaimsMapWithStaticHasuraClaimsMapValuesWithEd25519(AbstractTestJWTClaimsMapWithStaticHasuraClaimsMapValues):
|
|
|
|
pass
|