Skip to content

Commit 6702515

Browse files
committed
Manually merge PR203 to refactor the validate_auth_attemp helper signature and use slightly in preparation for reuse in account action.
git-svn-id: svn+ssh://svn.code.sf.net/p/migrid/code/trunk@6209 b75ad72c-e7d7-11dd-a971-7dbc132099af
1 parent a0d40af commit 6702515

File tree

2 files changed

+28
-32
lines changed

2 files changed

+28
-32
lines changed

mig/shared/functionality/reqpwresetaction.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# reqpwresetaction - handle account password reset requests and send email to user
7-
# Copyright (C) 2003-2024 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH
88
#
99
# This file is part of MiG.
1010
#
@@ -30,21 +30,18 @@
3030
from __future__ import absolute_import
3131

3232
import os
33-
import tempfile
34-
import time
3533

3634
from mig.shared import returnvalues
37-
from mig.shared.base import canonical_user_with_peers, generate_https_urls, \
38-
fill_distinguished_name, cert_field_map, auth_type_description, \
39-
mask_creds, is_gdp_user
40-
from mig.shared.defaults import keyword_auto, RESET_TOKEN_TTL
41-
from mig.shared.functional import validate_input, REJECT_UNSET
42-
from mig.shared.griddaemons.https import default_max_user_hits, \
43-
default_user_abuse_hits, default_proto_abuse_hits, hit_rate_limit, \
44-
expire_rate_limit, validate_auth_attempt
45-
from mig.shared.handlers import safe_handler, get_csrf_limit
46-
from mig.shared.htmlgen import themed_styles, themed_scripts
47-
from mig.shared.init import initialize_main_variables, find_entry
35+
from mig.shared.base import auth_type_description, canonical_user_with_peers, \
36+
cert_field_map, is_gdp_user, mask_creds
37+
from mig.shared.defaults import RESET_TOKEN_TTL, keyword_auto
38+
from mig.shared.functional import REJECT_UNSET, validate_input
39+
from mig.shared.griddaemons.https import default_proto_abuse_hits, \
40+
default_user_abuse_hits, expire_rate_limit, hit_rate_limit, \
41+
validate_auth_attempt
42+
from mig.shared.handlers import get_csrf_limit, safe_handler
43+
from mig.shared.htmlgen import themed_scripts, themed_styles
44+
from mig.shared.init import initialize_main_variables
4845
from mig.shared.notification import send_email
4946
from mig.shared.pwcrypto import generate_reset_token
5047
from mig.shared.url import urlencode
@@ -125,7 +122,7 @@ def main(client_id, user_arguments_dict):
125122
'class': 'genericbutton', 'text': "Try again"})
126123
return (output_objects, returnvalues.CLIENT_ERROR)
127124

128-
if not auth_type in configuration.site_login_methods:
125+
if auth_type not in configuration.site_login_methods:
129126
output_objects.append({'object_type': 'error_text', 'text':
130127
'You must provide a supported auth_type!'})
131128
output_objects.append(
@@ -141,7 +138,6 @@ def main(client_id, user_arguments_dict):
141138
'class': 'genericbutton', 'text': "Back"})
142139
return (output_objects, returnvalues.CLIENT_ERROR)
143140

