Skip to content

Commit 2141ae7

Browse files
authored
✨ feat: Supports Authelia login (lobehub#3589)
* feat(next-auth): support Authelia. * doc(authelia): Added configuration documentation for Authelia authentication. * fix: typo and update doc. * feat(next-auth): mapping profile for Authelia provider. * doc: Add Authelia-related content to Auth environment variables documentation.
1 parent b400fc0 commit 2141ae7

File tree

7 files changed

+246
-1
lines changed

7 files changed

+246
-1
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: Configuring Authelia Authentication Service for LobeChat
3+
description: >-
4+
Learn how to configure Authelia authentication service in LobeChat, including
5+
creating a provider, configuring environment variables, and deploying
6+
LobeChat. Detailed steps and necessary environment variable settings.
7+
tags:
8+
- Authelia Configuration
9+
- Single Sign-On (SSO)
10+
- LobeChat Authentication
11+
- Environment Variables
12+
- Deployment Instructions
13+
---
14+
15+
## Configuring Authelia Authentication Service
16+
17+
## Authelia Configuration Flow
18+
19+
<Steps>
20+
### Create an Authelia Identity Provider
21+
22+
We assume you are already familiar with using Authelia. Let's say your LobeChat instance is deployed at https://lobe.example.com/.
23+
Note that currently only localhost supports HTTP access; other domains need to enable TLS, otherwise Authelia will actively interrupt authentication by default.
24+
25+
Now, let's open and edit the configuration file of your Authelia instance:
26+
27+
Add a new lobe-chat item under identity_providers -> oidc:
28+
29+
```yaml
30+
identity_providers:
31+
oidc:
32+
...
33+
## The other portions of the mandatory OpenID Connect 1.0 configuration go here.
34+
## See: https://www.authelia.com/c/oidc
35+
- id: lobe-chat
36+
description: LobeChat
37+
secret: '$pbkdf2-sha512$310000$c8p78n7pUMln0jzvd4aK4Q$JNRBzwAo0ek5qKn50cFzzvE9RXV88h1wJn5KGiHrD0YKtZaR/nCb2CJPOsKaPK0hjf.9yHxzQGZziziccp6Yng' # The digest of 'insecure_secret'.
38+
public: false
39+
authorization_policy: two_factor
40+
redirect_uris:
41+
- https://chat.example.com/api/auth/callback/authelia
42+
scopes:
43+
- openid
44+
- profile
45+
- email
46+
userinfo_signing_algorithm: none
47+
```
48+
49+
Make sure to replace secret and redirect_urls with your own values.
50+
Note! The secret configured in Authelia is ciphertext, i.e., a salted hash value. Its corresponding plaintext needs to be filled in LobeChat later.
51+
52+
Save the configuration file and restart the Authelia service. Now we have completed the Authelia configuration.
53+
54+
### Configure Environment Variables
55+
56+
When deploying LobeChat, you need to configure the following environment variables:
57+
58+
| Environment Variable | Type | Description |
59+
| --- | --- | --- |
60+
| `NEXT_AUTH_SECRET` | Required | The secret used to encrypt Auth.js session tokens. You can generate a secret using the following command: `openssl rand -base64 32` |
61+
| `NEXT_AUTH_SSO_PROVIDERS` | Required | Select the SSO provider for LoboChat. Use `authentik` for Authentik. |
62+
| `AUTHELIA_CLIENT_ID` | Required | The id just configured in Authelia, example value is lobe-chat |
63+
| `AUTHELIA_CLIENT_SECRET` | The plaintext corresponding to the secret just configured in Authelia, example value is insecure_secret |
64+
| `AUTHELIA_ISSUER` | Required | Your Authelia URL, for example https://sso.example.com |
65+
| `NEXTAUTH_URL` | Optional | This URL is used to specify the callback address for Auth.js when performing OAuth verification. It only needs to be set when the default generated redirect address is incorrect. https://chat.example.com/api/auth |
66+
67+
<Callout type={'tip'}>
68+
Go to [📘 Environment Variables](/docs/self-hosting/environment-variable#Authelia) for details about the variables.
69+
</Callout>
70+
</Steps>
71+
72+
<Callout type={'info'}>
73+
After a successful deployment, users will be able to use LobeChat by authenticating with the users
74+
configured in Authelia.
75+
</Callout>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
title: 在 LobeChat 中配置 Authelia 身份验证服务
3+
description: 学习如何在 LobeChat 中配置 Authelia 身份验证服务,包括创建提供程序、配置环境变量和部署 LobeChat。详细步骤和必要环境变量设置。
4+
tags:
5+
- Authelia
6+
- 身份验证
7+
- 单点登录
8+
- 环境变量
9+
- LobeChat
10+
---
11+
12+
# 配置 Authelia 身份验证服务
13+
14+
## Authelia 配置流程
15+
16+
<Steps>
17+
### 创建 Authelia 提供应用
18+
19+
我们现在默认您已经了解了如何使用 Authelia。假设您的 LobeChat 实例部署在 `https://lobe.example.com/` 中。
20+
注意,目前只有 `localhost` 支持 HTTP 访问,其他域名需要启用 TLS,否则 Authelia 默认将主动中断身份认证。
21+
22+
现在,我们打开 Authelia 实例的配置文件进行编辑:
23+
24+
`identity_providers`-> `oidc` 下新增一个 `lobe-chat` 的项目:
25+
26+
```yaml
27+
...
28+
identity_providers:
29+
oidc:
30+
...
31+
## The other portions of the mandatory OpenID Connect 1.0 configuration go here.
32+
## See: https://www.authelia.com/c/oidc
33+
- id: lobe-chat
34+
description: LobeChat
35+
secret: '$pbkdf2-sha512$310000$c8p78n7pUMln0jzvd4aK4Q$JNRBzwAo0ek5qKn50cFzzvE9RXV88h1wJn5KGiHrD0YKtZaR/nCb2CJPOsKaPK0hjf.9yHxzQGZziziccp6Yng' # The digest of 'insecure_secret'.
36+
public: false
37+
authorization_policy: two_factor
38+
redirect_uris:
39+
- https://chat.example.com/api/auth/callback/authelia
40+
scopes:
41+
- openid
42+
- profile
43+
- email
44+
userinfo_signing_algorithm: none
45+
```
46+
47+
请您确保 `secret` 和 `redirect_urls` 替换成您自己的值。
48+
注意!Authelia 中配置 `secret` 是密文,即加盐哈希值。其对应的明文稍后需要填写在 lobeChat 中。
49+
50+
保存配置文件,然后重启 Authelia 服务。现在我们完成了 Authelia 的配置工作。
51+
52+
### 配置环境变量
53+
54+
在部署 LobeChat 时,你需要配置以下环境变量:
55+
56+
| 环境变量 | 类型 | 描述 |
57+
| --- | --- | --- |
58+
| `NEXT_AUTH_SECRET` | 必选 | 用于加密 Auth.js 会话令牌的密钥。您可以使用以下命令生成秘钥: `openssl rand -base64 32` |
59+
| `NEXT_AUTH_SSO_PROVIDERS` | 必选 | 选择 LoboChat 的单点登录提供商。使用 Authelia 请填写 `authelia`。 |
60+
| `AUTHELIA_CLIENT_ID` | 必选 | 刚刚在 Authelia 配置的 `id`,示例值是 `lobe-chat` |
61+
| `AUTHELIA_CLIENT_SECRET` | 必选 | 刚刚在 Authelia 配置的 `secret` 对应的明文,示例值是 `insecure_secret` |
62+
| `AUTHELIA_ISSUER` | 必选 |您的 Authelia 的网址,例如 `https://sso.example.com` |
63+
| `NEXTAUTH_URL` | 可选 | 该 URL 用于指定 Auth.js 在执行 OAuth 验证时的回调地址,当默认生成的重定向地址发生不正确时才需要设置。`https://chat.example.com/api/auth` |
64+
65+
<Callout type={'tip'}>
66+
前往 [📘 环境变量](/docs/self-hosting/environment-variable#Authelia) 可查阅相关变量详情。
67+
68+
</Callout>
69+
</Steps>
70+
71+
<Callout type={'info'}>
72+
部署成功后,用户将可以使用 Authelia 中配置的用户通过身份认证并使用 LobeChat。
73+
</Callout>

docs/self-hosting/environment-variables/auth.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ LobeChat provides a complete authentication service capability when deployed. Th
109109
- Default: `-`
110110
- Example: `https://your-authentik-domain.com/application/o/slug/`
111111

112+
### Authelia
113+
114+
#### `AUTHELIA_CLIENT_ID`
115+
116+
- Type: Required
117+
- Description: Client ID of the Authelia provider application. You can access it [here][auth0-client-page] and navigate to the application settings to view.
118+
- Default: `-`
119+
- Example: `lobe-chat`
120+
121+
#### `AUTHELIA_CLIENT_SECRET`
122+
123+
- Type: Required
124+
- Description: The plaintext of the Client Secret for the Authelia provider
125+
- Default: `-`
126+
- Example: `insecure_secret`
127+
128+
#### `AUTHELIA_ISSUER`
129+
130+
- Type: Required
131+
- Description: Issuer of the Authelia provider application.
132+
- Default: `-`
133+
- Example: `https://sso.example.com`
134+
112135
### Github
113136

114137
#### `GITHUB_CLIENT_ID`

docs/self-hosting/environment-variables/auth.zh-CN.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ LobeChat 在部署时提供了完善的身份验证服务能力,以下是相
107107
- 默认值: `-`
108108
- 示例: `https://your-authentik-domain.com/application/o/slug/`
109109

110+
### Authelia
111+
112+
#### `AUTHELIA_CLIENT_ID`
113+
114+
- 类型:必选
115+
- 描述: Authelia 提供程序的 Client ID
116+
- 默认值: `-`
117+
- 示例: `lobe-chat`
118+
119+
#### `AUTHELIA_CLIENT_SECRET`
120+
121+
- 类型:必选
122+
- 描述: Authelia 提供程序的 Client Secret 的明文
123+
- 默认值: `-`
124+
- 示例: `insecure_secret`
125+
126+
#### `AUTHELIA_ISSUER`
127+
128+
- 类型:必选
129+
- 描述: Authentik 提供程序的 OpenID Connect 颁发者
130+
- 默认值: `-`
131+
- 示例: `https://sso.example.com`
132+
110133
### Github
111134

112135
#### `GITHUB_CLIENT_ID`

src/config/auth.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export const getAuthConfig = () => {
8181
AUTHENTIK_CLIENT_SECRET: z.string().optional(),
8282
AUTHENTIK_ISSUER: z.string().optional(),
8383

84+
// AUTHELIA
85+
AUTHELIA_CLIENT_ID: z.string().optional(),
86+
AUTHELIA_CLIENT_SECRET: z.string().optional(),
87+
AUTHELIA_ISSUER: z.string().optional(),
88+
8489
// ZITADEL
8590
ZITADEL_CLIENT_ID: z.string().optional(),
8691
ZITADEL_CLIENT_SECRET: z.string().optional(),
@@ -118,6 +123,11 @@ export const getAuthConfig = () => {
118123
AUTHENTIK_CLIENT_SECRET: process.env.AUTHENTIK_CLIENT_SECRET,
119124
AUTHENTIK_ISSUER: process.env.AUTHENTIK_ISSUER,
120125

126+
// AUTHELIA
127+
AUTHELIA_CLIENT_ID: process.env.AUTHELIA_CLIENT_ID,
128+
AUTHELIA_CLIENT_SECRET: process.env.AUTHELIA_CLIENT_SECRET,
129+
AUTHELIA_ISSUER: process.env.AUTHELIA_ISSUER,
130+
121131
// ZITADEL
122132
ZITADEL_CLIENT_ID: process.env.ZITADEL_CLIENT_ID,
123133
ZITADEL_CLIENT_SECRET: process.env.ZITADEL_CLIENT_SECRET,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type { OIDCConfig } from '@auth/core/providers';
2+
3+
import { authEnv } from '@/config/auth';
4+
5+
import { CommonProviderConfig } from './sso.config';
6+
7+
export type AutheliaProfile = {
8+
// The users display name
9+
email: string;
10+
// The users email
11+
groups: string[];
12+
// The username the user used to login with
13+
name: string;
14+
preferred_username: string; // The users groups
15+
sub: string; // The users id
16+
};
17+
18+
const provider = {
19+
id: 'authelia',
20+
provider: {
21+
...CommonProviderConfig,
22+
authorization: { params: { scope: 'openid email profile' } },
23+
checks: ['state', 'pkce'],
24+
clientId: authEnv.AUTHELIA_CLIENT_ID,
25+
clientSecret: authEnv.AUTHELIA_CLIENT_SECRET,
26+
id: 'authelia',
27+
issuer: authEnv.AUTHELIA_ISSUER,
28+
name: 'Authelia',
29+
profile(profile) {
30+
return {
31+
email: profile.email,
32+
name: profile.name,
33+
providerAccountId: profile.sub,
34+
};
35+
},
36+
type: 'oidc',
37+
} satisfies OIDCConfig<AutheliaProfile>,
38+
};
39+
40+
export default provider;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Auth0 from './auth0';
2+
import Authelia from './authelia';
23
import Authentik from './authentik';
34
import AzureAD from './azure-ad';
45
import Github from './github';
56
import Zitadel from './zitadel';
67

7-
export const ssoProviders = [Auth0, Authentik, AzureAD, Github, Zitadel];
8+
export const ssoProviders = [Auth0, Authentik, AzureAD, Github, Zitadel, Authelia];

0 commit comments

Comments
 (0)