title: Django-OIDC with keycloak - OIDC callback state not found in session oidc_states
body: <p>I am trying to implement Keycloak SSO with Django API using OIDC.
  Getting below error message after requesting the call to Keyclock as a response</p>
  <p><em>SuspiciousOperation at /^oidc/callback/<br />
  OIDC callback state not found in session <code>oidc_states</code>!</em></p>
  <p>The same thing I did in the Flask framework and it's working as expected.</p>
  <p>Please find the below details.</p>
  <p><em><strong>Django Log:</strong></em></p>
  <pre><code>[20/Mar/2021 15:30:57] &quot;GET / HTTP/1.1&quot; 404 2229
  [20/Mar/2021 15:31:03] &quot;GET /hi HTTP/1.1&quot; 302 0
  [20/Mar/2021 15:31:03] &quot;GET /oidcauthenticate/?next=/hi HTTP/1.1&quot; 302 0
  failed to get or create user: Claims verification failed
  [20/Mar/2021 15:31:11] &quot;GET /oidccallback/?state=UziLDF2ZcE9p2WrUIihUahgUsWdQ8zYQ&amp;code=22d5a22b-16a6-42e3-81ba-9b569a3f1c81.2cd52631-8c4c-4f2e-a718-c908611336a3.81db1495-24af-402d-8244-a82f32bf4355 HTTP/1.1&quot; 302 0
  Not Found: /
  [20/Mar/2021 15:31:11] &quot;GET / HTTP/1.1&quot; 404 2229
  
  </code></pre>
  <p><strong>Settings:</strong></p>
  <pre><code>Django settings for keycloakexample project.
  
  Generated by 'django-admin startproject' using Django 3.1.7.
  
  For more information on this file, see
  https://docs.djangoproject.com/en/3.1/topics/settings/
  
  For the full list of settings and their values, see
  https://docs.djangoproject.com/en/3.1/ref/settings/
  &quot;&quot;&quot;
  
  from pathlib import Path
  
  # Build paths inside the project like this: BASE_DIR / 'subdir'.
  BASE_DIR = Path(__file__).resolve().parent.parent
  
  # Quick-start development settings - unsuitable for production
  # See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
  
  # SECURITY WARNING: keep the secret key used in production secret!
  SECRET_KEY = '_vg@)t*rri()^^wm7yl*i&amp;$%gk2h1h%!xk$xms19ceb3*2z&amp;g^'
  
  # SECURITY WARNING: don't run with debug turned on in production!
  DEBUG = True
  
  ALLOWED_HOSTS = []
  
  # Application definition
  
  INSTALLED_APPS = [
      'django.contrib.admin',
      'django.contrib.auth',
      'django.contrib.contenttypes',
      'django.contrib.sessions',
      'django.contrib.messages',
      'django.contrib.staticfiles',
      'keycloak_oidc',
      'demo',
  ]
  
  MIDDLEWARE = [
      'django.middleware.security.SecurityMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.middleware.common.CommonMiddleware',
      'django.middleware.csrf.CsrfViewMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'django.contrib.messages.middleware.MessageMiddleware',
      'django.contrib.sessions.middleware.SessionMiddleware',
      'django.contrib.auth.middleware.AuthenticationMiddleware',
      'mozilla_django_oidc.middleware.SessionRefresh',
  ]
  
  ROOT_URLCONF = 'keycloakexample.urls'
  
  TEMPLATES = [
      {
          'BACKEND': 'django.template.backends.django.DjangoTemplates',
          'DIRS': [],
          'APP_DIRS': True,
          'OPTIONS': {
              'context_processors': [
                  'django.template.context_processors.debug',
                  'django.template.context_processors.request',
                  'django.contrib.auth.context_processors.auth',
                  'django.contrib.messages.context_processors.messages',
              ],
          },
      },
  ]
  
  WSGI_APPLICATION = 'keycloakexample.wsgi.application'
  
  # Database
  # https://docs.djangoproject.com/en/3.1/ref/settings/#databases
  
  DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.sqlite3',
          'NAME': BASE_DIR / 'db.sqlite3',
      }
  }
  
  # Password validation
  # https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
  
  AUTH_PASSWORD_VALIDATORS = [
      {
          'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
      },
      {
          'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
      },
  ]
  
  # Internationalization
  # https://docs.djangoproject.com/en/3.1/topics/i18n/
  
  LANGUAGE_CODE = 'en-us'
  
  TIME_ZONE = 'UTC'
  
  USE_I18N = True
  
  USE_L10N = True
  
  USE_TZ = True
  
  # Static files (CSS, JavaScript, Images)
  # https://docs.djangoproject.com/en/3.1/howto/static-files/
  
  STATIC_URL = '/static/'
  from keycloak_oidc.default_settings import *
  
  
  OIDC_OP_AUTHORIZATION_ENDPOINT = 'http://localhost:8080/auth/realms/aaa/protocol/openid-connect/auth'
  OIDC_OP_TOKEN_ENDPOINT = 'http://localhost:8080/auth/realms/aaa/protocol/openid-connect/token'
  OIDC_OP_USER_ENDPOINT = 'http://localhost:8080/auth/realms/aaa/protocol/openid-connect/userinfo'
  OIDC_OP_JWKS_ENDPOINT = 'http://localhost:8080/auth/realms/aaa/protocol/openid-connect/certs'
  OIDC_OP_LOGOUT_ENDPOINT = 'http://localhost:8080/auth/realms/aaa/protocol/openid-connect/logout'
  
  
  AUTHENTICATION_BACKENDS = (
      'keycloak_oidc.auth.OIDCAuthenticationBackend',
  )
  
  
  OIDC_RP_CLIENT_ID = 'test'
  OIDC_RP_CLIENT_SECRET = 'd6e007e-1098-4146-a217-04e4b1d1ae9b'
  
  # REST_FRAMEWORK = dict(
  #     DEFAULT_AUTHENTICATION_CLASSES=(
  #         'mozilla_django_oidc.contrib.drf.OIDCAuthentication',
  #         'rest_framework.authentication.SessionAuthentication'
  #     )
  # )
  
  </code></pre>
  <p><strong>View:</strong></p>
  <pre><code>from django.http import HttpResponse
  from django.contrib.auth.decorators import login_required
  
  
  @login_required()
  def hello(request):
      print('123')
      return HttpResponse('hello')
  
  
  @login_required
  def hi(request):
      print('123')
      return HttpResponse('hi')
  
  </code></pre>
  <p><strong>urls:</strong></p>
  <pre><code>from django.urls import path, include
  
  
  urlpatterns = [
      path('oidc', include('keycloak_oidc.urls')),
      path('', include('demo.urls')),
  
  ]
  
  </code></pre>