Skip to content

Commit

Permalink
✨ Multilingualism (#295)
Browse files Browse the repository at this point in the history
Parent issue: sequentech/meta#122
  • Loading branch information
Findeton authored and edulix committed Sep 6, 2023
1 parent 5998828 commit 2c8549f
Show file tree
Hide file tree
Showing 11 changed files with 640 additions and 6 deletions.
21 changes: 20 additions & 1 deletion iam/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
'check': "iterate-list",
'check-list': [
{
'check': 'dict-keys-exact',
'check': 'dict-keys-exist',
'keys': ['event_id', 'title']
},
{
Expand Down Expand Up @@ -210,6 +210,25 @@
'range': [1, 254]
}
]
},
{
'check': 'index-check-list',
'index': 'title_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.values()])
},
]
}
]
}
Expand Down
96 changes: 94 additions & 2 deletions iam/authmethods/m_dnie.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@
# along with iam. If not, see <http://www.gnu.org/licenses/>.

import json
import logging
from . import register_method
from django.shortcuts import get_object_or_404, redirect
from django.conf.urls import url
from django.http import Http404

from authmethods.utils import check_pipeline, give_perms
from authmethods.utils import (
check_pipeline,
give_perms,
stack_trace_str
)

from api.models import AuthEvent

from utils import json_response

from contracts.base import check_contract, JsonTypeEncoder
from contracts import CheckException

LOGGER = logging.getLogger('iam')

def testview(request):
req = request.GET
Expand Down Expand Up @@ -112,6 +121,70 @@ class DNIE:
)
dni_definition = { "name": "dni", "type": "text", "required": True, "min": 2, "max": 200, "required_on_authentication": True }

CONFIG_CONTRACT = [
{
'check': 'isinstance',
'type': dict
},
{
'check': 'index-check-list',
'index': 'msg_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 200 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'subject_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 1024 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'html_message_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 5000 for k in d.values()])
},
]
}
]

def error(
self, msg, auth_event=None, error_codename=None, internal_error=None
):
Expand All @@ -135,7 +208,26 @@ def authenticate(self, ae, request):
return d

def check_config(self, config):
return ''
""" Check config when create auth-event. """
if config is None:
return ''
try:
check_contract(self.CONFIG_CONTRACT, config)
LOGGER.debug(\
"Dnie.check_config success\n"\
"config '%r'\n"\
"returns ''\n"\
"Stack trace: \n%s",\
config, stack_trace_str())
return ''
except CheckException as e:
LOGGER.error(\
"Dnie.check_config error\n"\
"error '%r'\n"\
"config '%r'\n"\
"Stack trace: \n%s",\
e.data, config, stack_trace_str())
return json.dumps(e.data, cls=JsonTypeEncoder)

def census(self, ae, request):
req = json.loads(request.body.decode('utf-8'))
Expand Down
57 changes: 57 additions & 0 deletions iam/authmethods/m_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,63 @@ class Email:
'check': 'dict-keys-exist',
'keys': ['msg', 'subject', 'registration-action', 'authentication-action']
},
{
'check': 'index-check-list',
'index': 'msg_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 200 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'subject_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 1024 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'html_message_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 5000 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'msg',
Expand Down
57 changes: 57 additions & 0 deletions iam/authmethods/m_email_otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,44 @@ class EmailOtp:
'check': 'dict-keys-exist',
'keys': ['msg', 'subject', 'registration-action', 'authentication-action']
},
{
'check': 'index-check-list',
'index': 'msg_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 200 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'subject_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 1024 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'msg',
Expand Down Expand Up @@ -162,6 +200,25 @@ class EmailOtp:
}
]
},
{
'check': 'index-check-list',
'index': 'html_message_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 5000 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'subject',
Expand Down
88 changes: 87 additions & 1 deletion iam/authmethods/m_emailpwd.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
authenticate_otl
)

from contracts.base import check_contract, JsonTypeEncoder
from contracts import CheckException

LOGGER = logging.getLogger('iam')


Expand Down Expand Up @@ -85,8 +88,91 @@ class EmailPassword:
"required_on_authentication": True
}

CONFIG_CONTRACT = [
{
'check': 'isinstance',
'type': dict
},
{
'check': 'index-check-list',
'index': 'msg_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 200 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'subject_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 1024 for k in d.values()])
},
]
},
{
'check': 'index-check-list',
'index': 'html_message_i18n',
'optional': True,
'check-list': [
{
'check': 'isinstance',
'type': dict
},
{ # keys are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) for k in d.keys()])
},
{ # values are strings
'check': 'lambda',
'lambda': lambda d: all([isinstance(k, str) and len(k) > 0 and len(k) <= 5000 for k in d.values()])
},
]
}
]

def check_config(self, config):
return ''
""" Check config when create auth-event. """
if config is None:
return ''
try:
check_contract(self.CONFIG_CONTRACT, config)
LOGGER.debug(\
"EmailPwd.check_config success\n"\
"config '%r'\n"\
"returns ''\n"\
"Stack trace: \n%s",\
config, stack_trace_str())
return ''
except CheckException as e:
LOGGER.error(\
"EmailPwd.check_config error\n"\
"error '%r'\n"\
"config '%r'\n"\
"Stack trace: \n%s",\
e.data, config, stack_trace_str())
return json.dumps(e.data, cls=JsonTypeEncoder)

def census(self, auth_event, request):
req = json.loads(request.body.decode('utf-8'))
Expand Down
Loading

0 comments on commit 2c8549f

Please sign in to comment.