Skip to content

Commit

Permalink
[jwt] Add issuer and audience for token verification (#2505)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing2fly committed Aug 31, 2021
1 parent b122cce commit 218575a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions desktop/conf.dist/hue.ini
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ idle_session_timeout=-1
# Endpoint to fetch the public key from verification server.
## key_server_url=https://ext_authz:8000

# The identifier of the service issued the JWT
## issuer=None

# The identifier of the resource intend to access
## audience=None

# Verify custom JWT signature.
## verify=true

Expand Down
6 changes: 6 additions & 0 deletions desktop/conf/pseudo-distributed.ini.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@
# Endpoint to fetch the public key from verification server.
## key_server_url=https://ext_authz:8000

# The identifier of the service issued the JWT
## issuer=None

# The identifier of the resource intend to access
## audience=None

# Verify custom JWT signature.
## verify=true

Expand Down
11 changes: 11 additions & 0 deletions desktop/core/src/desktop/auth/api_authentications.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,25 @@ def authenticate(self, request):
payload = jwt.decode(
access_token,
public_key_pem,
issuer=AUTH.JWT.ISSUER.get(),
audience=AUTH.JWT.AUDIENCE.get(),
algorithms=["RS256"],
verify=AUTH.JWT.VERIFY.get()
)
except jwt.DecodeError:
LOG.error('JwtAuthentication: Invalid token')
raise exceptions.AuthenticationFailed('JwtAuthentication: Invalid token')
except jwt.ExpiredSignatureError:
LOG.error('JwtAuthentication: Token expired')
raise exceptions.AuthenticationFailed('JwtAuthentication: Token expired')
except jwt.InvalidIssuerError:
LOG.error('JwtAuthentication: issuer not match')
raise exceptions.AuthenticationFailed('JwtAuthentication: issuer not matching')
except jwt.InvalidAudienceError:
LOG.error('JwtAuthentication: audience not match or no audience')
raise exceptions.AuthenticationFailed('JwtAuthentication: audience not matching or no audience')
except Exception as e:
LOG.error('JwtAuthentication: %s' % str(e))
raise exceptions.AuthenticationFailed(e)

if payload.get('user') is None:
Expand Down
6 changes: 5 additions & 1 deletion desktop/core/src/desktop/auth/api_authentications_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ def test_handle_public_key(self):

resets = [
AUTH.JWT.VERIFY.set_for_testing(True),
AUTH.JWT.KEY_SERVER_URL.set_for_testing('https://ext-authz:8000')
AUTH.JWT.KEY_SERVER_URL.set_for_testing('https://ext-authz:8000'),
AUTH.JWT.ISSUER.set_for_testing('issuer'),
AUTH.JWT.AUDIENCE.set_for_testing('audience')
]

try:
Expand All @@ -194,6 +196,8 @@ def test_handle_public_key(self):
b'Vno2e527clXzQisfJKwb4hjfKRMhHfnYfyJxaoHqWfx8DjXmH3CMqlWr/+hL3y1+\n'
b'4QIDAQAB\n'
b'-----END PUBLIC KEY-----\n',
issuer=AUTH.JWT.ISSUER.get(),
audience=AUTH.JWT.AUDIENCE.get(),
algorithms=['RS256'],
verify=True
)
Expand Down
15 changes: 14 additions & 1 deletion desktop/core/src/desktop/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,8 @@ def get_deprecated_login_lock_out_by_combination_browser_user_agent():
help=_("The service to use when querying PAM. "
"The service usually corresponds to a single filename in /etc/pam.d")),
PAM_USE_PWD_MODULE=Config("pam_use_pwd_module",
help=_("To use Python unix pwd module to get the username from the entered credentials in hue if Centrify like PAM service is in use. "
help=_("To use Python unix pwd module to get the username from the entered credentials in hue"
" if Centrify like PAM service is in use. "
"This will set the username to what is being returned by the pwd module."),
type=coerce_bool,
default=False),
Expand Down Expand Up @@ -1216,6 +1217,18 @@ def get_deprecated_login_lock_out_by_combination_browser_user_agent():
type=str,
help=_("Endpoint to fetch the public key from verification server.")
),
ISSUER=Config(
key="issuer",
default=None,
type=str,
help=_("The identifier of the service issued the JWT")
),
AUDIENCE=Config(
key="audience",
default=None,
type=str,
help=_("The identifier of the resource intend to access")
),
VERIFY=Config(
key="verify",
default=True,
Expand Down

0 comments on commit 218575a

Please sign in to comment.