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

♻️ refactor: Support Environment Variable Inference For NextAuth #3701

Merged
merged 22 commits into from
Sep 12, 2024

Conversation

cy948
Copy link
Contributor

@cy948 cy948 commented Aug 31, 2024

💻 变更类型 | Change Type

  • ✨ feat
  • 🐛 fix
  • ♻️ refactor
  • 💄 style
  • 👷 build
  • ⚡️ perf
  • 📝 docs
  • 🔨 chore

🔀 变更说明 | Description of Change

  • package.json: 将next-auth@auth/core更新到最新版本;
  • src/config/auth.ts
    • NEXT_AUTH_SECRET变量的废弃提醒;
    • 各SSO环境变量的废弃提醒;
  • src/config/__tests__/auth.test.ts: 测试是否有废弃提醒;

📝 补充信息 | Additional Information

  1. 将变量名对齐到 NextAuth 以减少日后的变量维护成本、SSO Provider接入成本。新版的变量名将会与 NextAuth 文档中一致:
  • 使用过NextAuth的用户可以直接从NextAuth文档中获取要配置的环境变量;
  • 未使用过NextAuth的用户可根据lobe文档及NextAuth环境变量推断规则(以下简称“env infer”)推断出需要使用的环境变量;
  • 新版环境变量示例:AUTH_[Provider]_[Param],其中Providerauth0,github...ParamID,SECRET,ISSUER此类;
  1. 用户优先的配置模式:允许用户通过环境变量对配置进行修改,在社区建议配置和用户配置之间优先选择用户配置。至此,NextAuth的配置文件已完全符合NextAuth环境变量推断规则。注:不包含变量 NEXT_AUTH_SECRET
  2. TODO: 有部分冗余的代码用于migration,已使用TODO进行标记,migration结束后应该被清理;
  3. 往后接入 sso provider 时不需要从 authEnv 中读取变量,当配置中不含clientId, issuer时next-auth才会进行env infer,示例:
  • 官方Provider,即原生支持env infer:

在传入config时,若config的key中不包含issuer,则会从环境变量中读取AUTH_[PROVIDER]_ISSUER

const provider = {
  id: 'auth0',
  provider: Auth0({
    ...CommonProviderConfig,
    // Specify auth scope, at least include 'openid email'
    // all scopes in Auth0 ref: https://auth0.com/docs/get-started/apis/scopes/openid-connect-scopes#standard-claims
    authorization: { params: { scope: 'openid email profile' } },
    profile(profile) {
      return {
        email: profile.email,
        image: profile.picture,
        name: profile.name,
        providerAccountId: profile.sub,
      };
    },
  }),
};
  • 社区支持provider,不原生支持env infer:

目前自定义provider暂无这个特性,只能进行显式定义

const provider = {
  id: 'generic-oidc',
  provider: {
    ...CommonProviderConfig,
    authorization: { params: { scope: 'email openid profile' } },
    checks: ['state', 'pkce'],
    clientId: process.env.AUTH_GENERIC_OIDC_ID,
    clientSecret: process.env.AUTH_GENERIC_OIDC_SECRET,
    id: 'generic-oidc',
    issuer: process.env.AUTH_GENERIC_OIDC_ISSUER,
    name: 'Generic OIDC',
    profile(profile) {
      return {
        email: profile.email,
        id: profile.sub,
        image: profile.picture,
        name: profile.name ?? profile.username ?? profile.email,
        providerAccountId: profile.sub,
      };
    },
    type: 'oidc',
  } satisfies OIDCConfig<GenericOIDCProfile>,
};

Copy link

vercel bot commented Aug 31, 2024

@cy948 is attempting to deploy a commit to the LobeHub Pro Team on Vercel.

A member of the Team first needs to authorize it.

@lobehubbot
Copy link
Member

👍 @cy948

Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
非常感谢您提出拉取请求并为我们的社区做出贡献,请确保您已经遵循了我们的贡献指南,我们会尽快审查它。
如果您遇到任何问题,请随时与我们联系。

Copy link

