OAuth2 Keycloak authentication #1590
Replies: 24 comments 1 reply
-
| 
         Using v5 without any issues with public client :  keycloak: {
        scheme: 'oauth2',
        endpoints: {
          authorization: `${process.env.KEYCLOAK_REMOTE_HOST}/auth/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/auth`,
          token: `${process.env.KEYCLOAK_REMOTE_HOST}/auth/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`,
          logout: `${process.env.KEYCLOAK_REMOTE_HOST}/auth/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/logout?redirect_uri=` + encodeURIComponent(String(process.env.REMOTE_API))
        },
        token: {
          property: 'access_token',
          type: 'Bearer',
          name: 'Authorization',
          maxAge: 1800 // Can be dynamic ?
        },
        refreshToken: {
          property: 'refresh_token',
          maxAge: 60 * 60 * 24 * 30 // Can be dynamic ? 
        },
        responseType: 'code',
        grantType: 'authorization_code',
        clientId: process.env.KEYCLOAK_CLIENT_ID,
        scope: ['openid', 'profile', 'email'],
        codeChallengeMethod: 'S256',
      }
    } | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Im using auth-next. So there is a double initialization of the Attributes, as there are two question marks in the url and that leads to a wrong logout redirect in the Browser. Has anyone the same problem or can help? My nuxt.config.js strategie looks like: keycloak: {
        scheme: 'oauth2',
        endpoints: {
          authorization: `${process.env.KEYCLOAK_REMOTE_HOST}/auth/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/auth`,
          token: `${process.env.KEYCLOAK_REMOTE_HOST}/auth/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/token`,
          logout: `${process.env.KEYCLOAK_REMOTE_HOST}/auth/realms/${process.env.KEYCLOAK_REALM}/protocol/openid-connect/logout?redirect_uri=` + encodeURIComponent(String(process.env.REMOTE_API))
        },
        token: {
          property: 'access_token',
          type: 'Bearer'
        },
        refreshToken: {
          property: 'refresh_token',
          type: 'Bearer'
        },
        responseType: 'id_token token',
        clientId: process.env.KEYCLOAK_CLIENT_ID,
        scope: ['profile', 'email'],
        accessType: 'implicit',
      }What I want to have is an url like this:  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         @Philschke you should not specify the   | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         @jper92 Specifying the  When the url parameter is manually changed to  Why is the redirect not working with   | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         @agravelot Hello, I have a problem when using @ nuxt / auth, I use it to interact with keycloak openID, with my settings. After I have passed the authorization, I am thrown to my page with pkce_code_verifier, but auth does not make a request to get tokens, if I do it manually with this code, then I will get tokens. What am I doing wrong, please tell me  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Hi all, My workaround was to create a new scheme overriding the logout function: Nuxtjs Logout Fix for Keycloak The best solution would be to give the possibility to specify the request parameter in the options.  | 
  
Beta Was this translation helpful? Give feedback.
-
        For others who are landing here searching for Logoutjust to sum it up... The ProblemLogout will give a white KeyCloak Page, no redirect is handled. File Referenceauth-module/src/schemes/oauth2.ts Lines 262 to 272 in 1122b76 DetailsKeyCloak Expects  While the default OAuth2 Module will submits the arg  SolutionProvided by @alvistar 
  | 
  
