Nuxt2-Keycloak-Django Rest Framework Authentication

57 Views Asked by At

I've integrated keycloak authentication in my nuxt2 application using nuxt-auth, here my auth configration in nuxt.config.js

auth: {
    strategies: {
      oidc: {
        scheme: "openIDConnect",
        clientId: process.env.KEYCLOAK_CLIENT_ID,
        endpoints: {
          configuration: `/keycloak/`,
        },
        responseType: "code",
        grantType: "authorization_code",
        scope: ["openid", "profile", "email"],
        codeChallengeMethod: "S256",
        token: {
          property: "access_token",
          type: "Bearer",
          name: "Authorization",
          maxAge: 300,
        },
        refreshToken: {
          property: "refresh_token",
          maxAge: 60 * 60 * 24 * 30,
        },
        // acrValues: "1",
      },
    },
    redirect: {
      login: "/",
      logout: "/",
      home: `/user`,
    },
  },

Now I want to authenticate my django rest framework API with this client keycloak accesstoken

I tried django-oauth-toolkit package for keycloak authentication.

and configration

INSTALLED_APPS = [
    ...
    "oauth2_provider",
    "rest_framework",
]
REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "oauth2_provider.contrib.rest_framework.OAuth2Authentication",
    ),
    "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
}

OAUTH2_PROVIDER = {
    "SCOPES": {
        "openid": "OpenID Connect",
        "profile": "User profile information",
        "email": "User email address",
    }
}

while I tried to call my api with the nuxt keycloak access token it returns me 401-Authentication credentials were not provided.

Please help me with this scenario

or tell me if it possible to authenticate django APIs with nuxt keycloak accesstoken

NB: Client used in nuxt and django are different.

0

There are 0 best solutions below