codecov bot commented Aug 31, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.90%. Comparing base (1013652) to head (d300101).
Report is 13 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3701      +/-   ##
==========================================
+ Coverage   91.88%   91.90%   +0.02%     
==========================================
  Files         460      460              
  Lines       30792    30887      +95     
  Branches     2965     1999     -966     
==========================================
+ Hits        28293    28388      +95     
  Misses       2499     2499              
Flag Coverage Δ
app 91.90% <100.00%> (+0.02%) ⬆️
server 97.36% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@arvinxx arvinxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是以 Auth0 为试点还是怎么说?

package.json Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
@cy948
Copy link
Contributor Author

cy948 commented Aug 31, 2024

@arvinxx 目前以 auth0 做一个示例,展示一下这个工作大概流程: 添加兼容旧环境变量的代码、添加废弃的warning、等迁移期过了之后移除标记的代码这样。

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@arvinxx is currently using auth0 as an example to show the general workflow: add code that is compatible with old environment variables, add obsolete warnings, and remove the marked code after the migration period.

@arvinxx
Copy link
Contributor

arvinxx commented Aug 31, 2024

@cy948 如果要包含示例的话,是不是应该还需要加文档?

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@cy948 If you want to include examples, should you also need to add documentation?

@cy948
Copy link
Contributor Author

cy948 commented Aug 31, 2024

@cy948 如果要包含示例的话,是不是应该还需要加文档?

是的,应该添加 sso provider 的接入文档

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@cy948 If you want to include examples, should you also need to add documentation?

Yes, the access document of the sso provider should be added

@cy948 cy948 force-pushed the refactor/nextauth-envs branch from f19e091 to 2d7b44a Compare September 1, 2024 08:20
@cy948
Copy link
Contributor Author

cy948 commented Sep 1, 2024

环境变量文档的修改就拜托天才文档man @zhuozhiyongde 完成,这里是一些可能有帮助的信息:

  • 对齐后,每个 provider 都会与官方文档中一致,如Auth0的变量会与官方Auth0文档中保持一致;若没有官方文档的自定义变量则需要查看代码 src/libs/next-auth/sso-providers/[provider].ts
  • 也许新的 NextAuth 文档可以有分部分的指引,先指引用户完成必填的变量,再指引用户根据自身需求选择 sso provider 并补充变量;

@cy948 cy948 marked this pull request as ready for review September 2, 2024 12:47
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Sep 2, 2024
@cy948 cy948 changed the title 🚧 Wip: Support Environment Variable Inference For NextAuth ♻️ Refactor: Support Environment Variable Inference For NextAuth Sep 2, 2024
@cy948 cy948 force-pushed the refactor/nextauth-envs branch from 3448d15 to f6904a0 Compare September 2, 2024 15:30
Copy link
Contributor

@arvinxx arvinxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我记得还有一个 NEXTAUTH_URL,这个改了么

src/config/auth.ts Outdated Show resolved Hide resolved
src/config/auth.ts Outdated Show resolved Hide resolved
@cy948
Copy link
Contributor Author

cy948 commented Sep 3, 2024

我记得还有一个 NEXTAUTH_URL,这个改了么

这个变量是 authjs 内置的,我们没传入参数,所以不用改

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I remember there is another NEXTAUTH_URL, has this been changed?

This variable is built-in to authjs. We did not pass in parameters, so there is no need to change it.

@arvinxx
Copy link
Contributor

arvinxx commented Sep 3, 2024

我记得还有一个 NEXTAUTH_URL,这个改了么

我想做成默认拼一个 APP_URL + /api/auth 的版本,然后如果有 NEXTAUTH_URL 的话再优先用这个。这样很多 docker 部署场景这个 NEXTAUTH_URL 就不需要做成必选项了。你觉得呢

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I remember there is another NEXTAUTH_URL, has this been changed?

I want to make a version of APP_URL + /api/auth by default, and then use this first if there is NEXTAUTH_URL. In this way, NEXTAUTH_URL does not need to be made a required option in many docker deployment scenarios. What do you think?

@cy948
Copy link
Contributor Author

cy948 commented Sep 3, 2024

