@@ -43,6 +43,7 @@ export function getUserForCheck(userId: string): IUser {
4343 fields : {
4444 emails : 1 ,
4545 language : 1 ,
46+ createdAt : 1 ,
4647 'services.totp' : 1 ,
4748 'services.email2fa' : 1 ,
4849 'services.emailCode' : 1 ,
@@ -61,6 +62,19 @@ export function getFingerprintFromConnection(connection: IMethodConnection): str
6162 return crypto . createHash ( 'md5' ) . update ( data ) . digest ( 'hex' ) ;
6263}
6364
65+ function getRememberDate ( from : Date = new Date ( ) ) : Date | undefined {
66+ const rememberFor = parseInt ( settings . get ( 'Accounts_TwoFactorAuthentication_RememberFor' ) as string , 10 ) ;
67+
68+ if ( rememberFor <= 0 ) {
69+ return ;
70+ }
71+
72+ const expires = new Date ( from ) ;
73+ expires . setSeconds ( expires . getSeconds ( ) + rememberFor ) ;
74+
75+ return expires ;
76+ }
77+
6478export function isAuthorizedForToken ( connection : IMethodConnection , user : IUser , options : ITwoFactorOptions ) : boolean {
6579 const currentToken = Accounts . _getLoginToken ( connection . id ) ;
6680 const tokenObject = user . services ?. resume ?. loginTokens ?. find ( ( i ) => i . hashedToken === currentToken ) ;
@@ -77,6 +91,12 @@ export function isAuthorizedForToken(connection: IMethodConnection, user: IUser,
7791 return false ;
7892 }
7993
94+ // remember user right after their registration
95+ const rememberAfterRegistration = user . createdAt && getRememberDate ( user . createdAt ) ;
96+ if ( rememberAfterRegistration && rememberAfterRegistration >= new Date ( ) ) {
97+ return true ;
98+ }
99+
80100 if ( ! tokenObject . twoFactorAuthorizedUntil || ! tokenObject . twoFactorAuthorizedHash ) {
81101 return false ;
82102 }
@@ -95,15 +115,11 @@ export function isAuthorizedForToken(connection: IMethodConnection, user: IUser,
95115export function rememberAuthorization ( connection : IMethodConnection , user : IUser ) : void {
96116 const currentToken = Accounts . _getLoginToken ( connection . id ) ;
97117
98- const rememberFor = parseInt ( settings . get ( 'Accounts_TwoFactorAuthentication_RememberFor' ) as string , 10 ) ;
99-
100- if ( rememberFor <= 0 ) {
118+ const expires = getRememberDate ( ) ;
119+ if ( ! expires ) {
101120 return ;
102121 }
103122
104- const expires = new Date ( ) ;
105- expires . setSeconds ( expires . getSeconds ( ) + rememberFor ) ;
106-
107123 Users . setTwoFactorAuthorizationHashAndUntilForUserIdAndToken ( user . _id , currentToken , getFingerprintFromConnection ( connection ) , expires ) ;
108124}
109125
0 commit comments