mirror of
https://github.com/Chewbaccalakis/rfid-database.git
synced 2026-09-10 00:11:56 -07:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
50b33ef19e |
@@ -22,3 +22,11 @@ jobs:
|
|||||||
- run: npm run build
|
- run: npm run build
|
||||||
env:
|
env:
|
||||||
AUTH_SECRET: ci-build-secret-at-least-32-characters
|
AUTH_SECRET: ci-build-secret-at-least-32-characters
|
||||||
|
|
||||||
|
docker:
|
||||||
|
name: docker-build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
- name: Build production image
|
||||||
|
run: docker build -t rfid-database:ci .
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
name: Docker
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [main]
|
|
||||||
tags: ["v*"]
|
|
||||||
pull_request:
|
|
||||||
branches: [main]
|
|
||||||
|
|
||||||
env:
|
|
||||||
REGISTRY: ghcr.io
|
|
||||||
# GHCR requires lowercase image names
|
|
||||||
IMAGE_NAME: ghcr.io/chewbaccalakis/rfid-database
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build-and-push:
|
|
||||||
name: build-and-push
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
packages: write
|
|
||||||
attestations: write
|
|
||||||
id-token: write
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
|
|
||||||
- name: Log in to GitHub Container Registry
|
|
||||||
if: github.event_name != 'pull_request'
|
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: ${{ env.REGISTRY }}
|
|
||||||
username: ${{ github.actor }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Extract metadata (tags, labels)
|
|
||||||
id: meta
|
|
||||||
uses: docker/metadata-action@v5
|
|
||||||
with:
|
|
||||||
images: ${{ env.IMAGE_NAME }}
|
|
||||||
tags: |
|
|
||||||
type=raw,value=latest,enable={{is_default_branch}}
|
|
||||||
type=sha,prefix=sha-,format=short
|
|
||||||
type=ref,event=branch
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
|
|
||||||
- name: Build and push
|
|
||||||
uses: docker/build-push-action@v6
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: ./Dockerfile
|
|
||||||
push: ${{ github.event_name != 'pull_request' }}
|
|
||||||
tags: ${{ steps.meta.outputs.tags }}
|
|
||||||
labels: ${{ steps.meta.outputs.labels }}
|
|
||||||
cache-from: type=gha
|
|
||||||
cache-to: type=gha,mode=max
|
|
||||||
@@ -67,26 +67,8 @@ Open http://localhost:3000 and sign in.
|
|||||||
6. Open `/login` — you should see **Sign in with AtlasHorizon**.
|
6. Open `/login` — you should see **Sign in with AtlasHorizon**.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
- `AUTH_OIDC_ISSUER` must match discovery (`{issuer}/.well-known/openid-configuration`).
|
- `AUTH_OIDC_ISSUER` must match discovery (`{issuer}/.well-known/openid-configuration`). Your AtlasHorizon issuer at `https://auth.atlashorizon.net` is valid.
|
||||||
- The app requests `openid email profile` and loads profile from **UserInfo** (Authelia puts `email` there by default).
|
- The IdP must return an **email** claim (we request `openid email profile`).
|
||||||
- Optional Authelia hardening — also put email on the ID token:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
identity_providers:
|
|
||||||
oidc:
|
|
||||||
claims_policies:
|
|
||||||
rfiddb:
|
|
||||||
id_token:
|
|
||||||
- 'email'
|
|
||||||
- 'email_verified'
|
|
||||||
- 'preferred_username'
|
|
||||||
- 'name'
|
|
||||||
clients:
|
|
||||||
- client_id: 'rfiddb'
|
|
||||||
claims_policy: 'rfiddb'
|
|
||||||
# ...rest of client...
|
|
||||||
```
|
|
||||||
|
|
||||||
- Local password login stays available alongside SSO.
|
- Local password login stays available alongside SSO.
|
||||||
- If the button is missing, you are almost certainly on an old image — rebuild.
|
- If the button is missing, you are almost certainly on an old image — rebuild.
|
||||||
|
|
||||||
@@ -135,26 +117,10 @@ npm run typecheck
|
|||||||
npm run build
|
npm run build
|
||||||
```
|
```
|
||||||
|
|
||||||
GitHub Actions on every pull request and push to `main`:
|
GitHub Actions runs on every pull request and push to `main`:
|
||||||
|
|
||||||
1. **lint / typecheck / test / Next.js build**
|
1. **lint / typecheck / test / Next.js build**
|
||||||
2. **Docker image** — build on PRs; on `main` (and `v*` tags) push to GHCR:
|
2. **`docker build`** — catches Dockerfile parse/build failures (the class of break that conflict markers previously caused)
|
||||||
`ghcr.io/chewbaccalakis/rfid-database`
|
|
||||||
|
|
||||||
### Pull the published image
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker pull ghcr.io/chewbaccalakis/rfid-database:latest
|
|
||||||
# or a specific commit: ghcr.io/chewbaccalakis/rfid-database:sha-<shortsha>
|
|
||||||
```
|
|
||||||
|
|
||||||
If the package is private, authenticate first:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
echo $GITHUB_TOKEN | docker login ghcr.io -u USERNAME --password-stdin
|
|
||||||
```
|
|
||||||
|
|
||||||
Or in Compose, set `image: ghcr.io/chewbaccalakis/rfid-database:latest` and comment out `build:`.
|
|
||||||
|
|
||||||
## Docker
|
## Docker
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
services:
|
services:
|
||||||
rfid-database:
|
rfid-database:
|
||||||
# Local build (default):
|
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
image: rfid-database:local
|
image: rfid-database:local
|
||||||
# Or pull from GHCR instead of building:
|
|
||||||
# image: ghcr.io/chewbaccalakis/rfid-database:latest
|
|
||||||
# pull_policy: always
|
|
||||||
container_name: rfid-database
|
container_name: rfid-database
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
ports:
|
ports:
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
import { isOidcConfigured } from "@/lib/auth/oidc";
|
import { isOidcConfigured } from "@/lib/auth/oidc";
|
||||||
import { LoginPageClient } from "@/components/LoginPageClient";
|
import { LoginPageClient } from "@/components/LoginPageClient";
|
||||||
|
|
||||||
// OIDC env is only available at runtime (Docker). Never prerender this page
|
|
||||||
// at build time or the SSO button stays permanently hidden.
|
|
||||||
export const dynamic = "force-dynamic";
|
|
||||||
|
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
|
const authUrl = (process.env.AUTH_URL || "").replace(/\/$/, "");
|
||||||
|
const callbackUrlHint = authUrl
|
||||||
|
? `${authUrl}/api/auth/callback/oidc`
|
||||||
|
: "https://<your-host>/api/auth/callback/oidc";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<LoginPageClient
|
<LoginPageClient
|
||||||
oidcEnabled={isOidcConfigured()}
|
oidcEnabled={isOidcConfigured()}
|
||||||
oidcName={process.env.AUTH_OIDC_NAME || "SSO"}
|
oidcName={process.env.AUTH_OIDC_NAME || "SSO"}
|
||||||
|
callbackUrlHint={callbackUrlHint}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { FormEvent, useEffect, useState } from "react";
|
import { FormEvent, useState } from "react";
|
||||||
import { signIn } from "next-auth/react";
|
import { signIn } from "next-auth/react";
|
||||||
import { useRouter, useSearchParams } from "next/navigation";
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
import { Suspense } from "react";
|
import { Suspense } from "react";
|
||||||
@@ -8,11 +8,7 @@ import { Suspense } from "react";
|
|||||||
type Props = {
|
type Props = {
|
||||||
oidcEnabled: boolean;
|
oidcEnabled: boolean;
|
||||||
oidcName: string;
|
oidcName: string;
|
||||||
};
|
callbackUrlHint: string;
|
||||||
|
|
||||||
type AuthConfig = {
|
|
||||||
oidcEnabled: boolean;
|
|
||||||
oidcName: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function authErrorMessage(code: string | null): string | null {
|
function authErrorMessage(code: string | null): string | null {
|
||||||
@@ -34,7 +30,7 @@ function authErrorMessage(code: string | null): string | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function LoginForm({ oidcEnabled, oidcName }: Props) {
|
function LoginForm({ oidcEnabled, oidcName, callbackUrlHint }: Props) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const params = useSearchParams();
|
const params = useSearchParams();
|
||||||
const callbackUrl = params.get("callbackUrl") || "/";
|
const callbackUrl = params.get("callbackUrl") || "/";
|
||||||
@@ -44,22 +40,6 @@ function LoginForm({ oidcEnabled, oidcName }: Props) {
|
|||||||
authErrorMessage(params.get("error"))
|
authErrorMessage(params.get("error"))
|
||||||
);
|
);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [runtimeOidc, setRuntimeOidc] = useState<AuthConfig | null>(null);
|
|
||||||
|
|
||||||
// Belt-and-suspenders: ask the live API so a stale static shell can't hide SSO
|
|
||||||
useEffect(() => {
|
|
||||||
fetch("/api/v1/auth/config")
|
|
||||||
.then((r) => (r.ok ? r.json() : null))
|
|
||||||
.then((data: AuthConfig | null) => {
|
|
||||||
if (data) setRuntimeOidc(data);
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
/* ignore */
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const showOidc = runtimeOidc?.oidcEnabled ?? oidcEnabled;
|
|
||||||
const displayName = runtimeOidc?.oidcName || oidcName;
|
|
||||||
|
|
||||||
async function onSubmit(e: FormEvent) {
|
async function onSubmit(e: FormEvent) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -117,14 +97,21 @@ function LoginForm({ oidcEnabled, oidcName }: Props) {
|
|||||||
<button className="btn" type="submit" disabled={loading}>
|
<button className="btn" type="submit" disabled={loading}>
|
||||||
{loading ? "Signing in…" : "Sign in"}
|
{loading ? "Signing in…" : "Sign in"}
|
||||||
</button>
|
</button>
|
||||||
{showOidc && (
|
{oidcEnabled && (
|
||||||
<button
|
<>
|
||||||
type="button"
|
<button
|
||||||
className="btn btn-secondary"
|
type="button"
|
||||||
onClick={() => signIn("oidc", { callbackUrl })}
|
className="btn btn-secondary"
|
||||||
>
|
onClick={() => signIn("oidc", { callbackUrl })}
|
||||||
Sign in with {displayName}
|
>
|
||||||
</button>
|
Sign in with {oidcName}
|
||||||
|
</button>
|
||||||
|
<p className="muted" style={{ margin: 0, fontSize: "0.75rem" }}>
|
||||||
|
IdP redirect URI must be exactly:
|
||||||
|
<br />
|
||||||
|
<code className="mono">{callbackUrlHint}</code>
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+14
-21
@@ -6,9 +6,9 @@ import { eq } from "drizzle-orm";
|
|||||||
import { getDb } from "@/db/client";
|
import { getDb } from "@/db/client";
|
||||||
import { users } from "@/db/schema";
|
import { users } from "@/db/schema";
|
||||||
import { authConfig } from "@/lib/auth/auth.config";
|
import { authConfig } from "@/lib/auth/auth.config";
|
||||||
import { emailFromOidcProfile, isOidcConfigured } from "@/lib/auth/oidc";
|
import { isOidcConfigured } from "@/lib/auth/oidc";
|
||||||
|
|
||||||
export { emailFromOidcProfile, isOidcConfigured } from "@/lib/auth/oidc";
|
export { isOidcConfigured } from "@/lib/auth/oidc";
|
||||||
|
|
||||||
function buildProviders(): Provider[] {
|
function buildProviders(): Provider[] {
|
||||||
const providers: Provider[] = [
|
const providers: Provider[] = [
|
||||||
@@ -45,11 +45,6 @@ function buildProviders(): Provider[] {
|
|||||||
wellKnown: `${issuer}/.well-known/openid-configuration`,
|
wellKnown: `${issuer}/.well-known/openid-configuration`,
|
||||||
clientId: process.env.AUTH_OIDC_CLIENT_ID!,
|
clientId: process.env.AUTH_OIDC_CLIENT_ID!,
|
||||||
clientSecret: process.env.AUTH_OIDC_CLIENT_SECRET!,
|
clientSecret: process.env.AUTH_OIDC_CLIENT_SECRET!,
|
||||||
// Auth.js OIDC defaults to PKCE-only; Authelia requires a strong `state`
|
|
||||||
checks: ["pkce", "state"],
|
|
||||||
// Authelia (and many IdPs) put `email` on UserInfo, not the ID token.
|
|
||||||
// Auth.js OIDC otherwise only reads ID-token claims.
|
|
||||||
idToken: false,
|
|
||||||
authorization: {
|
authorization: {
|
||||||
params: {
|
params: {
|
||||||
scope: "openid email profile",
|
scope: "openid email profile",
|
||||||
@@ -61,16 +56,21 @@ function buildProviders(): Provider[] {
|
|||||||
// Link OIDC logins to existing local users by email
|
// Link OIDC logins to existing local users by email
|
||||||
allowDangerousEmailAccountLinking: true,
|
allowDangerousEmailAccountLinking: true,
|
||||||
profile(profile: Record<string, unknown>) {
|
profile(profile: Record<string, unknown>) {
|
||||||
const email = emailFromOidcProfile(profile);
|
const email =
|
||||||
|
(typeof profile.email === "string" && profile.email) ||
|
||||||
|
(typeof profile.preferred_username === "string" &&
|
||||||
|
String(profile.preferred_username).includes("@")
|
||||||
|
? String(profile.preferred_username)
|
||||||
|
: null);
|
||||||
return {
|
return {
|
||||||
id: String(profile.sub ?? email ?? crypto.randomUUID()),
|
id: String(profile.sub ?? ""),
|
||||||
name:
|
name:
|
||||||
(typeof profile.name === "string" && profile.name) ||
|
(typeof profile.name === "string" && profile.name) ||
|
||||||
(typeof profile.preferred_username === "string" &&
|
(typeof profile.preferred_username === "string" &&
|
||||||
profile.preferred_username) ||
|
profile.preferred_username) ||
|
||||||
email ||
|
email ||
|
||||||
"OIDC user",
|
"OIDC user",
|
||||||
email: email ?? undefined,
|
email,
|
||||||
image: typeof profile.picture === "string" ? profile.picture : null,
|
image: typeof profile.picture === "string" ? profile.picture : null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
@@ -85,24 +85,17 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
|||||||
providers: buildProviders(),
|
providers: buildProviders(),
|
||||||
callbacks: {
|
callbacks: {
|
||||||
...authConfig.callbacks,
|
...authConfig.callbacks,
|
||||||
async signIn({ user, account, profile }) {
|
async signIn({ user, account }) {
|
||||||
if (account?.provider === "credentials") return true;
|
if (account?.provider === "credentials") return true;
|
||||||
|
if (!user.email) {
|
||||||
const email =
|
|
||||||
user.email?.trim().toLowerCase() ||
|
|
||||||
emailFromOidcProfile((profile ?? {}) as Record<string, unknown>);
|
|
||||||
|
|
||||||
if (!email) {
|
|
||||||
console.error(
|
console.error(
|
||||||
"[auth] OIDC sign-in rejected: no email in profile. Claim keys:",
|
"[auth] OIDC sign-in rejected: IdP did not return an email claim. Enable the email scope/claim on the client."
|
||||||
profile ? Object.keys(profile) : []
|
|
||||||
);
|
);
|
||||||
return "/login?error=EmailRequired";
|
return "/login?error=EmailRequired";
|
||||||
}
|
}
|
||||||
|
|
||||||
user.email = email;
|
|
||||||
|
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
|
const email = user.email.toLowerCase();
|
||||||
let existing = db.select().from(users).where(eq(users.email, email)).get();
|
let existing = db.select().from(users).where(eq(users.email, email)).get();
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
const id = crypto.randomUUID();
|
const id = crypto.randomUUID();
|
||||||
|
|||||||
@@ -5,23 +5,3 @@ export function isOidcConfigured(): boolean {
|
|||||||
process.env.AUTH_OIDC_CLIENT_SECRET?.trim()
|
process.env.AUTH_OIDC_CLIENT_SECRET?.trim()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Pull an email out of common OIDC claim shapes (Authelia, Keycloak, etc.). */
|
|
||||||
export function emailFromOidcProfile(
|
|
||||||
profile: Record<string, unknown>
|
|
||||||
): string | null {
|
|
||||||
const candidates = [
|
|
||||||
profile.email,
|
|
||||||
profile.preferred_username,
|
|
||||||
profile.upn,
|
|
||||||
profile.mail,
|
|
||||||
(profile.user as { email?: unknown } | undefined)?.email,
|
|
||||||
];
|
|
||||||
|
|
||||||
for (const value of candidates) {
|
|
||||||
if (typeof value !== "string") continue;
|
|
||||||
const trimmed = value.trim();
|
|
||||||
if (trimmed.includes("@")) return trimmed.toLowerCase();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
|
||||||
import { emailFromOidcProfile } from "@/lib/auth/oidc";
|
|
||||||
|
|
||||||
describe("emailFromOidcProfile", () => {
|
|
||||||
it("reads email claim", () => {
|
|
||||||
expect(emailFromOidcProfile({ email: "[email protected]" })).toBe(
|
|
||||||
"[email protected]"
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("falls back to preferred_username when it looks like an email", () => {
|
|
||||||
expect(
|
|
||||||
emailFromOidcProfile({ preferred_username: "[email protected]" })
|
|
||||||
).toBe("[email protected]");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("ignores non-email preferred_username", () => {
|
|
||||||
expect(emailFromOidcProfile({ preferred_username: "nick" })).toBeNull();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns null when nothing usable is present", () => {
|
|
||||||
expect(emailFromOidcProfile({ sub: "abc", name: "Nick" })).toBeNull();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user