mirror of
https://github.com/Chewbaccalakis/rfid-database.git
synced 2026-09-10 00:11:56 -07:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88c0c4a453 |
@@ -67,26 +67,8 @@ Open http://localhost:3000 and sign in.
|
||||
6. Open `/login` — you should see **Sign in with AtlasHorizon**.
|
||||
|
||||
Notes:
|
||||
- `AUTH_OIDC_ISSUER` must match discovery (`{issuer}/.well-known/openid-configuration`).
|
||||
- The app requests `openid email profile` and loads profile from **UserInfo** (Authelia puts `email` there by default).
|
||||
- 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...
|
||||
```
|
||||
|
||||
- `AUTH_OIDC_ISSUER` must match discovery (`{issuer}/.well-known/openid-configuration`). Your AtlasHorizon issuer at `https://auth.atlashorizon.net` is valid.
|
||||
- The IdP must return an **email** claim (we request `openid email profile`).
|
||||
- Local password login stays available alongside SSO.
|
||||
- If the button is missing, you are almost certainly on an old image — rebuild.
|
||||
|
||||
|
||||
+14
-19
@@ -6,9 +6,9 @@ import { eq } from "drizzle-orm";
|
||||
import { getDb } from "@/db/client";
|
||||
import { users } from "@/db/schema";
|
||||
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[] {
|
||||
const providers: Provider[] = [
|
||||
@@ -47,9 +47,6 @@ function buildProviders(): Provider[] {
|
||||
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: {
|
||||
params: {
|
||||
scope: "openid email profile",
|
||||
@@ -61,16 +58,21 @@ function buildProviders(): Provider[] {
|
||||
// Link OIDC logins to existing local users by email
|
||||
allowDangerousEmailAccountLinking: true,
|
||||
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 {
|
||||
id: String(profile.sub ?? email ?? crypto.randomUUID()),
|
||||
id: String(profile.sub ?? ""),
|
||||
name:
|
||||
(typeof profile.name === "string" && profile.name) ||
|
||||
(typeof profile.preferred_username === "string" &&
|
||||
profile.preferred_username) ||
|
||||
email ||
|
||||
"OIDC user",
|
||||
email: email ?? undefined,
|
||||
email,
|
||||
image: typeof profile.picture === "string" ? profile.picture : null,
|
||||
};
|
||||
},
|
||||
@@ -85,24 +87,17 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
||||
providers: buildProviders(),
|
||||
callbacks: {
|
||||
...authConfig.callbacks,
|
||||
async signIn({ user, account, profile }) {
|
||||
async signIn({ user, account }) {
|
||||
if (account?.provider === "credentials") return true;
|
||||
|
||||
const email =
|
||||
user.email?.trim().toLowerCase() ||
|
||||
emailFromOidcProfile((profile ?? {}) as Record<string, unknown>);
|
||||
|
||||
if (!email) {
|
||||
if (!user.email) {
|
||||
console.error(
|
||||
"[auth] OIDC sign-in rejected: no email in profile. Claim keys:",
|
||||
profile ? Object.keys(profile) : []
|
||||
"[auth] OIDC sign-in rejected: IdP did not return an email claim. Enable the email scope/claim on the client."
|
||||
);
|
||||
return "/login?error=EmailRequired";
|
||||
}
|
||||
|
||||
user.email = email;
|
||||
|
||||
const db = getDb();
|
||||
const email = user.email.toLowerCase();
|
||||
let existing = db.select().from(users).where(eq(users.email, email)).get();
|
||||
if (!existing) {
|
||||
const id = crypto.randomUUID();
|
||||
|
||||
@@ -5,23 +5,3 @@ export function isOidcConfigured(): boolean {
|
||||
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