Salta al contenuto
0
  • Home
  • Piero Bosio
  • Blog
  • Mondo
  • Fediverso
  • News
  • Categorie
  • Recenti
  • Popolare
  • Tag
  • Utenti
  • Home
  • Piero Bosio
  • Blog
  • Mondo
  • Fediverso
  • News
  • Categorie
  • Recenti
  • Popolare
  • Tag
  • Utenti
Skin
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Predefinito (Nessuna skin)
  • Nessuna skin
Collassa

Piero Bosio Web Site

Forum federato con il resto del mondo. Non contano le istanze, contano le persone

  1. Home
  2. Categorie
  3. Senza categoria
  4. Unable to Retrieve CSRF Token via /api/config Using express.sid After Keycloak Login

Unable to Retrieve CSRF Token via /api/config Using express.sid After Keycloak Login

Pianificato Fissato Bloccato Spostato Senza categoria
4 Post 3 Autori 1 Visualizzazioni
  • Da Vecchi a Nuovi
  • Da Nuovi a Vecchi
  • Più Voti
Rispondi
  • Topic risposta
Effettua l'accesso per rispondere
Questa discussione è stata eliminata. Solo gli utenti con diritti di gestione possono vederla.
  • baluundefined Questo utente è esterno a questo forum
    baluundefined Questo utente è esterno a questo forum
    balu
    scritto su ultima modifica di
    #1

    Hi,
    I have a question regarding the express.sid cookie:

    After login, the express.sid is generated and stored in the cookies.

    I tried using this session ID to fetch the CSRF token by calling /api/config, but it doesn’t seem to work for me.

    My goal is:

    Successfully retrieve a valid CSRF token.

    Use it to create topics or posts via the Write API.

    Ensure that once I have this token and session, I can access all the required NodeBB APIs.

    Could you please clarify the correct approach to:

    Retrieve and use the CSRF token with the express.sid?

    Authenticate API requests (like creating topics or posts) when using Keycloak for login instead of NodeBB’s built-in login?

    Any guidance or best practices for this integration would be greatly appreciated.

    app.post('/api/login', async (req, res) => {
    try {
    const { username, password } = req.body || {}
    if (!username || !password) {
    return res.status(400).json({ error: 'Missing username or password' })
    }
    // 1) Login against Keycloak (Direct Access Grant)
    const tokenResponse = await fetchKeycloakTokens({ username, password });

    // 2) Get user profile to build NodeBB payload
    const userinfo = await fetchUserInfo({ accessToken: tokenResponse.access_token });
    
    // 3) Build session-sharing JWT for NodeBB
    // Minimal fields: id (unique), username, email
    const nodebbPayload = {
        id: userinfo.sub || userinfo.preferred_username || username,
        username: userinfo.preferred_username || username,
        email: userinfo.email || undefined,
        fullname: userinfo.name || undefined,
    };
    if (!SESSION_SHARING_JWT_SECRET) {
        return res.status(500).json({ error: 'Server not configured: SESSION_SHARING_JWT_SECRET missing' });
    }
    const signed = await jwt.sign(nodebbPayload, SESSION_SHARING_JWT_SECRET, { expiresIn: '1h' });
    
     const loginRes = await axios.post('http://localhost:4567/api/v3/utilities/login', {
        username,
        password
    }, {
        headers: {
            "Content-Type": "application/json",
            "Authorization": `Bearer ${signed}`, // pass your Bearer token here
        },
        withCredentials: true
    });
    
    console.log(loginRes, "LOGIN RESPONSE");
    // 4) Set cookie for NodeBB domain so it can pick it up
    const response = await axios.get('http://192.168.60.108:4567/api/config', { withCredentials: true });
    const newToken = response?.data?.csrf_token;
    
    const cookieOptions = {
        httpOnly: true,
        secure: false,
        sameSite: 'none',
        path: '/',
        maxAge: 8 * 60 * 60 * 10000,
        domain: '192.168.60.108'
    }
    
    res.cookie("token", signed, cookieOptions);
    res.cookie('csrf_token', newToken, cookieOptions);
    
    return res.json({ success: true, message: "login successfully", redirect: NODEBB_BASE_URL, keycloakAccessToken: tokenResponse.access_token });
    

    } catch (err) {
    // eslint-disable-next-line no-console
    console.error('Login error:', err?.response?.data || err?.message || err);
    const status = err?.response?.status || 500
    const message = err?.response?.data?.error_description || err?.response?.data?.error || err?.message || 'Unexpected error';
    return res.status(status).json({ success: false, error: message });
    }

    });
    In the /api/login API, I called the /api/config API, but when I check the /api/config API in Postman using the express.sid generated after login, it never returns the response for that specific user.

    Thanks in advance!

    HenryCharlesundefined baluundefined 2 Risposte Ultima Risposta
    • baluundefined balu

      Hi,
      I have a question regarding the express.sid cookie:

      After login, the express.sid is generated and stored in the cookies.

      I tried using this session ID to fetch the CSRF token by calling /api/config, but it doesn’t seem to work for me.

      My goal is:

      Successfully retrieve a valid CSRF token.

      Use it to create topics or posts via the Write API.

      Ensure that once I have this token and session, I can access all the required NodeBB APIs.

      Could you please clarify the correct approach to:

      Retrieve and use the CSRF token with the express.sid?

      Authenticate API requests (like creating topics or posts) when using Keycloak for login instead of NodeBB’s built-in login?

      Any guidance or best practices for this integration would be greatly appreciated.

      app.post('/api/login', async (req, res) => {
      try {
      const { username, password } = req.body || {}
      if (!username || !password) {
      return res.status(400).json({ error: 'Missing username or password' })
      }
      // 1) Login against Keycloak (Direct Access Grant)
      const tokenResponse = await fetchKeycloakTokens({ username, password });

      // 2) Get user profile to build NodeBB payload
      const userinfo = await fetchUserInfo({ accessToken: tokenResponse.access_token });
      
      // 3) Build session-sharing JWT for NodeBB
      // Minimal fields: id (unique), username, email
      const nodebbPayload = {
          id: userinfo.sub || userinfo.preferred_username || username,
          username: userinfo.preferred_username || username,
          email: userinfo.email || undefined,
          fullname: userinfo.name || undefined,
      };
      if (!SESSION_SHARING_JWT_SECRET) {
          return res.status(500).json({ error: 'Server not configured: SESSION_SHARING_JWT_SECRET missing' });
      }
      const signed = await jwt.sign(nodebbPayload, SESSION_SHARING_JWT_SECRET, { expiresIn: '1h' });
      
       const loginRes = await axios.post('http://localhost:4567/api/v3/utilities/login', {
          username,
          password
      }, {
          headers: {
              "Content-Type": "application/json",
              "Authorization": `Bearer ${signed}`, // pass your Bearer token here
          },
          withCredentials: true
      });
      
      console.log(loginRes, "LOGIN RESPONSE");
      // 4) Set cookie for NodeBB domain so it can pick it up
      const response = await axios.get('http://192.168.60.108:4567/api/config', { withCredentials: true });
      const newToken = response?.data?.csrf_token;
      
      const cookieOptions = {
          httpOnly: true,
          secure: false,
          sameSite: 'none',
          path: '/',
          maxAge: 8 * 60 * 60 * 10000,
          domain: '192.168.60.108'
      }
      
      res.cookie("token", signed, cookieOptions);
      res.cookie('csrf_token', newToken, cookieOptions);
      
      return res.json({ success: true, message: "login successfully", redirect: NODEBB_BASE_URL, keycloakAccessToken: tokenResponse.access_token });
      

      } catch (err) {
      // eslint-disable-next-line no-console
      console.error('Login error:', err?.response?.data || err?.message || err);
      const status = err?.response?.status || 500
      const message = err?.response?.data?.error_description || err?.response?.data?.error || err?.message || 'Unexpected error';
      return res.status(status).json({ success: false, error: message });
      }

      });
      In the /api/login API, I called the /api/config API, but when I check the /api/config API in Postman using the express.sid generated after login, it never returns the response for that specific user.

      Thanks in advance!

      HenryCharlesundefined Questo utente è esterno a questo forum
      HenryCharlesundefined Questo utente è esterno a questo forum
      HenryCharles
      scritto su ultima modifica di
      #2

      You’ll need both the session cookie and its matching CSRF token. Log in so NodeBB sets the cookie, then call /api/config with it to get the token. With Keycloak, it’s best to use the session-sharing plugin and JWT so NodeBB handles sessions and CSRF for you.

      1 Risposta Ultima Risposta
      • baluundefined balu

        Hi,
        I have a question regarding the express.sid cookie:

        After login, the express.sid is generated and stored in the cookies.

        I tried using this session ID to fetch the CSRF token by calling /api/config, but it doesn’t seem to work for me.

        My goal is:

        Successfully retrieve a valid CSRF token.

        Use it to create topics or posts via the Write API.

        Ensure that once I have this token and session, I can access all the required NodeBB APIs.

        Could you please clarify the correct approach to:

        Retrieve and use the CSRF token with the express.sid?

        Authenticate API requests (like creating topics or posts) when using Keycloak for login instead of NodeBB’s built-in login?

        Any guidance or best practices for this integration would be greatly appreciated.

        app.post('/api/login', async (req, res) => {
        try {
        const { username, password } = req.body || {}
        if (!username || !password) {
        return res.status(400).json({ error: 'Missing username or password' })
        }
        // 1) Login against Keycloak (Direct Access Grant)
        const tokenResponse = await fetchKeycloakTokens({ username, password });

        // 2) Get user profile to build NodeBB payload
        const userinfo = await fetchUserInfo({ accessToken: tokenResponse.access_token });
        
        // 3) Build session-sharing JWT for NodeBB
        // Minimal fields: id (unique), username, email
        const nodebbPayload = {
            id: userinfo.sub || userinfo.preferred_username || username,
            username: userinfo.preferred_username || username,
            email: userinfo.email || undefined,
            fullname: userinfo.name || undefined,
        };
        if (!SESSION_SHARING_JWT_SECRET) {
            return res.status(500).json({ error: 'Server not configured: SESSION_SHARING_JWT_SECRET missing' });
        }
        const signed = await jwt.sign(nodebbPayload, SESSION_SHARING_JWT_SECRET, { expiresIn: '1h' });
        
         const loginRes = await axios.post('http://localhost:4567/api/v3/utilities/login', {
            username,
            password
        }, {
            headers: {
                "Content-Type": "application/json",
                "Authorization": `Bearer ${signed}`, // pass your Bearer token here
            },
            withCredentials: true
        });
        
        console.log(loginRes, "LOGIN RESPONSE");
        // 4) Set cookie for NodeBB domain so it can pick it up
        const response = await axios.get('http://192.168.60.108:4567/api/config', { withCredentials: true });
        const newToken = response?.data?.csrf_token;
        
        const cookieOptions = {
            httpOnly: true,
            secure: false,
            sameSite: 'none',
            path: '/',
            maxAge: 8 * 60 * 60 * 10000,
            domain: '192.168.60.108'
        }
        
        res.cookie("token", signed, cookieOptions);
        res.cookie('csrf_token', newToken, cookieOptions);
        
        return res.json({ success: true, message: "login successfully", redirect: NODEBB_BASE_URL, keycloakAccessToken: tokenResponse.access_token });
        

        } catch (err) {
        // eslint-disable-next-line no-console
        console.error('Login error:', err?.response?.data || err?.message || err);
        const status = err?.response?.status || 500
        const message = err?.response?.data?.error_description || err?.response?.data?.error || err?.message || 'Unexpected error';
        return res.status(status).json({ success: false, error: message });
        }

        });
        In the /api/login API, I called the /api/config API, but when I check the /api/config API in Postman using the express.sid generated after login, it never returns the response for that specific user.

        Thanks in advance!

        baluundefined Questo utente è esterno a questo forum
        baluundefined Questo utente è esterno a questo forum
        balu
        scritto su ultima modifica di
        #3

        HenryCharles Thank you. I’m using the session-sharing plugin, but I’m facing an issue. After logging in with SSO, I call NodeBB APIs directly (not the UI). The express.sid is generated, and I use that express.sid to get the CSRF token. However, when I call the /api/config API, the response always gives uid: 0 instead of the logged-in user’s UID.

        julianundefined 1 Risposta Ultima Risposta
        • baluundefined balu

          HenryCharles Thank you. I’m using the session-sharing plugin, but I’m facing an issue. After logging in with SSO, I call NodeBB APIs directly (not the UI). The express.sid is generated, and I use that express.sid to get the CSRF token. However, when I call the /api/config API, the response always gives uid: 0 instead of the logged-in user’s UID.

          julianundefined Questo utente è esterno a questo forum
          julianundefined Questo utente è esterno a questo forum
          julian
          scritto su ultima modifica di
          #4

          balu hey, sorry for the delay, can you post a cURL call to the /api/config endpoint with the cookie?

          I know you posted a code sample (via DM) but the reason I ask for a cURL call is that it is the most straightforward way to test the call and often eliminates errors in header values, etc.

          1 Risposta Ultima Risposta
          Rispondi
          • Topic risposta
          Effettua l'accesso per rispondere
          • Da Vecchi a Nuovi
          • Da Nuovi a Vecchi
          • Più Voti


          Gli ultimi otto messaggi ricevuti dalla Federazione

          • Luca Sironiundefined
            Luca Sironi

            D'altra parte ognuno ha gli amici che si merita

            https://www.tmz.com/2025/09/15/donald-trump-skips-kennedy-center-honors-charlie-kirk/?adid=social-tw

            per saperne di più

          • ginoundefined
            gino

            La gente a Gaza non può andarsene e in queste ore entrano i carri armati mentre Idf bombarda...Non ho parole #stopgenocide

            per saperne di più

          • Christine Lemmer-Webberundefined
            Christine Lemmer-Webber

            shit's fucked

            per saperne di più

          • Andre123 :tux: :gnu:undefined
            Andre123 :tux: :gnu:

            Change your profile to Clippy !

            Parla un pò veloce, ma il contenuto del video secondo me è interessante. In pratica dice: 20 anni fa Clippy era forse inutile, ma voleva aiutare senza fregarti dati, manipolarti, spiarti ecc.
            Oggi le aziende cercano di sfruttarti in ogni modo possibile. Era meglio Clippy, magari poco utile, ma voleva solo aiutare. E allora usiamo Clippy come immagine profilo per dire ai big tech: ora avete rotto le pal....

            https://www.youtube.com/watch?v=2_Dtmpe9qaQ

            #clippy #rossman

            per saperne di più

          • Silviaundefined
            Silvia

            @marcoboh @matz
            Sono basita per come, in Italia, si agitino per creare un clima d’odio, di contrapposizione , approfittando di un fatto accaduto negli usa, scaturito da un loro contesto.

            per saperne di più

          • Evan Prodromouundefined
            Evan Prodromou

            @skinnylatte So, for racism, we use the term "anti-racist" to mean not just "not racist", but committed to actively dismantling systems of racial oppression.

            Is there a similar term for people who are anti-homophobic and anti-transphobic? Is it just "ally"?

            per saperne di più

          • DigiDavidex :kde:undefined
            DigiDavidex :kde:

            Fine di un era! Dopo 30 anni stop al servizio di connessione dial-up 📞💻

            Il 30 settembre 2025 chiude un pezzo di storia di internet: AOL, ex-America Online, interromperà il suo servizio dialup, conosciuto da tutti per il celebre suono di connessione via modem.

            Così le connessioni via linea telefonica lasciano spazio alla rete internet in fibra e mobile. Ed è subito nostalgia: vi ricordate del suono dei vecchi 56k?

            https://www.ilsole24ore.com/art/aol-fine-un-era-30-anni-stop-servizio-connessione-dial-up-AHSACm9B

            #PC #computer #modem #56k #dialup

            per saperne di più

          • Sleepwithdemonsundefined
            Sleepwithdemons

            @evadm @notizie @valhalla @SalirandaDuedicoppe @brigitesposito @mammaincampagna @mondo-cucito
            Grazie! Non vedo l'ora di postare!!!
            Appena riesco a mettere in fila due neuroni per capire come c*o accedere. Madò come mi sento boomer.

            per saperne di più
          • Accedi

          • Accedi o registrati per effettuare la ricerca.
          Powered by NodeBB Contributors
          • Primo post
            Ultimo post