Einmalpasswort-Flow für Portal-Credentials
Wenn Admin "Zugangsdaten versenden" klickt, ist das Passwort jetzt ein echtes Einmalpasswort: beim ersten erfolgreichen Portal-Login werden Hash + Encrypted-Feld sofort genullt und der Kunde wird zwangsweise auf eine "Neues Passwort vergeben"-Seite geleitet. Erst nach eigenem Passwort kommt er ins Portal. Schema: - Customer.portalPasswordMustChange: Boolean @default(false) Backend: - sendPortalCredentials setzt Flag = true + erweitertes Mail-Template mit Einmalpasswort-Warnung - customerLogin: bei Flag=true wird OTP konsumiert (Hash+Encrypted=null, portalLastLogin aktualisiert), Response enthält mustChangePassword=true in token-payload + user-objekt - setCustomerPortalPassword (manuelles Setzen) räumt Flag wieder auf - changeInitialPortalPassword: neue Service-Funktion + Endpoint POST /api/auth/change-initial-portal-password (authenticated, nur Portal-User), validiert Komplexität, setzt neuen Hash, löscht Encrypted, invalidiert Session via portalTokenInvalidatedAt Frontend: - User-Type erweitert um mustChangePassword - AuthContext.customerLogin gibt User zurück (für sofortige Routing- Entscheidung) - Login.tsx: redirect zu /change-initial-password wenn mustChangePassword - ProtectedRoute: zwingt eingeloggte User mit Flag immer zur Change-Seite - ChangeInitialPasswordGate: blockt User OHNE Flag vom Zugriff - ChangeInitialPassword: eigene Seite mit Live-Komplexitäts-Hint, Passwort-Wiederholung, automatischer Logout + Redirect nach Erfolg Live-verifiziert (10 Schritte): - Setzen → Send → DB-Flag=true → OTP-Login gibt mustChange=true und consumed Hash → Re-Login mit OTP fehlschlägt → Change schwach=400, komplex=200 → neues Passwort funktioniert → Session invalidated. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -180,14 +180,30 @@ export async function customerLogin(email: string, password: string) {
|
||||
throw new Error('Ungültige Anmeldedaten');
|
||||
}
|
||||
|
||||
// Lazy-Upgrade analog zu Mitarbeiter-Login
|
||||
maybeUpgradePasswordHash('customer', customer.id, password, customer.portalPasswordHash).catch(() => {});
|
||||
// Einmalpasswort-Check: wurde es per "Zugangsdaten versenden" verschickt?
|
||||
// Falls ja, jetzt sofort verbrauchen – Hash + Encrypted nullen, damit
|
||||
// weder Re-Login noch Klartext-Abruf möglich ist. Customer landet im
|
||||
// Force-Change-Password-Flow.
|
||||
const mustChangePassword = customer.portalPasswordMustChange === true;
|
||||
if (mustChangePassword) {
|
||||
await prisma.customer.update({
|
||||
where: { id: customer.id },
|
||||
data: {
|
||||
portalPasswordHash: null,
|
||||
portalPasswordEncrypted: null,
|
||||
portalLastLogin: new Date(),
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Lazy-Upgrade analog zu Mitarbeiter-Login
|
||||
maybeUpgradePasswordHash('customer', customer.id, password, customer.portalPasswordHash).catch(() => {});
|
||||
|
||||
// Letzte Anmeldung aktualisieren
|
||||
await prisma.customer.update({
|
||||
where: { id: customer.id },
|
||||
data: { portalLastLogin: new Date() },
|
||||
});
|
||||
// Letzte Anmeldung aktualisieren
|
||||
await prisma.customer.update({
|
||||
where: { id: customer.id },
|
||||
data: { portalLastLogin: new Date() },
|
||||
});
|
||||
}
|
||||
|
||||
// IDs der Kunden sammeln, die dieser Kunde vertreten kann
|
||||
const representedCustomerIds = customer.representingFor.map(
|
||||
@@ -214,6 +230,7 @@ export async function customerLogin(email: string, password: string) {
|
||||
return {
|
||||
accessToken,
|
||||
refreshToken,
|
||||
mustChangePassword,
|
||||
user: {
|
||||
id: customer.id,
|
||||
email: customer.portalEmail,
|
||||
@@ -222,6 +239,7 @@ export async function customerLogin(email: string, password: string) {
|
||||
permissions: customerPermissions,
|
||||
customerId: customer.id,
|
||||
isCustomerPortal: true,
|
||||
mustChangePassword,
|
||||
representedCustomers: customer.representingFor.map((rep) => ({
|
||||
id: rep.customer.id,
|
||||
customerNumber: rep.customer.customerNumber,
|
||||
@@ -331,17 +349,45 @@ export async function setCustomerPortalPassword(customerId: number, password: st
|
||||
|
||||
console.log('[SetPortalPassword] Hash erstellt, Länge:', hashedPassword.length);
|
||||
|
||||
// Manuelles Setzen ist KEIN Einmalpasswort → Flag immer zurücksetzen,
|
||||
// falls vorher ein OTP gesetzt war.
|
||||
await prisma.customer.update({
|
||||
where: { id: customerId },
|
||||
data: {
|
||||
portalPasswordHash: hashedPassword,
|
||||
portalPasswordEncrypted: encryptedPassword,
|
||||
portalPasswordMustChange: false,
|
||||
},
|
||||
});
|
||||
|
||||
console.log('[SetPortalPassword] Passwort gespeichert');
|
||||
}
|
||||
|
||||
// Vom Endkunden selbst gesetztes Initial-Passwort nach OTP-Login.
|
||||
// Speichert neuen Hash, löscht das verbrauchte Encrypted-Feld (Klartext-
|
||||
// Speicherung soll bei OFF self-service nicht zurückkommen) und invalidiert
|
||||
// sofort alle bestehenden Sessions, damit Login mit dem neuen Passwort
|
||||
// gefordert wird.
|
||||
export async function changeInitialPortalPassword(customerId: number, newPassword: string) {
|
||||
const hashedPassword = await bcrypt.hash(newPassword, BCRYPT_COST);
|
||||
await prisma.customer.update({
|
||||
where: { id: customerId },
|
||||
data: {
|
||||
portalPasswordHash: hashedPassword,
|
||||
portalPasswordEncrypted: null,
|
||||
portalPasswordMustChange: false,
|
||||
portalTokenInvalidatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function markPortalPasswordForChange(customerId: number) {
|
||||
await prisma.customer.update({
|
||||
where: { id: customerId },
|
||||
data: { portalPasswordMustChange: true },
|
||||
});
|
||||
}
|
||||
|
||||
// Kundenportal-Passwort im Klartext abrufen
|
||||
export async function getCustomerPortalPassword(customerId: number): Promise<string | null> {
|
||||
const customer = await prisma.customer.findUnique({
|
||||
@@ -561,8 +607,14 @@ export async function sendPortalCredentialsEmail(params: {
|
||||
<tr><td style="padding: 6px 12px; color: #6b7280;">Passwort:</td>
|
||||
<td style="padding: 6px 12px; font-family: monospace;">${esc(params.password)}</td></tr>
|
||||
</table>
|
||||
<p style="color: #b91c1c; font-size: 14px; font-weight: 600;">
|
||||
⚠️ Dieses Passwort ist ein <u>Einmalpasswort</u>.
|
||||
</p>
|
||||
<p style="color: #6b7280; font-size: 14px;">
|
||||
Bitte ändern Sie Ihr Passwort nach dem ersten Login (im Portal unter „Mein Konto").
|
||||
Beim ersten Login werden Sie aufgefordert, ein eigenes Passwort zu vergeben.
|
||||
Danach ist dieses Passwort hier <strong>nicht mehr gültig</strong> – falls Sie den
|
||||
Vorgang abbrechen, fordern Sie bitte neue Zugangsdaten an oder nutzen die
|
||||
Passwort-vergessen-Funktion.
|
||||
</p>
|
||||
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 24px 0;">
|
||||
<p style="color: #9ca3af; font-size: 12px;">
|
||||
|
||||
Reference in New Issue
Block a user