-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OpenID Connect Token authenticator #167
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
|
||
from cryptography.hazmat.primitives import constant_time | ||
|
||
import requests | ||
|
||
from custodia import log | ||
from custodia.plugin import HTTPAuthenticator, PluginOption | ||
|
||
|
@@ -131,3 +133,62 @@ def handle(self, request): | |
self.audit_svc_access(log.AUDIT_SVC_AUTH_FAIL, | ||
request['client_id'], dn) | ||
return False | ||
|
||
|
||
class OpenIDCTokenAuth(HTTPAuthenticator): | ||
token_info_url = PluginOption(str, None, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
'URL for getting token information') | ||
client_id = PluginOption(str, None, | ||
'Client ID for verifying tokens') | ||
client_secret = PluginOption(str, None, | ||
'Client Secret for verifying tokens') | ||
scope = PluginOption(str, 'custodia', 'OAuth2 scope to require') | ||
|
||
def _get_token_info(self, token): | ||
return requests.post(self.token_info_url, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other plugins accept a CA cert argument to customize TLS verification. We could have global option for trusted CA cert, too. |
||
data={'client_id': self.client_id, | ||
'client_secret': self.client_secret, | ||
'token': token, | ||
'token_type_hint': 'Bearer'}).json() | ||
|
||
def handle(self, request): | ||
token = None | ||
if 'Authorization' in request['headers']: | ||
hdr = request['headers']['Authorization'] | ||
if hdr.startswith('Bearer '): | ||
self.logger.debug('Bearer token provided in header') | ||
token = hdr[len('Bearer '):] | ||
else: | ||
self.logger.debug('Unrecognized Authorization header') | ||
return False | ||
elif request.get('access_token'): | ||
self.logger.debug('Token provided in form') | ||
token = request.get('access_token') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
else: | ||
self.logger.debug('Missing any credentials in request') | ||
return False | ||
|
||
if not token: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it necessary to check token again? |
||
self.logger.debug('No token') | ||
return False | ||
|
||
try: | ||
tokeninfo = self._get_token_info(token) | ||
except: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't use a bare except. |
||
self.logger.debug('Error getting token information', | ||
exc_info=True) | ||
return False | ||
|
||
aud = tokeninfo['aud'] | ||
if isinstance(aud, list) and self.client_id not in aud: | ||
self.logger.debug('My client ID not in audience list') | ||
return False | ||
elif self.client_id != aud: | ||
self.logger.debug('Client ID is not audience') | ||
return False | ||
|
||
if self.scope not in tokeninfo['scope'].split(' '): | ||
self.logger.debug('Required scope not found') | ||
return False | ||
|
||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a doc string with an example how to configure and use this plugin with e.g. Ipsilon.