question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The automatic base64 encoding makes token cookie too large to save

See original GitHub issue

Describe the bug Our Keycloak installation returns a token with length approximately around 3800 characters long. Technically that should be short enough to be stored as a cookie but it fails because when saving the SSRCookies persistor base64 encodes the token which makes it around 5000 characters length.

To Reproduce Steps to reproduce the behavior:

  1. Use <SSRKeycloakProvider /> with the SSRCookies persistor.
  2. Perform login and receive tokens token + idToken.
  3. If the token length is for instance 3800 characters long it will get base64 encoded into a 5000 character long string before being stored as a cookie. And Firefox will complain that the 5000 character kcToken cookie is too long to store.

Expected behavior

I expected the token cookie to be successfully set.

Desktop (please complete the following information):

  • OS: macOS 11.2.3
  • Browser Firefox
  • Version 87

Additional context:

There might off course be good reasons as to why it is necessary to encode / decode these token strings. If that is the case, feel free to close this issue.

Many thanks for creating this solid library. 😄

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
nilsnhcommented, Sep 2, 2021

@aakay . We ended up building our own persistor that we pass into React Keycloak:

import { TokenPersistor } from "@react-keycloak/ssr/lib/persistors/types"

type Tokens = { idToken: string; token: string }

/**
 * We replace the default "token persistor" defined by @react-keycloak/ssr
 * with our own because the default used Cookie storage, and our
 * tokens were too large for that. This uses sessionStorage instead.
 */
export const SessionPersistor = (cookies: any): TokenPersistor => ({
  setTokens: ({ idToken, token }: Tokens) => {
    !!token && setToken("kcToken", token)
    !!idToken && setToken("kcIdToken", idToken)
  },
  getTokens: () => {
    const tknStr = getToken("kcToken", cookies)
    const idTknStr = getToken("kcIdToken", cookies)
    return {
      idToken: idTknStr || "",
      refreshToken: "",
      token: tknStr || "",
    }
  },
  resetTokens: () => {
    removeToken("kcToken")
    removeToken("kcIdToken")
  },
})

function setToken(name: string, val: string) {
  return sessionStorage.setItem(name, val)
}

function getToken(name: string, cookies = {}) {
  return typeof window === "undefined"
    ? cookies[name]
    : sessionStorage.getItem(name)
}

function removeToken(name: string) {
  sessionStorage.removeItem(name)
}

And then we use it like so:

<SSRKeycloakProvider 
      keycloakConfig={config}
      initOptions={keycloakInitOptions}
      persistor={SessionPersistor(cookies)} />

Hope this helps! ✌️

0reactions
aakaycommented, May 26, 2022

@nilsnh hi nils, sorry, i’ve somehow missed your response. thanks for the help. this looks like a good solution. thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cookies, Session, Token
Another issue: the data in the token is saved in plaintext (by using Base64 for encoding, but it is not encrypted. So, sensitive...
Read more >
Base64 encoding and decoding in client-side Javascript
I used this method to encode a SVG in base64 with the Date URI scheme. Surprise: this function urlencodes every character, so I...
Read more >
Cookie vs Token authentication
This article provides readers with a detailed guide on Token based authentication and Cookie based authentication and the advantages and ...
Read more >
Cookies in Postman And How to Manage ...
Now go the Headers tab in the response section. Header_Response_2. Here you will find Set_Cookie which is the cookie being sent by the...
Read more >
GraphQL file uploads - evaluating the 5 most common ...
Keep in mind that base64 encoded files are part of the enclosing JSON object. This means, you're not able to "stream" this base64...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found