Beta Was this translation helpful? Give feedback.
-
        
 I was able to login on Keycloak with this settings but is not pulling the user information, did you had the same issue?.  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         I'm not using userinfo endpoint, only jwt content for my case  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         @eramosr16 i am using this config auth: {
    strategies: {
      keycloak: {
        scheme: '~/plugins/keycloak.ts',
        endpoints: {
          token:
            'https://auth.host.tld/auth/realms/myRealm/protocol/openid-connect/token',
          authorization:
            'https://auth.host.tld//auth/realms/myRealm/protocol/openid-connect/auth',
          userInfo:
            'https://auth.host.tld/auth/realms/myRealm/protocol/openid-connect/userinfo',
          logout:
            'https://auth.host.tld/auth/realms/myRealm/protocol/openid-connect/logout',
        },
        token: {
          property: 'access_token',
          type: 'Bearer',
          name: 'Authorization',
        },
        refreshToken: {
          property: 'refresh_token',
        },
        grantType: 'authorization_code',
        responseType: 'code',
        codeChallengeMethod: 'S256',
        clientId: 'myApp-backend-ui',
        scope: ['openid', 'profile', 'roles'],
      },
    },
  },But never checked the values if they are used, but they are available  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Also having issues with keycloak logout redirect not working but can't seem to make any of the suggested solutions work with the latest version of auth-next  I tried using the example provided here https://gist.github.com/alvistar/b7adad5eb086915a67ad316452e86b8a but none of those import paths exist in the latest version of auth-next. The proposed solution to create your own Scheme https://auth.nuxtjs.org/guide/scheme#creating-your-own-scheme doesn't seem to work. When I load my custom theme ... I get  Anyone managed to find a solution that works? Cheers!  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Hi @m2de! When creating custom schemes, you should import from  Like this: import { Oauth2Scheme } from "~auth/runtime"
export default class KeycloakScheme extends Oauth2Scheme {
  ...
} | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Hi @JoaoPedroAS51. The   | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Hi @jyhubert! Can you tell me which version of auth module you are using and what you are trying to import? Also, are you using typescript?  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         @JoaoPedroAS51   | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         @jyhubert We're aware that there's an error with imports. For now, you can import from  
 declare module '~auth/runtime' {
  // Here you declare what you're trying to import from `~auth/runtime`
  export type { Oauth2Scheme } from '@nuxtjs/auth-next'
}Then in your scheme you import like    | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Thanks @JoaoPedroAS51. I will try it out.  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Working with "@nuxtjs/auth-next": "^5.0.0-1613647907.37b1156" nuxt.config.js plugins/keycloak.js node_modules/@nuxtjs/auth-next/dist/runtime.mjs  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Hi, There is different problems addressed in this topic but I'm encountering the problem with the logout URL. 
 @cs8898 summed it very well, Keycloak is waiting for  I tried @xeniumlee version of @alvistar solution and yes I have logout and redirection but: 
 Any help will really be appreciated please :) EDIT, I finally got it to work! 
  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Sorry, I forgot a bit the community. But indeed, something is not working well with the redirection. Concerning the  import { stringify as encodeQuery } from 'querystring'
import { Oauth2Scheme } from '@nuxtjs/auth-next/dist/runtime'
export default RhssoScheme extends Oauth2Scheme {
   logout (): void {
      const { endpoints: { logout }, clientId: client_id } = this.options
      if (logout) {
         const opts = {
            client_id,
            post_logout_redirect_uri: this.logoutRedirectURI,
         }
         const url = `${logout}?${encodeQuery(opts)}`
         location.replace(url)
      }
      return this.$auth.reset()
   }
} | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         @cdefy I Just created a "sample" repo to show you how i made it working int the "first" attempt while not editing the node_modules folder https://github.com/cs8898/nuxt-auth-auth2-keycloak-sample in an second run i started to edit the whole lib in a way that suites best for me, with some additions the link is in the sample repo (afaik i haven't documented my changes, but there are some nice to have additions) EDITSeems you took the same approche with the plugin  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Thanks @jyhubert and @cs8898 for your feedbacks. Yes it's working now, I don't touch it anymore ^^ !  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         I am wondering who is following the oauth standard, keycloak or nuxt/auth-next ? or oauth 2.0 didn't specify the standard parameter?  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         I found out that openIDConnect scheme is using the correct URL param, and it works : auth-module/src/schemes/openIDConnect.ts Line 171 in d57e832  | 
  
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, something new about this? Can we use it in v5?
https://cmty.app/nuxt/auth-module/issues/c445
https://dev.to/johanneslichtenberger/how-to-implement-nuxt-js-vue-js-oauth2-authentication-with-an-external-rest-api-server-based-on-vert-x-kotlin-and-keycloak-3c1h
Beta Was this translation helpful? Give feedback.
All reactions