我想做成默认拼一个 APP_URL + /api/auth 的版本,然后如果有 NEXTAUTH_URL 的话再优先用这个。这样很多 docker 部署场景这个 NEXTAUTH_URL 就不需要做成必选项了。你觉得呢

可以,我马上做

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I want to make a version that combines APP_URL + /api/auth by default, and then use this first if there is NEXTAUTH_URL. In this way, NEXTAUTH_URL does not need to be made a required option in many docker deployment scenarios. What do you think?

Okay, I'll do it right away

@cy948
Copy link
Contributor Author

cy948 commented Sep 3, 2024

我想做成默认拼一个 APP_URL + /api/auth 的版本,然后如果有 NEXTAUTH_URL 的话再优先用这个。这样很多 docker 部署场景这个 NEXTAUTH_URL 就不需要做成必选项了。你觉得呢

改了一版 bdf43bd

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


I want to make a version that combines APP_URL + /api/auth by default, and then use this first if there is NEXTAUTH_URL. In this way, NEXTAUTH_URL does not need to be made a required option in many docker deployment scenarios. What do you think?

Changed version bdf43bd

@arvinxx
Copy link
Contributor

arvinxx commented Sep 6, 2024

@cy948 补一下单测?主要验证下是否会有 warning。补好我感觉就可以合了

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@cy948 Can you make up for the single test? Once it's patched up, I feel like it'll fit.

@cy948
Copy link
Contributor Author

cy948 commented Sep 7, 2024

@arvinxx3894c77 中补充单测了

Copy link

vercel bot commented Sep 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lobe-chat-database ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 11, 2024 2:01pm
lobe-chat-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 11, 2024 2:01pm

@cy948 cy948 force-pushed the refactor/nextauth-envs branch from d24eb1b to d300101 Compare September 11, 2024 13:18
@cy948 cy948 marked this pull request as ready for review September 11, 2024 13:19
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Sep 11, 2024
@cy948
Copy link
Contributor Author

cy948 commented Sep 11, 2024

@arvinxx rebase完了,大佬再check一下?

@lobehubbot
Copy link
Member

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


@arvinxx The rebase is finished, please check again?

Copy link
Contributor

@arvinxx arvinxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我合了,环境变量的文档还得再改一下

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Sep 11, 2024
@arvinxx arvinxx merged commit b956755 into lobehub:main Sep 12, 2024
8 checks passed
@lobehubbot
Copy link
Member

❤️ Great PR @cy948 ❤️

The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world.
项目的成长离不开用户反馈和贡献,感谢您的贡献! 如果您对 LobeHub 开发者社区感兴趣,请加入我们的 discord,然后私信 @arvinxx@canisminor1990。他们会邀请您加入我们的私密开发者频道。我们将会讨论关于 Lobe Chat 的开发,分享和讨论全球范围内的 AI 消息。

github-actions bot pushed a commit that referenced this pull request Sep 12, 2024
### [Version&nbsp;1.16.10](v1.16.9...v1.16.10)
<sup>Released on **2024-09-12**</sup>

#### ♻ Code Refactoring

- **misc**: Support Environment Variable Inference For NextAuth.

#### 🐛 Bug Fixes

- **misc**: Qwen model param error.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### Code refactoring

