Giter VIP home page Giter VIP logo

django-master-password's Introduction

Build Status Coverage Status Version

Overview

This app provides a mixin class that adds fallback master password authentication to an existing backend, and a ready to use subclass of Django's ModelBackend with master password authentication.

This could be dangerous and is generally not recommended for production, but is super handy for development and staging environments.

In a pinch it can also be used temporarily (with a strong password) to troubleshoot end-user issues in production environments, without having to reset their password.

Installation

Install with pip:

$ pip install django-master-password

Update the AUTHENTICATION_BACKENDS setting:

AUTHENTICATION_BACKENDS += ('master_password.auth.ModelBackend', )

If you want to use the optional make_password management command, update the INSTALLED_APPS setting as well:

INSTALLED_APPS += ('master_password', )

Usage

The MasterPasswordMixin.authenticate() method will first try to authenticate with its superclass, and then it will fallback to master password authentication.

The default implementation authenticates against the MASTER_PASSWORDS setting, which should be a dictionary with clear text or hashed passwords as keys, and callback functions (or None) as values.

A callback function must take a user object as its only argument, and should return True if the user is allowed to authenticate with that password.

For example, you might have one master password that cannot be used for staff or superuser accounts, and another that can be used for any account:

MASTER_PASSWORDS = {
    'user123': lambda u: not u.is_staff and not u.is_superuser,
    'superuser123': None,
}

The use of clear text master passwords is intended as a convenience during development. When DEBUG=False, you must use a strong hashed password with at least 50 characters, 1 digit, 1 uppercase letter, 1 lowercase letter, and 1 non-alphanumeric character:

MASTER_PASSWORDS = {
    'pbkdf2_sha256$'
    '20000$'
    'kGdCcfmJtsUY$'
    'euTmHbJ9sdHirlsM2MvUjHQPDJ6CZdu02gYrxY3aAbI=': None,
}

This is a failsafe against accidentally enabling an unsafe master password for production and staging environments.

You can generate a hashed password in Python:

>>> from django.contrib.auth.hashers import make_password
>>> print make_password('password123')
pbkdf2_sha256$20000$kGdCcfmJtsUY$euTmHbJ9sdHirlsM2MvUjHQPDJ6CZdu02gYrxY3aAbI=

Or use the make_password management command:

(venv)$ ./manage.py make_password
Password:
Hashed password: pbkdf2_sha256$20000$kGdCcfmJtsUY$euTmHbJ9sdHirlsM2MvUjHQPDJ6CZdu02gYrxY3aAbI=

Customising

If you are already using a custom auth backend, use the mixin class to add master password authentication to it. You will need to define a get_user_object(**kwargs) method, which should be the same as the authenticate() method on the superclass but without any password validation.

You can also override the get_master_passwords() method if you want to get master passwords from another source than the MASTER_PASSWORDS setting.

django-master-password's People

Contributors

bhargav95 avatar mrmachine avatar sam-mi avatar variant77 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-master-password's Issues

Unable to use

I would like to enable a master password in my DRF (React.js + Django) website. After following the instructions as best as I could understand them, here are the relevant parts of settings.py:

DEBUG = TRUE

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions',

    # 3rd party apps
    'rest_framework',
    'rest_framework.authtoken',
    'dj_rest_auth',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'dj_rest_auth.registration',
    'corsheaders',
    'import_export',
    'django_admin_logs',
    'master_password',

    # Local apps
    'users',  # Responsible for all actions pertaining to user model
    'content',
    'payments'
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ACCOUNT_AUTHENTICATION_METHOD = 'email'

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
    "master_password.auth.ModelBackend"
)

MASTER_PASSWORDS = {
    'Abc123': None
}

When I try to log into a user account with Abc123 as password, I still get the response {non_field_errors: ["Unable to log in with provided credentials."]}. What am I missing?

P.S. Possibly unrelated, but when I open auth.py in VS Code, it shows the lines of authenticate() beginning with the line password = self.get_password(**kwargs) as unreachable code... This seems like a VS Code bug, but just mentioning in case this could give some clue...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.