-
-
Notifications
You must be signed in to change notification settings - Fork 286
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
feat: support for forwardAuth #580
base: develop
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ info: | |
|
||
- **Cookie Authentication**: A valid sign-in to the `/auth/plex` or `/auth/local` will generate a valid authentication cookie. | ||
- **API Key Authentication**: Sign-in is also possible by passing an `X-Api-Key` header along with a valid API Key generated by Overseerr. | ||
- **ForwardAuth Authentication**: Sign-in is also possible by passing an `X-Forwarded-User` header containing user's e-mail. | ||
tags: | ||
- name: public | ||
description: Public API endpoints requiring no authentication. | ||
|
@@ -186,6 +187,9 @@ components: | |
defaultPermissions: | ||
type: number | ||
example: 32 | ||
enableForwardAuth: | ||
type: boolean | ||
example: true | ||
PlexLibrary: | ||
type: object | ||
properties: | ||
|
@@ -1939,6 +1943,10 @@ components: | |
type: apiKey | ||
in: header | ||
name: X-Api-Key | ||
forwardAuth: | ||
type: apiKey | ||
in: header | ||
name: X-Forwarded-User | ||
|
||
paths: | ||
/status: | ||
|
@@ -6939,3 +6947,4 @@ paths: | |
security: | ||
- cookieAuth: [] | ||
- apiKey: [] | ||
- forwardAuth: [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not in the OpenAPI spec? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,19 @@ export const checkUser: Middleware = async (req, _res, next) => { | |
} | ||
|
||
user = await userRepository.findOne({ where: { id: userId } }); | ||
} else if (req.header('x-forwarded-user')) { | ||
const userRepository = getRepository(User); | ||
user = await userRepository.findOne({ | ||
where: { email: req.header('x-forwarded-user') }, | ||
}); | ||
|
||
if (user) { | ||
req.user = user; | ||
} | ||
|
||
req.locale = user?.settings?.locale | ||
? user.settings.locale | ||
: settings.main.locale; | ||
Comment on lines
+30
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not necessary, it's a duplicate from below. |
||
} else if (req.session?.userId) { | ||
const userRepository = getRepository(User); | ||
|
||
|
@@ -44,7 +57,18 @@ export const isAuthenticated = ( | |
permissions?: Permission | Permission[], | ||
options?: PermissionCheckOptions | ||
): Middleware => { | ||
const authMiddleware: Middleware = (req, res, next) => { | ||
const authMiddleware: Middleware = async (req, res, next) => { | ||
if (req.header('x-forwarded-user')) { | ||
const userRepository = getRepository(User); | ||
const user = await userRepository.findOne({ | ||
where: { email: req.header('x-forwarded-user') }, | ||
}); | ||
|
||
if (user) { | ||
req.user = user; | ||
} | ||
} | ||
|
||
if (!req.user || !req.user.hasPermission(permissions ?? 0, options)) { | ||
res.status(403).json({ | ||
status: 403, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wdyt about renaming this file to something like |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { NextPageContext } from 'next/dist/shared/lib/utils'; | ||
import type { GetServerSidePropsContext, PreviewData } from 'next/types'; | ||
import type { ParsedUrlQuery } from 'querystring'; | ||
|
||
export const getRequestHeaders = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this function be called something like |
||
ctx: NextPageContext | GetServerSidePropsContext<ParsedUrlQuery, PreviewData> | ||
) => { | ||
return ctx.req && ctx.req.headers | ||
? { | ||
...(ctx.req.headers.cookie && { | ||
cookie: ctx.req.headers.cookie, | ||
}), | ||
...(ctx.req.headers['x-forwarded-user'] && { | ||
'x-forwarded-user': ctx.req.headers['x-forwarded-user'] as string, | ||
}), | ||
} | ||
: undefined; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You wrote the setting here, but it is not available anywhere.
You should add this as a general setting, by adding a new properties in the
MainSettings
type inserver/lib/settings.ts
and adding a new input in the settings page insrc/components/Settings/SettingsMain/index.tsx
. You should also check that this setting is enabled before allowing login with forwardAuth.Or did you intend to not make a setting and set it enabled by default?