Mercurial > p > roundup > code
view roundup/cgi/TranslationService.py @ 8411:ef1ea918b07a reauth-confirm_id
feat(security): Add user confirmation/reauth for sensitive changes
Auditors can raise Reauth(reason) exception to require the user to
enter a token (e.g. account password) to verify the user is performing
the change.
Naming is subject to change.
actions.py: New ReauthAction class handler and verifyPassword() method
for overriding if needed.
client.py: Handle Reauth exception by calling Client:reauth() method.
Default client:reauth method. Add 'reauth' action declaration.
exceptions.py: Define and document Reauth exception as a subclass of
RoundupCGIException.
templating.py: Define method utils.embed_form_fields().
The original form making a change to the database has a lot of form
fields. These need to be resubmitted to Roundup as part of the form
submission that verifies the user's password.
This method turns all non file form fields into type=hidden inputs.
It escapes the names and values to prevent XSS.
For file form fields, it base64 encodes the contents and puts them
in hidden pre blocks. The pre blocks have data attributes for the
filename, filetype and the original field name. (Note the original
field name is not used.)
This stops the file content data (maybe binary e.g. jpegs) from
breaking the html page. The reauth template runs JavaScript that
turns the encoded data inside the pre tags back into a file. Then
it adds a multiple file input control to the page and attaches all
the files to it. This file input is submitted with the rest of the
fields.
_generic.reauth.html (multiple tracker templates): Generates a form
with id=reauth_form to:
display any message from the Reauth exception to the user (e.g. why
user is asked to auth).
get the user's password
submit the form
embed all the form data that triggered the reauth
recreate any file data that was submitted as part of the form and
generate a new file input to push the data to the back end
It has the JavaScript routine (as an IIFE) that regenerates a file
input without user intervention.
All the TAL based tracker templates use the same form. There is also
one for the jinja2 template. The JavaScript for both is the same.
reference.txt: document embed_form_fields utility method.
upgrading.txt: initial upgrading docs.
TODO:
Finalize naming. I am leaning toward ConfirmID rather than Reauth.
Still looking for a standard name for this workflow.
Externalize the javascript in _generic.reauth.html to a seperate file
and use utils.readfile() to embed it or change the script to load it
from a @@file url.
Clean up upgrading.txt with just steps to implement and less feature
detail/internals.
Document internals/troubleshooting in reference.txt.
Add tests using live server.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 11 Aug 2025 14:01:12 -0400 |
| parents | 07ce4e4110f5 |
| children |
line wrap: on
line source
# TranslationService for Roundup templates # # This module is free software, you may redistribute it # and/or modify under the same terms as Python. # # This module provides National Language Support # for Roundup templating - much like roundup.i18n # module for Roundup command line interface. # The only difference is that translator objects # returned by get_translation() have one additional # method which is used by TAL engines: # # translate(domain, msgid, mapping, context, target_language, default) # from roundup import i18n from roundup.cgi.PageTemplates import Expressions, PathIterator, TALES from roundup.cgi.TAL import TALInterpreter from roundup.anypy.strings import us2u, u2s ### Translation classes class TranslationServiceMixin: def translate(self, domain, msgid, mapping=None, context=None, target_language=None, default=None): _msg = self.gettext(msgid) # print ("TRANSLATE", msgid, _msg, mapping, context) _msg = TALInterpreter.interpolate(_msg, mapping) return _msg if hasattr(i18n.RoundupTranslations, 'ugettext'): def gettext(self, msgid): msgid = us2u(msgid) msgtrans = self.ugettext(msgid) return u2s(msgtrans) def ngettext(self, singular, plural, number): singular = us2u(singular) plural = us2u(plural) msgtrans = self.ungettext(singular, plural, number) return u2s(msgtrans) class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations): pass class NullTranslationService(TranslationServiceMixin, i18n.RoundupNullTranslations): if hasattr(i18n.RoundupNullTranslations, 'ugettext'): def ugettext(self, message): if self._fallback: return self._fallback.ugettext(message) # Sometimes the untranslatable message is a UTF-8 encoded string # (thanks to PageTemplate's internals). message = us2u(message) return message ### TAL patching # # Template Attribute Language (TAL) uses only global translation service, # which is not thread-safe. We will use context variable 'i18n' # to access request-dependent transalation service (with domain # and target language set during initializations of the roundup # client interface. # class Context(TALES.Context): def __init__(self, compiler, contexts): TALES.Context.__init__(self, compiler, contexts) if not self.contexts.get('i18n', None): # if the context contains no TranslationService, # create default one self.contexts['i18n'] = get_translation() self.i18n = self.contexts['i18n'] def translate(self, domain, msgid, mapping=None, context=None, target_language=None, default=None): if context is None: context = self.contexts.get('here') return self.i18n.translate(domain, msgid, mapping=mapping, context=context, default=default, target_language=target_language) class Engine(TALES.Engine): def getContext(self, contexts=None, **kwcontexts): if contexts is not None: if kwcontexts: kwcontexts.update(contexts) else: kwcontexts = contexts return Context(self, kwcontexts) # patching TAL like this is a dirty hack, # but i see no other way to specify different Context class Expressions._engine = Engine(PathIterator.Iterator) Expressions.installHandlers(Expressions._engine) ### main API function def get_translation(language=None, tracker_home=None, translation_class=TranslationService, null_translation_class=NullTranslationService): """Return Translation object for given language and domain Arguments 'translation_class' and 'null_translation_class' specify the classes that are instantiated for existing and non-existing translations, respectively. """ return i18n.get_translation(language=language, tracker_home=tracker_home, translation_class=translation_class, null_translation_class=null_translation_class) # vim: set et sts=4 sw=4 :
