Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different cookie secret based on request #289

Open
2 tasks done
jonaskello opened this issue Jun 25, 2024 · 1 comment
Open
2 tasks done

Different cookie secret based on request #289

jonaskello opened this issue Jun 25, 2024 · 1 comment

Comments

@jonaskello
Copy link

jonaskello commented Jun 25, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

We have a multi-tenant site where the tenant name is in the url like tenant1.site.com, tenant2.site.com etc.

I would like each tenant to have a different session secret in fastify session which seems to boil down to the cookie secret. Today it is possible to implement a custom signer, however it does not receive the request as a parameter so it is not possible to use different secrets for sign/unsign per tenant.

I think if the custom signer could receive request as a parameter this could be solved.

    sign: (value, req) => { ... }

However, maybe there is a better already solution to my particular scenario?

Motivation

Support for multi tenancy.

Example

fastify.register(require('@fastify/cookie'), {
  secret: {
    sign: (value, req) => {
      // get tenant from req and fetch corresponding secret
      // sign cookie with the tenant's secret
      return signedValue
    },
    unsign: (value, req) => {
      // get tenant from req and fetch corresponding secret
      // unsign cookie with the tenant's secret
      return {
        valid: true, // the cookie has been unsigned successfully
        renew: false, // the cookie has been unsigned with an old secret
        value: 'unsignedValue'
      }
    }
  }
})
@mcollina
Copy link
Member

mcollina commented Jul 1, 2024

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants