django-otp-agents

PyPI Documentation Source

This uses django-agent-trust to add OTP-based machine authorization to django-otp.

See django-otp for more information on the OTP framework.

Installation

This is technically a Django app, but only for testing purposes. To use this project, just make sure you have django-otp and django-agent-trust installed correctly.

Forms

This package provides subclasses of OTPAuthenticationForm and OTPTokenForm with an extra boolean field called otp_trust_agent. The user can check this option to indicate that they’d like to bypass OTP verification when accessing the site from the same browser in the future.

When these forms are used with django.contrib.auth.views.login(), we will call the appropriate django-agent-trust APIs automatically. If the trust option is selected, the agent will be trusted persistently; if not, it will be trusted for the current session. Views willing to accept a trusted agent in lieu of OTP verification may then use trusted_agent_required() in place of otp_required().

class otp_agents.forms.OTPAuthenticationForm(request=None, *args, **kwargs)[source]

Extends OTPAuthenticationForm with support for agent trust.

clean()[source]

Hook for doing any extra form-wide cleaning after Field.clean() has been called on every field. Any ValidationError raised by this method will not be associated with a particular field; it will have a special-case association with the field named ‘__all__’.

property media

Return all media required to render the widgets on this form.

class otp_agents.forms.OTPTokenForm(user, request=None, *args, **kwargs)[source]

Extends OTPTokenForm with support for agent trust.

clean()[source]

Hook for doing any extra form-wide cleaning after Field.clean() has been called on every field. Any ValidationError raised by this method will not be associated with a particular field; it will have a special-case association with the field named ‘__all__’.

property media

Return all media required to render the widgets on this form.

Views

class otp_agents.views.LoginView(**kwargs)[source]

This is just like django_otp.views.LoginView except that it uses our agent-trust-enabled forms.

otp_authentication_form

alias of OTPAuthenticationForm

otp_token_form

alias of OTPTokenForm

Decorators

If you use any OTP-aware authentication forms that are not trusted-agent-aware, then the following decorator may be useful. For example, suppose you have one view protected by otp_required() and another that is protected by trusted_agent_required(). If an authenticated user visits the first view, he will be asked to provide an OTP token via the standard OTPTokenForm, which will verify the user, but will not set up a trusted agent. If he then visits the second view, he will be asked to verify again, this time with the trusted-agent-aware OTPTokenForm. This is clearly not what you intend, since OTP verification should be more than sufficient to authorize the user for the second view.

To get around this, the following decorator effectively merges otp_required() and trusted_agent_required() into a single decorator that will be satisfied with either a verified user or a trusted agent. The default behavior is to act exactly like otp_required(); pass accept_trusted_agent=True to enable the more lenient policy.

otp_agents.decorators.otp_required(view=None, redirect_field_name='next', login_url=None, if_configured=False, accept_trusted_agent=False)[source]

Similar to otp_required(), but with an extra argument.

The default value for login_url depends on the value of accept_trusted_agent. If True, we’ll use AGENT_LOGIN_URL; otherwise, we’ll use OTP_LOGIN_URL.

Parameters:

accept_trusted_agent (bool) – If True, we’ll accept a trusted agent in lieu of OTP verification. Default is False.

Admin

class otp_agents.admin.TrustedAgentAdminSite(name='otpadmin')[source]

This is an OTPAdminSite subclass that is satisfied by a trusted agent.

This is identical to django_otp.admin.OTPAdminSite except that it will skip the login dialog if the user is already logged in and is either verified with an OTP device or is coming from a previously trusted agent.

Note that AdminSite lacks the hooks necessary to properly implement a trusted agent login form here. If the user does not meet the criteria above, they will have to verify themselves with an OTP device to log in. The experience will be better if you send users through your own custom login flow before redirecting them to this admin site.

has_permission(request)[source]

In addition to the default requirements, this only allows access to users who have been verified by a registered OTP device.

Changes

Change Log

License

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.

In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org/>