Skip to content

Commit

Permalink
add openid connect login
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed Jul 3, 2024
1 parent 4def4f8 commit c881a75
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
POSTGRES_PASSWORD=postgres
DJANGO_SECRET_KEY=secret

# Generate a client id/client secret on keycloack
OIDC_CLIENT_ID=
OIDC_SECRET=
OIDC_PROVIDER_URL=https://keycloak.nina.no/realms/nina/
7 changes: 6 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ volumes:
x-django-env: &django-env
DATABASE_URL: "postgis://postgres:${POSTGRES_PASSWORD}@postgres:5432/postgres?sslmode=disable"
DJANGO_SETTINGS_MODULE: config.settings.production
DJANGO_ACCOUNT_ALLOW_REGISTRATION: "False"
USE_DOCKER: "yes"
OIDC_PROVIDER_ID: nina
OIDC_CLIENT_ID: "${OIDC_CLIENT_ID}"
OIDC_SECRET: "${OIDC_SECRET}"
OIDC_PROVIDER_URL: "${OIDC_PROVIDER_URL}"
OIDC_PROVIDER_NAME: NINA


x-django-prod-env: &django-prod-env
<<: *django-env
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies = [
"django-template-partials",
"neapolitan",
"fontawesomefree",
"django-allauth-ui==0.3.8",
"django-allauth-ui==1.1.6",
"django-widget-tweaks",
"django-taggit",
"django-tables2",
Expand Down
11 changes: 11 additions & 0 deletions src/apps/users/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
from typing import Self

from allauth.account.adapter import DefaultAccountAdapter
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from django.conf import settings
from django.http import HttpRequest


class AccountAdapter(DefaultAccountAdapter):
def is_open_for_signup(self: Self, request: HttpRequest) -> bool:
return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)


class SocialAccountAdapter(DefaultSocialAccountAdapter):
"""
just for debugging obscure integration exceptions
"""

def authentication_error(self, *args, **kwargs):
print(args, kwargs)
return super().authentication_error(*args, **kwargs)
21 changes: 20 additions & 1 deletion src/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@

# django-allauth
# ------------------------------------------------------------------------------
ACCOUNT_ALLOW_REGISTRATION = env.bool("DJANGO_ACCOUNT_ALLOW_REGISTRATION", False)
ACCOUNT_ALLOW_REGISTRATION = True
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_AUTHENTICATION_METHOD = "email"
# https://django-allauth.readthedocs.io/en/latest/configuration.html
Expand All @@ -317,9 +317,28 @@
ACCOUNT_EMAIL_VERIFICATION = "none"
# https://django-allauth.readthedocs.io/en/latest/configuration.html
ACCOUNT_ADAPTER = "apps.users.adapters.AccountAdapter"
SOCIALACCOUNT_ADAPTER = "apps.users.adapters.SocialAccountAdapter"
# https://django-allauth.readthedocs.io/en/latest/forms.html
ACCOUNT_FORMS = {"signup": "apps.users.forms.UserSignupForm"}

SOCIALACCOUNT_ONLY = True
SOCIALACCOUNT_PROVIDERS = {
"openid_connect": {
"APPS": [
{
"provider_id": env("OIDC_PROVIDER_ID"),
"name": env("OIDC_PROVIDER_NAME"),
"client_id": env("OIDC_CLIENT_ID"),
"secret": env("OIDC_SECRET"),
"settings": {
"server_url": env("OIDC_PROVIDER_URL"),
},
},
]
}
}


# Django REST-Framework
REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": [
Expand Down

0 comments on commit c881a75

Please sign in to comment.