robrotheram / taiga-contrib-openid-auth

Taiga plugin for openid authentication
45 stars 25 forks source link

Error using Apereo CAS OpenID #37

Open CartierPierre opened 2 years ago

CartierPierre commented 2 years ago

Hi, I tried to connect with my CAS OpenID server, and encounter some troubles

taiga-back_1             | Traceback (most recent call last):
taiga-back_1             |   File "/opt/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
taiga-back_1             |     response = get_response(request)
taiga-back_1             |   File "/opt/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
taiga-back_1             |     response = self.process_exception_by_middleware(e, request)
taiga-back_1             |   File "/opt/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
taiga-back_1             |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
taiga-back_1             |   File "/taiga-back/taiga/base/api/viewsets.py", line 95, in view
taiga-back_1             |     return self.dispatch(request, *args, **kwargs)
taiga-back_1             |   File "/opt/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
taiga-back_1             |     return view_func(*args, **kwargs)
taiga-back_1             |   File "/taiga-back/taiga/base/api/views.py", line 449, in dispatch
taiga-back_1             |     response = self.handle_exception(exc)
taiga-back_1             |   File "/taiga-back/taiga/base/api/views.py", line 447, in dispatch
taiga-back_1             |     response = handler(request, *args, **kwargs)
taiga-back_1             |   File "/taiga-back/taiga/auth/api.py", line 77, in create
taiga-back_1             |     data = auth_plugins[login_type]['login_func'](request)
taiga-back_1             |   File "/taiga-back/taiga-contrib-openid-auth/back/taiga_contrib_openid_auth/services.py", line 96, in openid_login_func
taiga-back_1             |     token=token)
taiga-back_1             |   File "/usr/local/lib/python3.7/contextlib.py", line 74, in inner
taiga-back_1             |     return func(*args, **kwds)
taiga-back_1             |   File "/taiga-back/taiga-contrib-openid-auth/back/taiga_contrib_openid_auth/services.py", line 60, in openid_register
taiga-back_1             |     username_unique = slugify_uniquely(username, user_model, slugfield="username")
taiga-back_1             |   File "/taiga-back/taiga/base/utils/slug.py", line 28, in slugify_uniquely
taiga-back_1             |     potential = base = django_slugify(unidecode(value))
taiga-back_1             |   File "/opt/venv/lib/python3.7/site-packages/unidecode/__init__.py", line 48, in unidecode_expect_ascii
taiga-back_1             |     bytestring = string.encode('ASCII')
taiga-back_1             | AttributeError: 'NoneType' object has no attribute 'encode'

It seems that the username isn't recovered from the OpenID, here is my docker compose file

x-environment:
  &default-back-environment
  ## Some params are hidden
  # Enable OpenID to allow to register users if they do not exist. Set to false to disable all signups
  PUBLIC_REGISTER_ENABLED: "True"
  # OpenID settings
  ENABLE_OPENID: "True"
  OPENID_USER_URL : "https://{cas-server}/cas/oidc/profile"
  OPENID_TOKEN_URL : "https://{cas-server}/cas/oidc/token"
  OPENID_CLIENT_ID : "taiga"
  OPENID_CLIENT_SECRET : "abc"
  OPENID_SCOPE : "openid email"
  OPENID_USERNAME_FIELD : "uid"

  taiga-front:
    image: robrotheram/taiga-front-openid:latest
    environment:
      TAIGA_URL: "http://myserv:9000"
      TAIGA_WEBSOCKETS_URL: "ws://myserv:9000"
      TAIGA_SUBPATH: "" # "" or "/subpath"
      ENABLE_OPENID: "true"
      OPENID_URL : "https://{cas-server}/cas/oidc/authorize"
      OPENID_CLIENT_ID : "taiga"
      OPENID_NAME: "CAS"

How can I log the user attributes to know if there is something in uid ?

I also tried OPENID_USERNAME_FIELD : "email"

CartierPierre commented 2 years ago

I found some troubles with custom field. First, this code is missing, I'll send a pull request later : https://github.com/robrotheram/taiga-contrib-openid-auth/commit/fbd65c15801e355140a69baa896fbe325c71b4dd

In addition, I finally logged what my CAS OpenID server responds :

{
  "sub":"mylogin",
  "service":"http://imiev.univ-lemans.fr:9000/login",
  "auth_time":1645109775,
  "attributes":{
    "givenName":"John",
    "mail":"john.doe@mail.com",
    "Sn":"Doe"},
  "id":"mylogin",
  "client_id":"taiga"
}

So, if I link OPENID_EMAIL_FIELD to mail or email, it should find it in attributes I don't know if it's just a peculiarity of the CAS OpenID.

Then I'll adapt your code to my forked branch to fill my config