Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Monkey Patching with IValidators Implementation #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion ckanext/security/logic/action.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from ckan.plugins.toolkit import (
get_action,
chained_action,
check_access, get_or_bust)
check_access, get_or_bust,
get_validator)
from ckanext.security.authenticator import (
get_user_throttle,
get_address_throttle,
reset_user_throttle,
reset_address_throttle,
reset_totp
)
from ckan.logic.schema import default_update_user_schema


def security_throttle_user_reset(context, data_dict):
Expand Down Expand Up @@ -67,6 +69,10 @@ def user_update(up_func, context, data_dict):
ckanext-security: reset throttling information for updated users
to allow new login attempts after password reset
"""
old_username_validator = get_validator('old_username_validator')
if 'schema' not in context:
context['schema'] = default_update_user_schema()
context['schema']['name'].append(old_username_validator)
rval = up_func(context, data_dict)
get_action('security_throttle_user_reset')(
dict(context, ignore_auth=True), {'user': rval['name']})
Expand Down
25 changes: 12 additions & 13 deletions ckanext/security/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from ckanext.security.logic import auth, action
from ckanext.security.helpers import security_enable_totp
from ckanext.security import validators

from ckanext.security.plugin.flask_plugin import MixinPlugin

Expand All @@ -22,30 +23,28 @@ class CkanSecurityPlugin(MixinPlugin, p.SingletonPlugin):
p.implements(p.IActions)
p.implements(p.IAuthFunctions)
p.implements(p.ITemplateHelpers)
p.implements(p.IValidators, inherit=True)

# BEGIN Hooks for IConfigurer

def update_config(self, config):
define_security_tables() # map security models to db schema

# Monkeypatching all user schemas in order to enforce a stronger
# password policy. I tried monkeypatching
# `ckan.logic.validators.user_password_validator` instead
# without success.
core_schema.default_user_schema = \
ext_schema.default_user_schema
core_schema.user_new_form_schema = \
ext_schema.user_new_form_schema
core_schema.user_edit_form_schema = \
ext_schema.user_edit_form_schema
core_schema.default_update_user_schema = \
ext_schema.default_update_user_schema

tk.add_template_directory(config, '../templates')
tk.add_resource('../fanstatic', 'security')

# END Hooks for IConfigurer

# BEGIN Hooks for IValidators

def get_validators(self):
return {
'user_password_validator': validators.user_password_validator,
'old_username_validator': validators.old_username_validator,
}

# END Hooks for IValidators

# BEGIN Hooks for IResourceController

# CKAN < 2.10
Expand Down