@@ -148,6 +148,48 @@ def authentication():
148148 f"served by this authenticator." )
149149 return True
150150
151+ # token should come from `Authorization: Bearer $token` header. rather than x-tapis-token
152+ # this endpoint takes both, converts Authorization to x-tapis-token for simplicity
153+ if '/v3/oauth2/userinfo/oidc' in request .url_rule .rule :
154+ logger .debug (f"top of /v3/oauth2/userinfo/oidc auth: request.headers: { request .headers } " )
155+
156+ auth_token = request .headers .get ('Authorization' )
157+ if auth_token and auth_token .startswith ('Bearer ' ) and not request .headers .get ('X-Tapis-Token' ):
158+ try :
159+ # overwrite the headers via wsgi environ. request.headers itself is read-only
160+ tapis_token = auth_token .replace ('Bearer ' , '' )
161+ logger .debug (f"found auth header; setting environ X-Tapis-Token to { tapis_token } " )
162+
163+ # tokens might have aud, if jwt.decode in tapisservice doesn't specify expected aud you'll
164+ # get invalid aud. Either we can somehow pop aud or specify to jwt.decode(options={'verify_aud': False})
165+ # Instead of verify = false we can also specify a list of valid auds. Pop aud would require
166+ # re-encoding+sigining key. We don't have private tenant key in auth though. Ignoring for now, only
167+ # bookstack looks for this when running their auth.
168+ # resolve_tenant_id_for_request decode needs aud to expect - https://github.com/jpadilla/pyjwt/blob/master/docs/usage.rst#audience-claim-aud
169+
170+ # modify the WSGI environment directly
171+ # wsgi requires headers be uppercase, no dashes, and prefixed with HTTP_
172+ request .environ ['HTTP_X_TAPIS_TOKEN' ] = tapis_token
173+ except Exception as e :
174+ logger .error (f"found auth header, but failed to parse it; exception: { e } " )
175+
176+ # debug logs
177+ try :
178+ headers = request .headers
179+ logger .debug (f"before auth.authentication(). request.headers: { headers } " )
180+ except Exception as e :
181+ pass
182+
183+ auth .authentication ()
184+ # always resolve the request tenant id based on the URL:
185+ auth .resolve_tenant_id_for_request ()
186+ # make sure this request is for a tenant served by this authenticator
187+ if g .request_tenant_id not in conf .tenants :
188+ raise common_errors .PermissionsError (f"The request is for a tenant ({ g .request_tenant_id } ) that is not "
189+ f"served by this authenticator." )
190+ logger .debug (f"End of v3/oauth2/userinfo/oidc auth: final request_tenant_id: { g .request_tenant_id } " )
191+ return True
192+
151193 # the profiles endpoints always use standard Tapis Token auth -
152194 if '/v3/oauth2/profiles' in request .url_rule .rule or \
153195 '/v3/oauth2/userinfo' in request .url_rule .rule :
0 commit comments