|
7 | 7 | from custodia import log
|
8 | 8 | from custodia.plugin import HTTPAuthenticator, PluginOption
|
9 | 9 |
|
| 10 | +import requests |
| 11 | + |
10 | 12 |
|
11 | 13 | class SimpleCredsAuth(HTTPAuthenticator):
|
12 | 14 | uid = PluginOption('pwd_uid', -1, "User id or name, -1 ignores user")
|
@@ -131,3 +133,62 @@ def handle(self, request):
|
131 | 133 | self.audit_svc_access(log.AUDIT_SVC_AUTH_FAIL,
|
132 | 134 | request['client_id'], dn)
|
133 | 135 | return False
|
| 136 | + |
| 137 | + |
| 138 | +class OpenIDCTokenAuth(HTTPAuthenticator): |
| 139 | + token_info_url = PluginOption(str, None, |
| 140 | + 'URL for getting token information') |
| 141 | + client_id = PluginOption(str, None, |
| 142 | + 'Client ID for verifying tokens') |
| 143 | + client_secret = PluginOption(str, None, |
| 144 | + 'Client Secret for verifying tokens') |
| 145 | + scope = PluginOption(str, 'custodia', 'OAuth2 scope to require') |
| 146 | + |
| 147 | + def _get_token_info(self, token): |
| 148 | + return requests.post(self.token_info_url, |
| 149 | + data={'client_id': self.client_id, |
| 150 | + 'client_secret': self.client_secret, |
| 151 | + 'token': token, |
| 152 | + 'token_type_hint': 'Bearer'}).json() |
| 153 | + |
| 154 | + def handle(self, request): |
| 155 | + token = None |
| 156 | + if 'Authorization' in request['headers']: |
| 157 | + hdr = request['headers']['Authorization'] |
| 158 | + if hdr.startswith('Bearer '): |
| 159 | + self.logger.debug('Bearer token provided in header') |
| 160 | + token = hdr[len('Bearer '):] |
| 161 | + else: |
| 162 | + self.logger.debug('Unrecognized Authorization header') |
| 163 | + return False |
| 164 | + elif request.get('access_token'): |
| 165 | + self.logger.debug('Token provided in form') |
| 166 | + token = request.get('access_token') |
| 167 | + else: |
| 168 | + self.logger.debug('Missing any credentials in request') |
| 169 | + return False |
| 170 | + |
| 171 | + if not token: |
| 172 | + self.logger.debug('No token') |
| 173 | + return False |
| 174 | + |
| 175 | + try: |
| 176 | + tokeninfo = self._get_token_info(token) |
| 177 | + except: |
| 178 | + self.logger.debug('Error getting token information', |
| 179 | + exc_info=True) |
| 180 | + return False |
| 181 | + |
| 182 | + aud = tokeninfo['aud'] |
| 183 | + if isinstance(aud, list) and self.client_id not in aud: |
| 184 | + self.logger.debug('My client ID not in audience list') |
| 185 | + return False |
| 186 | + elif self.client_id != aud: |
| 187 | + self.logger.debug('Client ID is not audience') |
| 188 | + return False |
| 189 | + |
| 190 | + if self.scope not in tokeninfo['scope'].split(' '): |
| 191 | + self.logger.debug('Required scope not found') |
| 192 | + return False |
| 193 | + |
| 194 | + return True |
0 commit comments