diff --git a/index.ts b/index.ts index a087585..9a9dc9c 100644 --- a/index.ts +++ b/index.ts @@ -293,7 +293,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { const beforeLoginConfirmation = this.adminforth.config.auth.beforeLoginConfirmation; const beforeLoginConfirmationArray = Array.isArray(beforeLoginConfirmation) ? beforeLoginConfirmation : [beforeLoginConfirmation]; beforeLoginConfirmationArray.push( - async({ adminUser, response, extra }: { adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: any} )=> { + async({ adminUser, response, extra, sessionDuration }: { adminUser: AdminUser, response: IAdminForthHttpResponse, extra?: any, sessionDuration?: number} )=> { if (extra?.body?.loginAllowedByPasskeyDirectSignIn === true) { return { body: { loginAllowed: true }, ok: true }; } @@ -307,8 +307,8 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { const authResource = adminforth.config.resources.find((res)=>res.resourceId === adminforth.config.auth.usersResourceId ) const authPk = authResource.columns.find((col)=>col.primaryKey).name const userPk = adminUser.dbUser[authPk] - const rememberMe = extra?.body?.rememberMe || false; - const rememberMeDays = rememberMe ? adminforth.config.auth.rememberMeDays || 30 : 1; + + const useSessionDuration = sessionDuration || 1; // default 1 day, but should never happen for new versions let newSecret = null; const userNeeds2FA = this.options.usersFilterToApply ? this.options.usersFilterToApply(adminUser) : true; @@ -321,7 +321,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { const tempSecret = twofactor.generateSecret({name: issuerName,account: userName}) newSecret = tempSecret.secret; - const totpTemporaryJWT = this.adminforth.auth.issueJWT({userName, newSecret, issuer:issuerName, pk:userPk, userCanSkipSetup, rememberMeDays }, 'temp2FA', this.options.passkeys?.challengeValidityPeriod || '1m'); + const totpTemporaryJWT = this.adminforth.auth.issueJWT({userName, newSecret, issuer:issuerName, pk:userPk, userCanSkipSetup, sessionDuration: useSessionDuration }, 'temp2FA', this.options.passkeys?.challengeValidityPeriod || '1m'); this.adminforth.auth.setCustomCookie({response, payload: {name: "2FaTemporaryJWT", value: totpTemporaryJWT, expiry: undefined, expirySeconds: 10 * 60, httpOnly: true}}); return { @@ -333,7 +333,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { } } else { - const value = this.adminforth.auth.issueJWT({userName, issuer:issuerName, pk:userPk, userCanSkipSetup, rememberMeDays }, 'temp2FA', this.options.passkeys?.challengeValidityPeriod || '1m'); + const value = this.adminforth.auth.issueJWT({userName, issuer:issuerName, pk:userPk, userCanSkipSetup, sessionDuration: useSessionDuration }, 'temp2FA', this.options.passkeys?.challengeValidityPeriod || '1m'); this.adminforth.auth.setCustomCookie({response, payload: {name: "2FaTemporaryJWT", value: value, expiry: undefined, expirySeconds: 10 * 60, httpOnly: true}}); return { @@ -389,7 +389,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { await connector.updateRecord({resource:this.authResource, recordId:decoded.pk, newValues:{[this.options.twoFaSecretFieldName]: decoded.newSecret}}) } this.adminforth.auth.removeCustomCookie({response, name:'2FaTemporaryJWT'}) - this.adminforth.auth.setAuthCookie({expireInDays: decoded.rememberMeDays, response, username:decoded.userName, pk:decoded.pk}) + this.adminforth.auth.setAuthCookie({expireInDays: decoded.sessionDuration, response, username:decoded.userName, pk:decoded.pk}) return { status: 'ok', allowedLogin: true } } else { return {error: 'Wrong or expired OTP code'} @@ -420,7 +420,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { } if (verified) { this.adminforth.auth.removeCustomCookie({response, name:'2FaTemporaryJWT'}) - this.adminforth.auth.setAuthCookie({expireInDays: decoded.rememberMeDays, response, username:decoded.userName, pk:decoded.pk}) + this.adminforth.auth.setAuthCookie({expireInDays: decoded.sessionDuration, response, username:decoded.userName, pk:decoded.pk}) return { status: 'ok', allowedLogin: true } } else { return {error: 'Verification failed'} @@ -496,6 +496,7 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { const toReturn = { allowedLogin: true, error: '' }; + const sessionDuration = this.options.passkeys.rememberDaysAfterPasskeyLogin ? this.options.passkeys.rememberDaysAfterPasskeyLogin : this.adminforth.config.auth.rememberMeDays; await this.adminforth.restApi.processLoginCallbacks(adminUser, toReturn, response, { headers, cookies, @@ -504,14 +505,14 @@ export default class TwoFactorsAuthPlugin extends AdminForthPlugin { body: { loginAllowedByPasskeyDirectSignIn: true }, - }); + }, sessionDuration ); if ( toReturn.allowedLogin === true ) { this.adminforth.auth.setAuthCookie({ response, username, pk: user.id, - expireInDays: this.options.passkeys.rememberDaysAfterPasskeyLogin ? this.options.passkeys.rememberDaysAfterPasskeyLogin : this.adminforth.config.auth.rememberMeDays, + expireInDays: sessionDuration, }); } return toReturn;