144-
mig_user = os.environ.get('USER', 'mig')
145141
client_addr = os.environ.get('REMOTE_ADDR', None)
146142
tcp_port = int(os.environ.get('REMOTE_PORT', '0'))
147143
anon_migoid_url = configuration.migserver_https_sid_url
@@ -165,13 +161,13 @@ def main(client_id, user_arguments_dict):
165161
(authorized, disconnect) = validate_auth_attempt(
166162
configuration,
167163
proto,
168-
op_name,
164+
"passwordreset",
169165
cert_id,
170166
client_addr,
171167
tcp_port,
172168
secret=None,
173169
authtype_enabled=True,
174-
auth_reset=True,
170+
modify_account=True,
175171
exceeded_rate_limit=exceeded_rate_limit,
176172
user_abuse_hits=default_user_abuse_hits,
177173
proto_abuse_hits=default_proto_abuse_hits,
@@ -211,7 +207,7 @@ def main(client_id, user_arguments_dict):
211207
# Registered emails are automatically lowercased
212208
search_filter['email'] = cert_id.lower()
213209
(_, hits) = search_users(search_filter, configuration, keyword_auto, False)
214-
user_dict, password_hash = None, None
210+
user_dict = None
215211
for (uid, user_dict) in hits:
216212
if is_gdp_user(configuration, uid):
217213
logger.debug("skip password reset for gdp sub-user %r" % cert_id)
@@ -227,7 +223,7 @@ def main(client_id, user_arguments_dict):
227223
try:
228224
reset_token = generate_reset_token(configuration, user_dict,
229225
auth_type)
230-
except ValueError as vae:
226+
except ValueError:
231227
logger.info("skip password reset for %r without matching auth" %
232228
cert_id)
233229
continue

mig/shared/griddaemons/auth.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# --- BEGIN_HEADER ---
55
#
66
# auth - grid daemon auth helper functions
7-
# Copyright (C) 2010-2024 The MiG Project lead by Brian Vinter
7+
# Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH
88
#
99
# This file is part of MiG.
1010
#
@@ -27,15 +27,15 @@
2727

2828
""" MiG daemon auth functions"""
2929

30-
import time
3130
import re
31+
import time
3232

3333
from mig.shared.auth import active_twofactor_session
34-
from mig.shared.base import extract_field, expand_openid_alias
34+
from mig.shared.base import expand_openid_alias, extract_field
3535
from mig.shared.defaults import CRACK_USERNAME_REGEX, protocol_aliases
3636
from mig.shared.gdp.all import get_client_id_from_project_client_id
37-
from mig.shared.griddaemons.ratelimits import default_user_abuse_hits, \
38-
default_proto_abuse_hits, default_max_secret_hits, update_rate_limit
37+
from mig.shared.griddaemons.ratelimits import default_max_secret_hits, \
38+
default_proto_abuse_hits, default_user_abuse_hits, update_rate_limit
3939
from mig.shared.griddaemons.sessions import active_sessions
4040
from mig.shared.notification import send_system_notification
4141
from mig.shared.settings import load_twofactor
@@ -187,7 +187,7 @@ def validate_auth_attempt(configuration,
187187
valid_twofa=False,
188188
authtype_enabled=False,
189189
valid_auth=False,
190-
auth_reset=False,
190+
modify_account=False,
191191
exceeded_rate_limit=False,
192192
exceeded_max_sessions=False,
193193
user_abuse_hits=default_user_abuse_hits,
@@ -229,8 +229,8 @@ def validate_auth_attempt(configuration,
229229
% valid_twofa
230230
+ "authtype_enabled: %s, valid_auth: %s\n"
231231
% (authtype_enabled, valid_auth)
232-
+ "auth_reset: %s\n"
233-
% auth_reset
232+
+ "modify_account: %s\n"
233+
% modify_account
234234
+ "exceeded_rate_limit: %s\n"
235235
% exceeded_rate_limit
236236
+ "exceeded_max_sessions: %s\n"
@@ -268,12 +268,12 @@ def validate_auth_attempt(configuration,
268268
or authtype in ["session"]):
269269
pass
270270
elif protocol == 'https' \
271-
and authtype in ["twofactor", "reqpwresetaction"]:
271+
and authtype in ["twofactor", "passwordreset", "accountupdate"]:
272272
pass
273273
elif protocol == 'openid' \
274274
and authtype in configuration.user_openid_auth:
275275
pass
276-
elif not protocol in ['davs', 'ftps', 'sftp', 'sftp-subsys', 'https',
276+
elif protocol not in ['davs', 'ftps', 'sftp', 'sftp-subsys', 'https',
277277
'openid']:
278278
logger.error("Invalid protocol: %r" % protocol)
279279
return (authorized, disconnect)
@@ -395,8 +395,8 @@ def validate_auth_attempt(configuration,
395395
authlog(configuration, 'WARNING', protocol, authtype,
396396
username, ip_addr, auth_msg,
397397
notify=notify, hint=mount_hint)
398-
elif authtype_enabled and auth_reset:
399-
# IMPORTANT: leave unauthorized here to enforce rate limit on resets
398+
elif authtype_enabled and modify_account:
399+
# IMPORTANT: leave unauthorized here for rate limit on resets and renew
400400
authorized = False
401401
auth_msg = "Allow %s" % authtype
402402
log_msg = auth_msg + " for %s from %s" % (username, ip_addr)

0 commit comments

Comments
 (0)