Aucune description

oidc_handler.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # IRIS Source Code
  2. # contact@dfir-iris.org
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 3 of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public License
  15. # along with this program; if not, write to the Free Software Foundation,
  16. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. # OIDC Configuration
  18. from oic.oic import Client
  19. from oic.utils.authn.client import CLIENT_AUTHN_METHOD
  20. from oic.oic.message import RegistrationResponse
  21. from oic.oic.message import ProviderConfigurationResponse
  22. def get_oidc_client(app) -> Client:
  23. client = Client(client_authn_method=CLIENT_AUTHN_METHOD)
  24. # retrieve provider configuration dynamically from metadata
  25. # or fall back to env vars
  26. try:
  27. client.provider_config(app.config.get("OIDC_ISSUER_URL"))
  28. except Exception as e:
  29. app.logger.warning(f"Could not read OIDC metadata, using environment variables - error {e}")
  30. op_info = ProviderConfigurationResponse(
  31. issuer=app.config.get("OIDC_ISSUER_URL"),
  32. authorization_endpoint=app.config.get("OIDC_AUTH_ENDPOINT"),
  33. token_endpoint=app.config.get("OIDC_TOKEN_ENDPOINT"),
  34. end_session_endpoint=app.config.get("OIDC_END_SESSION_ENDPOINT"),
  35. )
  36. client.handle_provider_config(op_info, op_info['issuer'])
  37. info = {
  38. "client_id": app.config.get("OIDC_CLIENT_ID"),
  39. "client_secret": app.config.get("OIDC_CLIENT_SECRET")
  40. }
  41. client_reg = RegistrationResponse(**info)
  42. client.store_registration_info(client_reg)
  43. return client