mirror of
https://github.com/Chewbaccalakis/rfid-database.git
synced 2026-09-10 00:11:56 -07:00
Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ac7710a23 | ||
|
|
0e50c537ee | ||
|
|
5aa5fef9af | ||
|
|
d3ed4566b7 |
@@ -0,0 +1,15 @@
|
|||||||
|
node_modules
|
||||||
|
.next
|
||||||
|
.git
|
||||||
|
data
|
||||||
|
*.db
|
||||||
|
*.db-*
|
||||||
|
.env
|
||||||
|
.env.*
|
||||||
|
!.env.example
|
||||||
|
coverage
|
||||||
|
tests
|
||||||
|
.github
|
||||||
|
.cursor
|
||||||
|
**/*.md
|
||||||
|
!README.md
|
||||||
+28
-4
@@ -1,11 +1,35 @@
|
|||||||
AUTH_SECRET=change-me-to-a-long-random-string
|
AUTH_SECRET=change-me-to-a-long-random-string
|
||||||
|
|
||||||
|
# Public URL of this app (important behind reverse proxies / Docker)
|
||||||
|
# Example: http://localhost:3000 or https://rfid.example.com
|
||||||
|
# AUTH_URL=http://localhost:3000
|
||||||
|
# AUTH_TRUST_HOST=true
|
||||||
|
|
||||||
# Optional: path to SQLite file (default ./data/rfid.db)
|
# Optional: path to SQLite file (default ./data/rfid.db)
|
||||||
# RFID_DB_PATH=./data/rfid.db
|
# RFID_DB_PATH=./data/rfid.db
|
||||||
|
|
||||||
# Optional OIDC (Authentik, Keycloak, Authelia, etc.)
|
# Docker Compose published port (default 3000)
|
||||||
|
# RFID_PORT=3000
|
||||||
|
|
||||||
|
# Optional first-boot user when using Docker entrypoint
|
||||||
|
# [email protected]
|
||||||
|
# CREATE_USER_PASSWORD=changeme
|
||||||
|
# CREATE_USER_NAME=Admin
|
||||||
|
|
||||||
|
# Optional OIDC (Authentik, Keycloak, Authelia, Google, etc.)
|
||||||
|
# Setting these three enables the "Sign in with SSO" button automatically
|
||||||
|
# (no NEXT_PUBLIC_* flag needed).
|
||||||
|
#
|
||||||
|
# In your IdP, create a confidential OIDC client with redirect URI:
|
||||||
|
# {AUTH_URL}/api/auth/callback/oidc
|
||||||
|
# e.g. http://localhost:3000/api/auth/callback/oidc
|
||||||
|
#
|
||||||
|
# AUTH_OIDC_ISSUER must be the issuer that serves
|
||||||
|
# {issuer}/.well-known/openid-configuration
|
||||||
|
# Authentik example: https://sso.example.com/application/o/rfid/
|
||||||
|
# Keycloak example: https://sso.example.com/realms/myrealm
|
||||||
|
#
|
||||||
# AUTH_OIDC_ISSUER=https://sso.example.com/application/o/rfid/
|
# AUTH_OIDC_ISSUER=https://sso.example.com/application/o/rfid/
|
||||||
# AUTH_OIDC_CLIENT_ID=
|
# AUTH_OIDC_CLIENT_ID=
|
||||||
# AUTH_OIDC_CLIENT_SECRET=
|
# AUTH_OIDC_CLIENT_SECRET=
|
||||||
# AUTH_OIDC_NAME=SSO
|
# AUTH_OIDC_NAME=Authentik
|
||||||
# NEXT_PUBLIC_AUTH_OIDC_ENABLED=1
|
|
||||||
# NEXT_PUBLIC_AUTH_OIDC_NAME=SSO
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ on:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
check:
|
check:
|
||||||
|
name: lint-typecheck-test-build
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@@ -18,3 +19,14 @@ jobs:
|
|||||||
- run: npm run lint
|
- run: npm run lint
|
||||||
- run: npm run typecheck
|
- run: npm run typecheck
|
||||||
- run: npm test
|
- run: npm test
|
||||||
|
- run: npm run build
|
||||||
|
env:
|
||||||
|
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 .
|
||||||
|
|||||||
+31
-12
@@ -2,18 +2,21 @@
|
|||||||
|
|
||||||
FROM node:22-bookworm-slim AS deps
|
FROM node:22-bookworm-slim AS deps
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
RUN apt-get update && apt-get install -y python3 make g++ && rm -rf /var/lib/apt/lists/*
|
RUN apt-get update && apt-get install -y --no-install-recommends python3 make g++ \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
COPY package.json package-lock.json ./
|
COPY package.json package-lock.json ./
|
||||||
RUN npm ci
|
RUN npm ci
|
||||||
|
|
||||||
FROM node:22-bookworm-slim AS builder
|
FROM node:22-bookworm-slim AS builder
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
RUN apt-get update && apt-get install -y python3 make g++ && rm -rf /var/lib/apt/lists/*
|
RUN apt-get update && apt-get install -y --no-install-recommends python3 make g++ \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
COPY --from=deps /app/node_modules ./node_modules
|
COPY --from=deps /app/node_modules ./node_modules
|
||||||
COPY . .
|
COPY . .
|
||||||
ENV NEXT_TELEMETRY_DISABLED=1
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
ENV AUTH_SECRET=build-time-placeholder-secret-32chars
|
ENV AUTH_SECRET=build-time-placeholder-secret-32chars
|
||||||
RUN npm run build
|
RUN npm run build \
|
||||||
|
&& npm prune --omit=dev
|
||||||
|
|
||||||
FROM node:22-bookworm-slim AS runner
|
FROM node:22-bookworm-slim AS runner
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
@@ -21,16 +24,32 @@ ENV NODE_ENV=production
|
|||||||
ENV NEXT_TELEMETRY_DISABLED=1
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
ENV RFID_DB_PATH=/data/rfid.db
|
ENV RFID_DB_PATH=/data/rfid.db
|
||||||
ENV PORT=3000
|
ENV PORT=3000
|
||||||
RUN apt-get update && apt-get install -y libstdc++6 && rm -rf /var/lib/apt/lists/*
|
ENV HOSTNAME=0.0.0.0
|
||||||
RUN mkdir -p /data && chown node:node /data
|
ENV AUTH_TRUST_HOST=true
|
||||||
COPY --from=builder /app/package.json /app/package-lock.json ./
|
|
||||||
COPY --from=builder /app/node_modules ./node_modules
|
RUN apt-get update && apt-get install -y --no-install-recommends libstdc++6 curl \
|
||||||
COPY --from=builder /app/.next ./.next
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
COPY --from=builder /app/public ./public
|
&& mkdir -p /data \
|
||||||
COPY --from=builder /app/scripts ./scripts
|
&& chown node:node /data
|
||||||
COPY --from=builder /app/src ./src
|
|
||||||
COPY --from=builder /app/tsconfig.json ./tsconfig.json
|
COPY --from=builder --chown=node:node /app/package.json /app/package-lock.json ./
|
||||||
|
COPY --from=builder --chown=node:node /app/node_modules ./node_modules
|
||||||
|
COPY --from=builder --chown=node:node /app/.next ./.next
|
||||||
|
COPY --from=builder --chown=node:node /app/public ./public
|
||||||
|
COPY --from=builder --chown=node:node /app/scripts ./scripts
|
||||||
|
COPY --from=builder --chown=node:node /app/src ./src
|
||||||
|
COPY --from=builder --chown=node:node /app/tsconfig.json ./tsconfig.json
|
||||||
|
# tsx is needed for create-user / entrypoint bootstrap (devDependency pruned above)
|
||||||
|
RUN npm install --omit=dev [email protected] \
|
||||||
|
&& chown -R node:node /app/node_modules
|
||||||
|
COPY --chown=node:node docker/entrypoint.sh /entrypoint.sh
|
||||||
|
RUN chmod +x /entrypoint.sh
|
||||||
|
|
||||||
USER node
|
USER node
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
VOLUME ["/data"]
|
VOLUME ["/data"]
|
||||||
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
|
||||||
|
CMD curl -fsS http://127.0.0.1:3000/login >/dev/null || exit 1
|
||||||
|
|
||||||
|
ENTRYPOINT ["/entrypoint.sh"]
|
||||||
CMD ["npm", "run", "start"]
|
CMD ["npm", "run", "start"]
|
||||||
|
|||||||
@@ -30,13 +30,37 @@ Open http://localhost:3000 and sign in.
|
|||||||
| Variable | Required | Description |
|
| Variable | Required | Description |
|
||||||
|----------|----------|-------------|
|
|----------|----------|-------------|
|
||||||
| `AUTH_SECRET` | yes | NextAuth secret |
|
| `AUTH_SECRET` | yes | NextAuth secret |
|
||||||
|
| `AUTH_URL` | recommended | Public app URL (e.g. `https://rfid.example.com`) |
|
||||||
| `RFID_DB_PATH` | no | SQLite path (default `./data/rfid.db`) |
|
| `RFID_DB_PATH` | no | SQLite path (default `./data/rfid.db`) |
|
||||||
| `AUTH_OIDC_ISSUER` | no | OIDC issuer URL |
|
| `AUTH_OIDC_ISSUER` | for OIDC | Issuer URL (must expose `/.well-known/openid-configuration`) |
|
||||||
| `AUTH_OIDC_CLIENT_ID` | no | OIDC client id |
|
| `AUTH_OIDC_CLIENT_ID` | for OIDC | OIDC client id |
|
||||||
| `AUTH_OIDC_CLIENT_SECRET` | no | OIDC client secret |
|
| `AUTH_OIDC_CLIENT_SECRET` | for OIDC | OIDC client secret |
|
||||||
| `AUTH_OIDC_NAME` | no | Button label (default `SSO`) |
|
| `AUTH_OIDC_NAME` | no | SSO button label (default `SSO`) |
|
||||||
| `NEXT_PUBLIC_AUTH_OIDC_ENABLED` | no | Set `1` to show SSO button |
|
|
||||||
| `NEXT_PUBLIC_AUTH_OIDC_NAME` | no | Public SSO button label |
|
### Enabling OIDC
|
||||||
|
|
||||||
|
1. In your IdP, create a **confidential** OIDC application.
|
||||||
|
2. Set the redirect / callback URI to:
|
||||||
|
```
|
||||||
|
{AUTH_URL}/api/auth/callback/oidc
|
||||||
|
```
|
||||||
|
Example: `http://localhost:3000/api/auth/callback/oidc`
|
||||||
|
3. Put these in `.env` (Docker Compose reads `.env` automatically):
|
||||||
|
```bash
|
||||||
|
AUTH_URL=http://localhost:3000
|
||||||
|
AUTH_SECRET=...long random...
|
||||||
|
AUTH_OIDC_ISSUER=https://sso.example.com/application/o/rfid/
|
||||||
|
AUTH_OIDC_CLIENT_ID=...
|
||||||
|
AUTH_OIDC_CLIENT_SECRET=...
|
||||||
|
AUTH_OIDC_NAME=Authentik
|
||||||
|
```
|
||||||
|
4. Restart the app (`docker compose up -d` or restart `npm run dev`).
|
||||||
|
5. Open `/login` — you should see **Sign in with Authentik** (or your `AUTH_OIDC_NAME`).
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
- `AUTH_OIDC_ISSUER` must be exactly the issuer value from discovery (trailing slash matters for some IdPs).
|
||||||
|
- The IdP must return an **email** claim; accounts are created/linked by email.
|
||||||
|
- Local password login stays available alongside SSO.
|
||||||
|
|
||||||
## REST API (`/api/v1`)
|
## REST API (`/api/v1`)
|
||||||
|
|
||||||
@@ -80,22 +104,56 @@ curl -sH "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
|
|||||||
npm test
|
npm test
|
||||||
npm run lint
|
npm run lint
|
||||||
npm run typecheck
|
npm run typecheck
|
||||||
|
npm run build
|
||||||
```
|
```
|
||||||
|
|
||||||
GitHub Actions runs lint, typecheck, and tests 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**
|
||||||
|
2. **`docker build`** — catches Dockerfile parse/build failures (the class of break that conflict markers previously caused)
|
||||||
|
|
||||||
## Docker
|
## Docker
|
||||||
|
|
||||||
|
### docker compose (recommended)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cp .env.example .env
|
||||||
|
# set AUTH_SECRET to a long random string
|
||||||
|
|
||||||
|
# optional first user on boot:
|
||||||
|
# echo '[email protected]' >> .env
|
||||||
|
# echo 'CREATE_USER_PASSWORD=changeme' >> .env
|
||||||
|
|
||||||
|
docker compose up -d --build
|
||||||
|
```
|
||||||
|
|
||||||
|
App: http://localhost:3000 — SQLite persists in the `rfid-data` volume.
|
||||||
|
|
||||||
|
Create a user later (if you skipped bootstrap):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose exec rfid-database \
|
||||||
|
npx tsx scripts/create-user.ts --email [email protected] --password 'secret' --name You
|
||||||
|
```
|
||||||
|
|
||||||
|
Stop / remove (keeps volume):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker compose down
|
||||||
|
```
|
||||||
|
|
||||||
|
### Plain docker
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker build -t rfid-database .
|
docker build -t rfid-database .
|
||||||
docker run --rm -p 3000:3000 \
|
docker run --rm -p 3000:3000 \
|
||||||
-e AUTH_SECRET=your-long-secret \
|
-e AUTH_SECRET=your-long-secret \
|
||||||
|
-e [email protected] \
|
||||||
|
-e CREATE_USER_PASSWORD=changeme \
|
||||||
-v rfid-data:/data \
|
-v rfid-data:/data \
|
||||||
rfid-database
|
rfid-database
|
||||||
```
|
```
|
||||||
|
|
||||||
Create the first user against the mounted DB (exec into the container or run `create-user` with `RFID_DB_PATH` pointed at the volume).
|
|
||||||
|
|
||||||
## Security
|
## Security
|
||||||
|
|
||||||
Tag dumps often include sector keys. Keep the app behind HTTPS, do not expose it publicly without auth, and treat `data/rfid.db` as sensitive.
|
Tag dumps often include sector keys. Keep the app behind HTTPS, do not expose it publicly without auth, and treat `data/rfid.db` as sensitive.
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
services:
|
||||||
|
rfid-database:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
image: rfid-database:local
|
||||||
|
container_name: rfid-database
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- "${RFID_PORT:-3000}:3000"
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
environment:
|
||||||
|
NODE_ENV: production
|
||||||
|
RFID_DB_PATH: /data/rfid.db
|
||||||
|
PORT: "3000"
|
||||||
|
HOSTNAME: 0.0.0.0
|
||||||
|
AUTH_TRUST_HOST: "true"
|
||||||
|
# OIDC: set AUTH_OIDC_ISSUER / CLIENT_ID / CLIENT_SECRET (and AUTH_URL)
|
||||||
|
# in .env — the login page picks them up at runtime.
|
||||||
|
# Optional bootstrap (only creates if missing):
|
||||||
|
# CREATE_USER_EMAIL: [email protected]
|
||||||
|
# CREATE_USER_PASSWORD: change-me
|
||||||
|
# CREATE_USER_NAME: Admin
|
||||||
|
volumes:
|
||||||
|
- rfid-data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-fsS", "http://127.0.0.1:3000/login"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 3
|
||||||
|
start_period: 25s
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
rfid-data:
|
||||||
Executable
+21
@@ -0,0 +1,21 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -e
|
||||||
|
|
||||||
|
if [ -z "$AUTH_SECRET" ] || [ "$AUTH_SECRET" = "change-me-to-a-long-random-string" ]; then
|
||||||
|
echo "ERROR: AUTH_SECRET must be set to a long random value." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p "$(dirname "${RFID_DB_PATH:-/data/rfid.db}")"
|
||||||
|
|
||||||
|
# Optional first-boot user: set CREATE_USER_EMAIL + CREATE_USER_PASSWORD
|
||||||
|
if [ -n "$CREATE_USER_EMAIL" ] && [ -n "$CREATE_USER_PASSWORD" ]; then
|
||||||
|
echo "Ensuring user $CREATE_USER_EMAIL exists (ignored if already present)..."
|
||||||
|
npx tsx scripts/create-user.ts \
|
||||||
|
--email "$CREATE_USER_EMAIL" \
|
||||||
|
--password "$CREATE_USER_PASSWORD" \
|
||||||
|
--name "${CREATE_USER_NAME:-Admin}" \
|
||||||
|
|| true
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec "$@"
|
||||||
+6
-90
@@ -1,95 +1,11 @@
|
|||||||
"use client";
|
import { isOidcConfigured } from "@/lib/auth/oidc";
|
||||||
|
import { LoginPageClient } from "@/components/LoginPageClient";
|
||||||
import { FormEvent, useState } from "react";
|
|
||||||
import { signIn } from "next-auth/react";
|
|
||||||
import { useRouter, useSearchParams } from "next/navigation";
|
|
||||||
import { Suspense } from "react";
|
|
||||||
|
|
||||||
function LoginForm() {
|
|
||||||
const router = useRouter();
|
|
||||||
const params = useSearchParams();
|
|
||||||
const callbackUrl = params.get("callbackUrl") || "/";
|
|
||||||
const [email, setEmail] = useState("");
|
|
||||||
const [password, setPassword] = useState("");
|
|
||||||
const [error, setError] = useState<string | null>(null);
|
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
const oidcName = process.env.NEXT_PUBLIC_AUTH_OIDC_NAME || "SSO";
|
|
||||||
const oidcEnabled = process.env.NEXT_PUBLIC_AUTH_OIDC_ENABLED === "1";
|
|
||||||
|
|
||||||
async function onSubmit(e: FormEvent) {
|
|
||||||
e.preventDefault();
|
|
||||||
setLoading(true);
|
|
||||||
setError(null);
|
|
||||||
const res = await signIn("credentials", {
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
redirect: false,
|
|
||||||
callbackUrl,
|
|
||||||
});
|
|
||||||
setLoading(false);
|
|
||||||
if (res?.error) {
|
|
||||||
setError("Invalid email or password");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
router.push(callbackUrl);
|
|
||||||
router.refresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div style={{ maxWidth: 400, margin: "3rem auto" }}>
|
|
||||||
<h1 className="page-title">RFID Database</h1>
|
|
||||||
<p className="page-sub">Sign in to manage site tag dumps</p>
|
|
||||||
{error && <div className="error-banner">{error}</div>}
|
|
||||||
<form className="panel stack" onSubmit={onSubmit}>
|
|
||||||
<div className="field">
|
|
||||||
<label className="label" htmlFor="email">
|
|
||||||
Email
|
|
||||||
</label>
|
|
||||||
<input
|
|
||||||
id="email"
|
|
||||||
className="input"
|
|
||||||
type="email"
|
|
||||||
autoComplete="username"
|
|
||||||
value={email}
|
|
||||||
onChange={(e) => setEmail(e.target.value)}
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<div className="field">
|
|
||||||
<label className="label" htmlFor="password">
|
|
||||||
Password
|
|
||||||
</label>
|
|
||||||
<input
|
|
||||||
id="password"
|
|
||||||
className="input"
|
|
||||||
type="password"
|
|
||||||
autoComplete="current-password"
|
|
||||||
value={password}
|
|
||||||
onChange={(e) => setPassword(e.target.value)}
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
<button className="btn" type="submit" disabled={loading}>
|
|
||||||
{loading ? "Signing in…" : "Sign in"}
|
|
||||||
</button>
|
|
||||||
{oidcEnabled && (
|
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className="btn btn-secondary"
|
|
||||||
onClick={() => signIn("oidc", { callbackUrl })}
|
|
||||||
>
|
|
||||||
Sign in with {oidcName}
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
return (
|
return (
|
||||||
<Suspense>
|
<LoginPageClient
|
||||||
<LoginForm />
|
oidcEnabled={isOidcConfigured()}
|
||||||
</Suspense>
|
oidcName={process.env.AUTH_OIDC_NAME || "SSO"}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { FormEvent, useState } from "react";
|
||||||
|
import { signIn } from "next-auth/react";
|
||||||
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
|
import { Suspense } from "react";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
oidcEnabled: boolean;
|
||||||
|
oidcName: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function LoginForm({ oidcEnabled, oidcName }: Props) {
|
||||||
|
const router = useRouter();
|
||||||
|
const params = useSearchParams();
|
||||||
|
const callbackUrl = params.get("callbackUrl") || "/";
|
||||||
|
const [email, setEmail] = useState("");
|
||||||
|
const [password, setPassword] = useState("");
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
async function onSubmit(e: FormEvent) {
|
||||||
|
e.preventDefault();
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
const res = await signIn("credentials", {
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
redirect: false,
|
||||||
|
callbackUrl,
|
||||||
|
});
|
||||||
|
setLoading(false);
|
||||||
|
if (res?.error) {
|
||||||
|
setError("Invalid email or password");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
router.push(callbackUrl);
|
||||||
|
router.refresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ maxWidth: 400, margin: "3rem auto" }}>
|
||||||
|
<h1 className="page-title">RFID Database</h1>
|
||||||
|
<p className="page-sub">Sign in to manage site tag dumps</p>
|
||||||
|
{error && <div className="error-banner">{error}</div>}
|
||||||
|
<form className="panel stack" onSubmit={onSubmit}>
|
||||||
|
<div className="field">
|
||||||
|
<label className="label" htmlFor="email">
|
||||||
|
Email
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="email"
|
||||||
|
className="input"
|
||||||
|
type="email"
|
||||||
|
autoComplete="username"
|
||||||
|
value={email}
|
||||||
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="field">
|
||||||
|
<label className="label" htmlFor="password">
|
||||||
|
Password
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="password"
|
||||||
|
className="input"
|
||||||
|
type="password"
|
||||||
|
autoComplete="current-password"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
|
required
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button className="btn" type="submit" disabled={loading}>
|
||||||
|
{loading ? "Signing in…" : "Sign in"}
|
||||||
|
</button>
|
||||||
|
{oidcEnabled && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="btn btn-secondary"
|
||||||
|
onClick={() => signIn("oidc", { callbackUrl })}
|
||||||
|
>
|
||||||
|
Sign in with {oidcName}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function LoginPageClient(props: Props) {
|
||||||
|
return (
|
||||||
|
<Suspense>
|
||||||
|
<LoginForm {...props} />
|
||||||
|
</Suspense>
|
||||||
|
);
|
||||||
|
}
|
||||||
+8
-15
@@ -6,6 +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 { isOidcConfigured } from "@/lib/auth/oidc";
|
||||||
|
|
||||||
|
export { isOidcConfigured } from "@/lib/auth/oidc";
|
||||||
|
|
||||||
function buildProviders(): Provider[] {
|
function buildProviders(): Provider[] {
|
||||||
const providers: Provider[] = [
|
const providers: Provider[] = [
|
||||||
@@ -32,17 +35,15 @@ function buildProviders(): Provider[] {
|
|||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
const issuer = process.env.AUTH_OIDC_ISSUER;
|
if (isOidcConfigured()) {
|
||||||
const clientId = process.env.AUTH_OIDC_CLIENT_ID;
|
|
||||||
const clientSecret = process.env.AUTH_OIDC_CLIENT_SECRET;
|
|
||||||
if (issuer && clientId && clientSecret) {
|
|
||||||
providers.push({
|
providers.push({
|
||||||
id: "oidc",
|
id: "oidc",
|
||||||
name: process.env.AUTH_OIDC_NAME || "SSO",
|
name: process.env.AUTH_OIDC_NAME || "SSO",
|
||||||
type: "oidc",
|
type: "oidc",
|
||||||
issuer,
|
issuer: process.env.AUTH_OIDC_ISSUER!,
|
||||||
clientId,
|
clientId: process.env.AUTH_OIDC_CLIENT_ID!,
|
||||||
clientSecret,
|
clientSecret: process.env.AUTH_OIDC_CLIENT_SECRET!,
|
||||||
|
// Link OIDC logins to existing local users by email
|
||||||
allowDangerousEmailAccountLinking: true,
|
allowDangerousEmailAccountLinking: true,
|
||||||
} as Provider);
|
} as Provider);
|
||||||
}
|
}
|
||||||
@@ -82,11 +83,3 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export function isOidcConfigured(): boolean {
|
|
||||||
return Boolean(
|
|
||||||
process.env.AUTH_OIDC_ISSUER &&
|
|
||||||
process.env.AUTH_OIDC_CLIENT_ID &&
|
|
||||||
process.env.AUTH_OIDC_CLIENT_SECRET
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
export function isOidcConfigured(): boolean {
|
||||||
|
return Boolean(
|
||||||
|
process.env.AUTH_OIDC_ISSUER?.trim() &&
|
||||||
|
process.env.AUTH_OIDC_CLIENT_ID?.trim() &&
|
||||||
|
process.env.AUTH_OIDC_CLIENT_SECRET?.trim()
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -21,6 +21,10 @@ import { GET as getTags, POST as postTag } from "@/app/api/v1/tags/route";
|
|||||||
import { PUT as putByUid } from "@/app/api/v1/tags/by-uid/[uid]/route";
|
import { PUT as putByUid } from "@/app/api/v1/tags/by-uid/[uid]/route";
|
||||||
import { GET as getExport } from "@/app/api/v1/tags/[id]/export/route";
|
import { GET as getExport } from "@/app/api/v1/tags/[id]/export/route";
|
||||||
import { GET as listTokens, POST as createToken } from "@/app/api/v1/tokens/route";
|
import { GET as listTokens, POST as createToken } from "@/app/api/v1/tokens/route";
|
||||||
|
import { DELETE as deleteToken } from "@/app/api/v1/tokens/[id]/route";
|
||||||
|
import { GET as search } from "@/app/api/v1/search/route";
|
||||||
|
import { GET as getBackup, POST as postBackup } from "@/app/api/v1/backup/route";
|
||||||
|
import { POST as importTag } from "@/app/api/v1/tags/import/route";
|
||||||
import { NextRequest } from "next/server";
|
import { NextRequest } from "next/server";
|
||||||
|
|
||||||
function req(url: string, init?: ConstructorParameters<typeof NextRequest>[1]) {
|
function req(url: string, init?: ConstructorParameters<typeof NextRequest>[1]) {
|
||||||
@@ -189,9 +193,81 @@ describe("API v1 integration", () => {
|
|||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const revoked = await deleteToken(
|
||||||
|
req(`http://localhost/api/v1/tokens/${data.id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: auth,
|
||||||
|
}),
|
||||||
|
{ params: Promise.resolve({ id: data.id }) }
|
||||||
|
);
|
||||||
|
expect(revoked.status).toBe(200);
|
||||||
|
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
const user = db.select().from(users).where(eq(users.id, userId)).get();
|
const user = db.select().from(users).where(eq(users.id, userId)).get();
|
||||||
expect(user?.email).toBe("[email protected]");
|
expect(user?.email).toBe("[email protected]");
|
||||||
expect(db.select().from(sites).all().length).toBeGreaterThanOrEqual(0);
|
expect(db.select().from(sites).all().length).toBeGreaterThanOrEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("searches, imports MCT, and backs up", async () => {
|
||||||
|
const auth = { Authorization: `Bearer ${bearer}` };
|
||||||
|
|
||||||
|
const siteRes = await postSite(
|
||||||
|
req("http://localhost/api/v1/sites", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { ...auth, "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ code: "C", name: "Lab" }),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
const site = await siteRes.json();
|
||||||
|
|
||||||
|
const mct = `+UID: AABBCCDD
|
||||||
|
+Sector: 0
|
||||||
|
AABBCCDD00112233445566778899AABB
|
||||||
|
00000000000000000000000000000000
|
||||||
|
00000000000000000000000000000000
|
||||||
|
FFFFFFFFFFFFFF078069FFFFFFFFFFFF
|
||||||
|
`;
|
||||||
|
|
||||||
|
const imported = await importTag(
|
||||||
|
req("http://localhost/api/v1/tags/import", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { ...auth, "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
siteId: site.id,
|
||||||
|
label: "Imported fob",
|
||||||
|
content: mct,
|
||||||
|
filename: "sample.mct",
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
expect(imported.status).toBe(201);
|
||||||
|
const tag = await imported.json();
|
||||||
|
expect(tag.uid).toBe("AABBCCDD");
|
||||||
|
|
||||||
|
const searchRes = await search(
|
||||||
|
req("http://localhost/api/v1/search?q=Imported", { headers: auth })
|
||||||
|
);
|
||||||
|
expect(searchRes.status).toBe(200);
|
||||||
|
const found = await searchRes.json();
|
||||||
|
expect(found.tags.some((t: { label: string }) => t.label === "Imported fob")).toBe(
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
const backupRes = await getBackup(
|
||||||
|
req("http://localhost/api/v1/backup", { headers: auth })
|
||||||
|
);
|
||||||
|
expect(backupRes.status).toBe(200);
|
||||||
|
const backup = await backupRes.json();
|
||||||
|
expect(backup.sites.length).toBeGreaterThanOrEqual(1);
|
||||||
|
expect(backup.tags.length).toBeGreaterThanOrEqual(1);
|
||||||
|
|
||||||
|
const restore = await postBackup(
|
||||||
|
req("http://localhost/api/v1/backup", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { ...auth, "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ ...backup, mode: "merge" }),
|
||||||
|
})
|
||||||
|
);
|
||||||
|
expect(restore.status).toBe(200);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { exportTag } from "@/lib/rfid/exporters";
|
||||||
|
import {
|
||||||
|
dumpDataSchema,
|
||||||
|
mifareClassicDumpSchema,
|
||||||
|
siteCreateSchema,
|
||||||
|
tagCreateSchema,
|
||||||
|
} from "@/lib/validation/rfid";
|
||||||
|
|
||||||
|
const classicDump = {
|
||||||
|
size: "1K" as const,
|
||||||
|
sectors: [
|
||||||
|
{
|
||||||
|
index: 0,
|
||||||
|
blocks: [
|
||||||
|
"04A1B2C304A1B2C304A1B2C304A1B2C3",
|
||||||
|
"00000000000000000000000000000000",
|
||||||
|
"00000000000000000000000000000000",
|
||||||
|
"FFFFFFFFFFFFFF078069FFFFFFFFFFFF",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const sampleTag = {
|
||||||
|
id: "00000000-0000-4000-8000-000000000001",
|
||||||
|
label: "Dock fob",
|
||||||
|
frequency: "HF",
|
||||||
|
protocol: "MIFARE_CLASSIC_1K",
|
||||||
|
uid: "04A1B2C3",
|
||||||
|
dumpData: classicDump,
|
||||||
|
keys: { A: ["FFFFFFFFFFFF"], B: [] },
|
||||||
|
notes: null as string | null,
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("exporters", () => {
|
||||||
|
it("exports canonical JSON", () => {
|
||||||
|
const out = exportTag(sampleTag, "json");
|
||||||
|
expect(out.contentType).toBe("application/json");
|
||||||
|
const parsed = JSON.parse(out.body);
|
||||||
|
expect(parsed.uid).toBe("04A1B2C3");
|
||||||
|
expect(parsed.dumpData.size).toBe("1K");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("exports proxmark JSON with blocks", () => {
|
||||||
|
const out = exportTag(sampleTag, "proxmark");
|
||||||
|
const parsed = JSON.parse(out.body);
|
||||||
|
expect(parsed.FileType).toBe("mfcard");
|
||||||
|
expect(parsed.blocks).toHaveLength(4);
|
||||||
|
expect(parsed.Card.UID).toBe("04A1B2C3");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("exports MCT text", () => {
|
||||||
|
const out = exportTag(sampleTag, "mct");
|
||||||
|
expect(out.body).toContain("+UID: 04A1B2C3");
|
||||||
|
expect(out.body).toContain("+Sector: 0");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("exports hex listing", () => {
|
||||||
|
const out = exportTag(sampleTag, "hex");
|
||||||
|
expect(out.body).toContain("# UID 04A1B2C3");
|
||||||
|
expect(out.body).toContain("S0B0");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("validation schemas", () => {
|
||||||
|
it("accepts valid classic dump", () => {
|
||||||
|
expect(mifareClassicDumpSchema.safeParse(classicDump).success).toBe(true);
|
||||||
|
expect(dumpDataSchema.safeParse(classicDump).success).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects invalid site codes", () => {
|
||||||
|
expect(
|
||||||
|
siteCreateSchema.safeParse({ name: "Lab", code: "bad code!" }).success
|
||||||
|
).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accepts valid tag create payload", () => {
|
||||||
|
const parsed = tagCreateSchema.safeParse({
|
||||||
|
siteId: "00000000-0000-4000-8000-000000000099",
|
||||||
|
label: "Dock",
|
||||||
|
frequency: "HF",
|
||||||
|
protocol: "MIFARE_CLASSIC_1K",
|
||||||
|
uid: "04:A1:B2:C3",
|
||||||
|
dumpData: classicDump,
|
||||||
|
keys: { A: ["FFFFFFFFFFFF"] },
|
||||||
|
});
|
||||||
|
expect(parsed.success).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import { afterEach, describe, expect, it } from "vitest";
|
||||||
|
import { isOidcConfigured } from "@/lib/auth/oidc";
|
||||||
|
|
||||||
|
describe("isOidcConfigured", () => {
|
||||||
|
const keys = [
|
||||||
|
"AUTH_OIDC_ISSUER",
|
||||||
|
"AUTH_OIDC_CLIENT_ID",
|
||||||
|
"AUTH_OIDC_CLIENT_SECRET",
|
||||||
|
] as const;
|
||||||
|
const previous: Record<string, string | undefined> = {};
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
for (const key of keys) {
|
||||||
|
if (previous[key] === undefined) delete process.env[key];
|
||||||
|
else process.env[key] = previous[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function save() {
|
||||||
|
for (const key of keys) previous[key] = process.env[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
it("is false when any var is missing", () => {
|
||||||
|
save();
|
||||||
|
delete process.env.AUTH_OIDC_ISSUER;
|
||||||
|
delete process.env.AUTH_OIDC_CLIENT_ID;
|
||||||
|
delete process.env.AUTH_OIDC_CLIENT_SECRET;
|
||||||
|
expect(isOidcConfigured()).toBe(false);
|
||||||
|
|
||||||
|
process.env.AUTH_OIDC_ISSUER = "https://sso.example.com/";
|
||||||
|
process.env.AUTH_OIDC_CLIENT_ID = "id";
|
||||||
|
expect(isOidcConfigured()).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("is true when all three are set", () => {
|
||||||
|
save();
|
||||||
|
process.env.AUTH_OIDC_ISSUER = "https://sso.example.com/";
|
||||||
|
process.env.AUTH_OIDC_CLIENT_ID = "id";
|
||||||
|
process.env.AUTH_OIDC_CLIENT_SECRET = "secret";
|
||||||
|
expect(isOidcConfigured()).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user