* **misc**: Support Environment Variable Inference For NextAuth, closes [#3701](#3701) ([b956755](b956755))

#### What's fixed

* **misc**: Qwen model param error, closes [#3902](#3902) ([c9f00e5](c9f00e5))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@lobehubbot
Copy link
Member

🎉 This PR is included in version 1.16.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

@cy948 cy948 deleted the refactor/nextauth-envs branch September 12, 2024 14:29
github-actions bot pushed a commit to bentwnghk/lobe-chat that referenced this pull request Sep 13, 2024
## [Version&nbsp;1.60.0](v1.59.4...v1.60.0)
<sup>Released on **2024-09-13**</sup>

#### ♻ Code Refactoring

- **misc**: Support Environment Variable Inference For NextAuth.

#### ✨ Features

- **misc**: Support openai new OpenAI o1-preview/o1-mini models.

#### 🐛 Bug Fixes

- **misc**: Qwen model param error, support webhooks for logto.

#### 💄 Styles

- **model**: Remove `OpenAI` deprecated model.
- **misc**: Default disable mistral provider useless models, Remove brackets from model names with dates in OpenAI, Support Google Model List, Update siliconcloud model.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### Code refactoring

* **misc**: Support Environment Variable Inference For NextAuth, closes [lobehub#3701](https://github.com/bentwnghk/lobe-chat/issues/3701) ([b956755](b956755))

#### What's improved

* **misc**: Support openai new OpenAI o1-preview/o1-mini models, closes [lobehub#3943](https://github.com/bentwnghk/lobe-chat/issues/3943) ([61bfeb2](61bfeb2))

#### What's fixed

* **misc**: Qwen model param error, closes [lobehub#3902](https://github.com/bentwnghk/lobe-chat/issues/3902) ([c9f00e5](c9f00e5))
* **misc**: Support webhooks for logto, closes [lobehub#3774](https://github.com/bentwnghk/lobe-chat/issues/3774) ([0cfee6b](0cfee6b))

#### Styles

* **model**: Remove `OpenAI` deprecated model, closes [lobehub#3465](https://github.com/bentwnghk/lobe-chat/issues/3465) ([68a4fb2](68a4fb2))
* **misc**: Default disable mistral provider useless models, closes [lobehub#3922](https://github.com/bentwnghk/lobe-chat/issues/3922) ([bdbc647](bdbc647))
* **misc**: Remove brackets from model names with dates in OpenAI, closes [lobehub#3927](https://github.com/bentwnghk/lobe-chat/issues/3927) ([2a937bc](2a937bc))
* **misc**: Support Google Model List, closes [lobehub#3938](https://github.com/bentwnghk/lobe-chat/issues/3938) ([be4efc7](be4efc7))
* **misc**: Update siliconcloud model, closes [lobehub#3935](https://github.com/bentwnghk/lobe-chat/issues/3935) ([882e981](882e981))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
cy948 added a commit to cy948/lobe-chat that referenced this pull request Sep 21, 2024
…ehub#3701)

* ♻️ refactor: `AUTH_SECRET`&`AUTH_TRUST_HOST`

* ⬆️ chore: update nextauth & @auth/core version

* ♻️ refactor: env infer for `auth0`

* ⬆️ chore: always use latest `next-auth` & `@auth/core`

* ♻️ refactor: align `authelia`

* ♻️ refactor: align `authentik`

* ♻️ align `github`

* ♻️ refactor: align `azure_ad`

* ♻️ refactor: align `cloudflare zero trust`

* ♻️ refactor: align `generic-oidc`

* ♻️ refactor: align `logto`

* ♻️ refactor: align `zitadel`

* ♻️ refactor: add deprecate tips

* ♻️ refactor: add warning for `azure_ad`

* 💄 style: reformat codes

* 🐛 fix: azure warning

* 🐛 fix: warning for cloudfalre zero turst

* 🐛 fix: warning for generic oidc

* ⏪ revert: revert changes to `NEXT_AUTH_SECRET`

* ♻️ refactor: add redirectProxy url

* ⏪ revert: unmodify ENABLE_NEXT_AUTH

* 🧪 test: should show env warning
cy948 pushed a commit to cy948/lobe-chat that referenced this pull request Sep 21, 2024
### [Version&nbsp;1.16.10](lobehub/lobe-chat@v1.16.9...v1.16.10)
<sup>Released on **2024-09-12**</sup>

#### ♻ Code Refactoring

- **misc**: Support Environment Variable Inference For NextAuth.

#### 🐛 Bug Fixes

- **misc**: Qwen model param error.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### Code refactoring

* **misc**: Support Environment Variable Inference For NextAuth, closes [lobehub#3701](lobehub#3701) ([b956755](lobehub@b956755))

#### What's fixed

* **misc**: Qwen model param error, closes [lobehub#3902](lobehub#3902) ([c9f00e5](lobehub@c9f00e5))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer released size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants