Skip to content

feat(oauth2): Add support for runtime and environment-specific options #460

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

MathiasCiarlo
Copy link
Collaborator

@MathiasCiarlo MathiasCiarlo commented Sep 25, 2019

The problem

Currently all the scheme options are baked into the bundle during build.
Because of this - you can't use the same bundle in multiple environments (e.g. test and prod) if you have options that are environment-specific.

Example use case

Some organisations have dedicated auth servers for each environment:
Prod: login.example.com/oauth-path
Test: login-test.example.com/oauth-path

The developers want to deploy the same bundle in all environments, and supply an environment variable AUTH_URL which points to the correct url.

The developers can't set authorization_endpoint: process.env.AUTH_URL in the options, because this variable is not known at build time.

Proposed solution

To solve this issue, I propose we introduce a new option: _runtimeOptions, which is evaluated runtime:

auth: {
  strategies: {
    yourFavoriteProvider: {
      _scheme: 'oauth2',
      scope: ['openid', 'profile', 'email'],
      _runtimeOptions: () => {
        return {
          client_id: process.env.CLIENT_ID,
          authorization_endpoint: process.env.AUTH_URL + '/auth',
          access_token_endpoint: process.env.AUTH_URL + '/token',
          userinfo_endpoint: process.env.AUTH_URL + '/userinfo'
        }
      }
    }
  }
}

The _runtimeOptions function will be evaluated and merged with the other options during the initial request to the server.

Security concerns

I evaluated the risk of creating a function runtime (see eval is evil), but I think it should be fine because the function is only evaluated at build time, removing the risk of XSS.

Alternative solution

If we can't do it this way, maybe we could supply a list of environment variable name mappings?:

options: {
  _scheme: 'oauth2',
  _optionsFromEnvironment: {
    client_id: AUTH_CLIENT_ID,
    authorization_endpoint: AUTH_ENDPOINT,
    token_endpoint: AUTH_TOKEN_ENDPOINT
  }
}

i.e: client_id would be mapped to process.env.AUTH_CLIENT_ID

Although this method is less flexible, it does not rely on creating functions runtime.

Caveats

  • Only supported in universal mode (I think we can get around this, though)

What about local scheme?

This feature is very easy to add for local schemes, but I could not come up with a use case, so I left it out. Please tell me if you have a use case and think it should be available for local schemes 🙂

What do you think about this proposal? 😄

@pi0
Copy link
Member

pi0 commented Oct 7, 2019

Hi @MathiasCiarlo. Thanks for this PR. I think we can support it for both SPA and Universal modes (but not generated). So triggered a core team discussion if we can have it in core

@pi0 pi0 added the pending label Oct 7, 2019
@MathiasCiarlo
Copy link
Collaborator Author

Thanks, @pi0! Excited to hear what you guys figure out 😄

@jbmolle
Copy link

jbmolle commented Dec 9, 2019

Hi,
What is the status of this PR?
We've developed an app with Next Auth and we would like to package it with Docker and environment variables to have one Docker image for multiple environment.
For that we need this PR.
Thanks,
JB

@riccardogioratoatoms
Copy link

Any news on this PR?
I would need this feature too or this plugin is not usable without a dynamic endpoint at runtime.

@vehagn
Copy link

vehagn commented Oct 16, 2020

I'm patiently awaiting this feature as well. Is there anything I can do to help with it getting merged, @pi0 ?

@acarlsson
Copy link

acarlsson commented Aug 12, 2021

Me and the team are also awaiting this feature. We need it for dynamic API endpoints at runtime. Any idea when it will be merged? @pi0

EDIT: Found this comment #713 (comment) that schema solution worked for us with todays version of @nuxtjs/auth-next. Now we can use runtime env variables for our auth endpoints.

@wensaint
Copy link

I want to change the cookie options at runningtime, Any news on this PR?

@rijnhard
Copy link

rijnhard commented May 7, 2023

Would this cover for example setting window.location? this way the settings can be configured based on where its deployed. e.g.

auth: {
  strategies: {
    yourFavoriteProvider: {
      _scheme: 'oauth2',
      scope: ['openid', 'profile', 'email'],
      _runtimeOptions: () => {
        return {
          redirectURI: window.location.origin + '/logged-out'
        }
      }
    }
  }
}

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

Successfully merging this pull request may close these issues.

8 participants