Skip to content

Commit 3195778

Browse files
authored
feat: support custom auth proxy header name (#472)
Signed-off-by: BobDu <[email protected]>
1 parent 1fa33bb commit 3195778

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

README.en.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ Set env `AUTH_PROXY_ENABLED=true` can enable auth proxy mode.
363363

364364
After activating this feature, it is necessary to ensure that chatgpt-web can only be accessed through a reverse proxy.
365365

366-
Authentication is carried out by the reverse proxy, which then forwards the request with the `X-Email` header to identify the user identity.
366+
Authentication is carried out by the reverse proxy, which then forwards the request with the header to identify the user identity.
367+
Default header name is `X-Email`, can custom config use set env `AUTH_PROXY_HEADER_NAME`.
367368

368369
Recommended for current IdP to use LDAP protocol, using [authelia](https://www.authelia.com)
369370

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ pnpm build
360360

361361
在开启该功能后 需确保 chatgpt-web 只能通过反向代理访问
362362

363-
由反向代理进行进行身份验证 并再转发请求时携带请求头`X-Email`标识用户身份
363+
由反向代理进行进行身份验证 并再转发请求时携带请求头标识用户身份
364+
默认请求头为 `X-Email` 并可以通过设置环境变量 `AUTH_PROXY_HEADER_NAME` 自定义配置
364365

365366
推荐当前 Idp 使用 LDAP 协议的 可以选择使用 [authelia](https://www.authelia.com)
366367

service/src/middleware/auth.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import jwt from 'jsonwebtoken'
22
import type { Request } from 'express'
3-
import { getCacheConfig } from '../storage/config'
3+
import { authProxyHeaderName, getCacheConfig } from '../storage/config'
44
import { createUser, getUser, getUserById } from '../storage/mongo'
55
import { Status, UserRole } from '../storage/model'
66
import type { AuthJwtPayload } from '../types'
@@ -10,17 +10,17 @@ async function auth(req, res, next) {
1010

1111
if (config.siteConfig.authProxyEnabled) {
1212
try {
13-
const username = req.header('X-Email')
13+
const username = req.header(authProxyHeaderName)
1414
if (!username) {
15-
res.send({ status: 'Unauthorized', message: 'Please config auth proxy (usually is nginx) add set proxy header X-Email.', data: null })
15+
res.send({ status: 'Unauthorized', message: `Please config auth proxy (usually is nginx) add set proxy header ${authProxyHeaderName}.`, data: null })
1616
return
1717
}
1818
const user = await getUser(username)
1919
req.headers.userId = user._id.toString()
2020
next()
2121
}
2222
catch (error) {
23-
res.send({ status: 'Unauthorized', message: error.message ?? 'Please config auth proxy (usually is nginx) add set proxy header X-Email.', data: null })
23+
res.send({ status: 'Unauthorized', message: error.message ?? `Please config auth proxy (usually is nginx) add set proxy header ${authProxyHeaderName}.`, data: null })
2424
}
2525
return
2626
}
@@ -52,7 +52,11 @@ async function getUserId(req: Request): Promise<string | undefined> {
5252
try {
5353
const config = await getCacheConfig()
5454
if (config.siteConfig.authProxyEnabled) {
55-
const username = req.header('X-Email')
55+
const username = req.header(authProxyHeaderName)
56+
if (!username) {
57+
globalThis.console.error(`Please config auth proxy (usually is nginx) add set proxy header ${authProxyHeaderName}.`)
58+
return null
59+
}
5660
let user = await getUser(username)
5761
if (user == null) {
5862
const isRoot = username.toLowerCase() === process.env.ROOT_USER

service/src/middleware/rootAuth.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import jwt from 'jsonwebtoken'
22
import * as dotenv from 'dotenv'
33
import { Status, UserRole } from '../storage/model'
44
import { getUser, getUserById } from '../storage/mongo'
5-
import { getCacheConfig } from '../storage/config'
5+
import { authProxyHeaderName, getCacheConfig } from '../storage/config'
66
import type { AuthJwtPayload } from '../types'
77

88
dotenv.config()
@@ -12,7 +12,7 @@ async function rootAuth(req, res, next) {
1212

1313
if (config.siteConfig.authProxyEnabled) {
1414
try {
15-
const username = req.header('X-Email')
15+
const username = req.header(authProxyHeaderName)
1616
const user = await getUser(username)
1717
req.headers.userId = user._id
1818
if (user == null || user.status !== Status.Normal || !user.roles.includes(UserRole.Admin))
@@ -21,7 +21,7 @@ async function rootAuth(req, res, next) {
2121
next()
2222
}
2323
catch (error) {
24-
res.send({ status: 'Unauthorized', message: error.message ?? 'Please config auth proxy (usually is nginx) add set proxy header X-Email.', data: null })
24+
res.send({ status: 'Unauthorized', message: error.message ?? `Please config auth proxy (usually is nginx) add set proxy header ${authProxyHeaderName}.`, data: null })
2525
}
2626
return
2727
}

service/src/storage/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,5 @@ export async function getApiKeys() {
180180
})
181181
return result
182182
}
183+
184+
export const authProxyHeaderName = process.env.AUTH_PROXY_HEADER_NAME ?? 'X-Email'

0 commit comments

Comments
 (0)