1616from ckan .lib .redis import connect_to_redis
1717from ckan .views .user import next_page_or_default , rotate_token
1818
19- from ckanext .auth import config
2019from ckanext .auth import config as auth_config
2120from ckanext .auth .exceptions import ReplayAttackError
2221from ckanext .auth .model import UserSecret
@@ -84,8 +83,8 @@ def reset_all(cls) -> None:
8483 redis .delete (key )
8584
8685
87- def send_verification_email_to_user (user_id : str ) -> bool :
88- user = model . User . get ( user_id )
86+ def send_verification_email_to_user (user_reference : str ) -> bool :
87+ user = get_user_by_username_or_email ( user_reference )
8988
9089 if not user or not user .email :
9190 return False
@@ -96,7 +95,7 @@ def send_verification_email_to_user(user_id: str) -> bool:
9695 "site_url" : tk .config ["ckan.site_url" ],
9796 "site_title" : tk .config ["ckan.site_title" ],
9897 "user_name" : user .display_name ,
99- "subject" : tk ._ (config .get_2fa_subject ()),
98+ "subject" : tk ._ (auth_config .get_2fa_subject ()),
10099 "body" : f"Your verification code is: { code } " ,
101100 }
102101
@@ -138,23 +137,23 @@ def get_email_verification_code(user: model.User) -> str:
138137 return user_secret .get_code ()
139138
140139
141- def regenerate_user_secret (user_id : str ) -> str :
140+ def regenerate_user_secret (user_reference : str ) -> str :
142141 """Regenerate the secret for a user.
143142
144143 Args:
145- user_id (str): The id of the user
144+ user_reference (str): The user’s ID or email.
146145
147146 Returns:
148147 str: The new secret
149148 """
150- user = model . User . get ( user_id )
149+ user = get_user_by_username_or_email ( user_reference )
151150
152151 if not user :
153152 raise tk .ObjectNotFound ("User not found" )
154153
155154 user_secret = UserSecret .create_for_user (user .name )
156155
157- log .debug ("2FA: Rotated the 2fa secret for user %s" , user_id )
156+ log .debug ("2FA: Rotated the 2fa secret for user %s" , user . id )
158157
159158 return cast (str , user_secret .secret )
160159
@@ -205,13 +204,16 @@ def authenticate(identity: IdentityDict) -> model.User | model.AnonymousUser | N
205204 if LoginManager .is_login_blocked (identity ["login" ]):
206205 return None
207206
208- if LoginManager .get_user_login_attempts (identity ["login" ]) > config .get_2fa_max_attempts ():
207+ if (
208+ LoginManager .get_user_login_attempts (identity ["login" ])
209+ > auth_config .get_2fa_max_attempts ()
210+ ):
209211 LoginManager .block_user_login (identity ["login" ])
210212
211213 if not ckan_auth_result :
212214 return LoginManager .log_user_login_attempt (identity ["login" ])
213215
214- if not config .is_2fa_enabled ():
216+ if not auth_config .is_2fa_enabled ():
215217 LoginManager .reset_for_user (identity ["login" ])
216218 return ckan_auth_result
217219
@@ -254,3 +256,7 @@ def authenticate_totp(user_name: str) -> str | None:
254256 )
255257 else :
256258 return user_name if result else None
259+
260+
261+ def get_user_by_username_or_email (user_reference : str ) -> model .User | None :
262+ return model .User .get (user_reference ) or model .User .by_email (user_reference )
0 commit comments