-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
127 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
services: | ||
traefik: | ||
image: traefik:latest | ||
command: | ||
- --log.level=INFO | ||
- --accesslog=true | ||
- --api.dashboard=true | ||
- --providers.docker=true | ||
- --entrypoints.web.address=:80 | ||
ports: | ||
- "8080:80/tcp" | ||
volumes: | ||
- /var/run/docker.sock:/var/run/docker.sock | ||
labels: | ||
- traefik.http.routers.traefik.rule=Host(`traefik.localhost`) | ||
- traefik.http.routers.traefik.service=api@internal | ||
|
||
nanaoidc: | ||
image: node:lts-alpine | ||
command: sleep infinity | ||
working_dir: /app | ||
volumes: | ||
- .:/app | ||
labels: | ||
- traefik.http.routers.nanaoidc.rule=Host(`nanaoidc.localhost`) || Path(`/_oauth`) | ||
- traefik.http.services.nanaoidc.loadbalancer.server.port=3000 | ||
- traefik.http.middlewares.nanaoidc.forwardAuth.address=http://nanaoidc:3000/traefik | ||
- traefik.http.middlewares.nanaoidc.forwardAuth.trustForwardHeader=true | ||
|
||
whoami: | ||
image: traefik/whoami:latest | ||
labels: | ||
- traefik.http.routers.whoami.rule=Host(`whoami.localhost`) | ||
- traefik.http.routers.whoami.middlewares=nanaoidc | ||
- traefik.http.services.whoami.loadbalancer.server.port=80 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import assert from "node:assert/strict"; | ||
|
||
export default eventHandler(async (event) => { | ||
const session = await useTypedSession(event); | ||
|
||
const query = getQuery(event); | ||
const { code } = query; | ||
assert(typeof code === "string"); | ||
await session.update({ code }); | ||
|
||
const session = await useTypedSession(event); | ||
const redirect = session.data.redirect || "/"; | ||
return sendRedirect(event, redirect); | ||
const params = new URLSearchParams({ code }); | ||
return sendRedirect(event, `${redirect}?${params}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import assert from "node:assert/strict"; | ||
|
||
export default eventHandler(async (event) => { | ||
const session = await useTypedSession(event); | ||
const query = getQuery(event); | ||
const { code, forwardAuthRedirect } = query; | ||
if (forwardAuthRedirect) { | ||
assert(typeof forwardAuthRedirect === "string"); | ||
await session.update({ redirect: ".", forwardAuthRedirect }); | ||
return sendRedirect(event, `${userConfig.publicUrl}/api/discord/auth`); | ||
} else if (code) { | ||
assert(typeof code === "string"); | ||
const resp = await exchangeCode(code); | ||
const { member } = await fetchUserinfo(resp.access_token); | ||
const groups = [userConfig.baseGroup]; | ||
for (const role of member.roles) { | ||
const mapped = userConfig.discord.roles[role]; | ||
if (mapped) { | ||
groups.push(mapped); | ||
} | ||
} | ||
await session.update({ forwardAuthGroups: groups }); | ||
return sendRedirect(event, session.data.forwardAuthRedirect); | ||
} else { | ||
throw createError({ status: 400, message: "Missing required query" }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export default eventHandler(async (event) => { | ||
const headers = event.headers; | ||
const proto = headers.get("x-forwarded-proto"); | ||
const host = headers.get("x-forwarded-host"); | ||
const uri = headers.get("x-forwarded-uri"); | ||
|
||
const session = await useTypedSession(event); | ||
const groups = session.data.forwardAuthGroups; | ||
|
||
if (groups !== undefined) { | ||
const required = userConfig.forwardAuth.hosts[host]; | ||
if (!required || required.some((req) => groups.includes(req))) { | ||
return; | ||
} else { | ||
await session.clear(); | ||
throw createError({ status: 401, message: "Missing required group" }); | ||
} | ||
} else { | ||
const forwardAuthRedirect = `${proto}://${host}${uri}`; | ||
const params = new URLSearchParams({ forwardAuthRedirect }); | ||
return sendRedirect(event, `http://${host}/_oauth?${params}`); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { type H3Event } from "h3"; | ||
|
||
interface SessionData { | ||
code: string; | ||
redirect: string; | ||
forwardAuthRedirect: string; | ||
forwardAuthGroups: string[]; | ||
} | ||
|
||
export const useTypedSession = (event: H3Event) => | ||
useSession<SessionData>(event, { password: userConfig.sessionPassword }); | ||
useSession<SessionData>(event, { | ||
password: userConfig.sessionPassword, | ||
maxAge: 60 * 60 * 24, | ||
}); |