Skip to content

Commit

Permalink
Merge pull request #3 from keitaroinc/locked-user
Browse files Browse the repository at this point in the history
locked time from config
  • Loading branch information
miloshIra authored Sep 1, 2023
2 parents 2138bb7 + 25f1607 commit 8626e20
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
19 changes: 16 additions & 3 deletions ckanext/password_policy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def user_login_count(username):
redis_conn = connect_to_redis()
user_cached = redis_conn.get(username)
if user_cached == None:
expiry = config.get('ckan.password_policy.user_locked_time', 600)
expiry = config.get('ckanext.password_policy.user_locked_time', 600)
# user will be cached in redis with count 1
redis_conn.set(username, 1, ex=expiry)
else:
Expand Down Expand Up @@ -38,7 +38,7 @@ def custom_password_check(password):
1 uppercase letter or more
1 lowercase letter or more
"""
password_length = int(config.get('ckan.password_policy.password_length', 12))
password_length = int(config.get('ckanext.password_policy.password_length', 12))
# calculating the length
length_error = len(password) < password_length

Expand All @@ -64,4 +64,17 @@ def custom_password_check(password):
'uppercase_error': uppercase_error,
'lowercase_error': lowercase_error,
'symbol_error': symbol_error,
}
}

def lockout_time():
lockout = config.get('ckanext.password_policy.user_locked_time')

time_to_int = int(lockout)

if time_to_int >= 60:
time_in_minutes = time_to_int//60
alert = f" You failed 3 atempts to login and you have been locked out for {time_in_minutes} minutes. Try again later."
return alert
else:
alert = f"You failed 3 atempts to login and you have been locked out for {time_to_int} seconds. Try again later."
return alert
6 changes: 5 additions & 1 deletion ckanext/password_policy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Missing = df.Missing
missing = df.missing

password_length = config.get('ckan.password_policy.password_length')
password_length = config.get('ckanext.password_policy.password_length')


def user_custom_password_validator(key, data, errors, context):
Expand All @@ -34,6 +34,7 @@ class PasswordPolicyPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IValidators)
plugins.implements(plugins.IBlueprint)
plugins.implements(plugins.IAuthenticator, inherit=True)
plugins.implements(plugins.ITemplateHelpers)



Expand All @@ -52,3 +53,6 @@ def get_blueprint(self):
return views.get_blueprints()


def get_helpers(self):
return {'lockout_time': h.lockout_time}

7 changes: 4 additions & 3 deletions ckanext/password_policy/templates/user/locked.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
{% endblock %}

{% block primary_content %}
<div>
<h3>{{ 'User locked. Please try again in 15 minutes.' }}</h3>
</div>

<br>
{{ h.lockout_time() }}

{% endblock %}

{% block secondary_content %}
Expand Down
8 changes: 6 additions & 2 deletions ckanext/password_policy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _get_form_password(self):
password1 = request.form.get(u'password1')
password2 = request.form.get(u'password2')

password_length = config.get('ckan.password_policy.password_length')
password_length = config.get('ckanext.password_policy.password_length')

valid_pass = helper.custom_password_check(password1)
if valid_pass['password_ok']==False:
Expand All @@ -128,7 +128,7 @@ def identify(self, environ):
the ``environ``.
'''
allowed_failes_logins = int(config.get('ckan.password_policy.failed_logins', 3))
allowed_failes_logins = int(config.get('ckanext.password_policy.failed_logins', 3))
request = Request(environ, charset=self.charset)

path_info = environ[u'PATH_INFO']
Expand Down Expand Up @@ -253,7 +253,11 @@ def logged_in():

def locked_user():

alert = helper.lockout_time()


extra_vars = {}
extra_vars['alert'] = alert
return base.render(u'user/locked.html', extra_vars)


Expand Down

0 comments on commit 8626e20

Please sign in to comment.