mozilla / mozilla-django-oidc

A django OpenID Connect library
https://mozilla-django-oidc.readthedocs.io
Mozilla Public License 2.0
452 stars 169 forks source link

getting error : Authentication credentials were not provided #356

Closed rkpip3 closed 12 months ago

rkpip3 commented 4 years ago

using Keycloak, I have created user in keycloak, trying to get api via Curl getting

"detail": "Authentication credentials were not provided."

any i have tried https://github.com/mozilla/mozilla-django-oidc/issues/265

my curl call : curl --location --request GET 'http://127.0.0.1:8000/api/' \ --header 'Authorization: Token eyJhbGciOiJSUzI1NiIsInR5'

OS: windows Browser : chrome

settings.py

`INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'mozilla_django_oidc', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'core', ]

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.middleware.clickjacking.XFrameOptionsMiddleware', 'mozilla_django_oidc.middleware.SessionRefresh', ]

AUTHENTICATION_BACKENDS = ( 'mozilla_django_oidc.auth.OIDCAuthenticationBackend', 'django.contrib.auth.backends.ModelBackend', )

OIDC_RP_CLIENT_ID = 'XXXXXClientID' OIDC_RP_CLIENT_SECRET = '541ac339-cf0e-44b1-b6bc-685ab037cf6c' OIDC_OP_AUTHORIZATION_ENDPOINT = "http://localhost:8088/auth/realms/XXXXX/protocol/openid-connect/auth" OIDC_OP_TOKEN_ENDPOINT = "http://localhost:8088/auth/realms/XXXXX/protocol/openid-connect/token" OIDC_OP_USER_ENDPOINT = "http://localhost:8088/auth/realms/XXXXX/protocol/openid-connect/userinfo" LOGIN_REDIRECT_URL = "http://127.0.0.1:8000/api" LOGOUT_REDIRECT_URL = "http://127.0.0.1:8000"
OIDC_RP_SIGN_ALGO = 'RS256' OIDC_OP_JWKS_ENDPOINT = 'http://localhost:8088/auth/realms/XXXXX/protocol/openid-connect/certs' OIDC_STORE_ID_TOKEN = True

REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'mozilla_django_oidc.contrib.drf.OIDCAuthentication',

'rest_framework.authentication.TokenAuthentication',

'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
    'rest_framework.permissions.IsAuthenticated',
)

}

OIDC_DRF_AUTH_BACKEND = 'mozilla_django_oidc.auth.OIDCAuthenticationBackend'`

views.py

from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.permissions import IsAuthenticated from django.shortcuts import render from rest_framework import viewsets from mozilla_django_oidc.auth import OIDCAuthenticationBackend from mozilla_django_oidc.utils import absolutify, import_from_settings from . import models from . import serializers class QuestionViewSet(viewsets.ModelViewSet): queryset = models.Question.objects.all() serializer_class = serializers.QuestionSerializer

serializers.py

`from rest_framework import serializers from .models import Question from mozilla_django_oidc.auth import OIDCAuthenticationBackend

class QuestionSerializer(serializers.ModelSerializer): class Meta: fields = ( 'id', 'question_text', 'pub_date', ) model = Question`

akatsoulas commented 12 months ago

Due to lack of activity, I am closing this as stale.