Skip to content

Commit 69a3043

Browse files
committed
Initial code release
0 parents  commit 69a3043

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1686
-0
lines changed

.bandit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[bandit]
2+
exclude: tests

.editorconfig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 4
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
charset = utf-8
11+
end_of_line = lf
12+
13+
[*.{json,yml,yaml,js,jsx}]
14+
indent_size = 2
15+
16+
[*.{html,htm}]
17+
indent_size = 2
18+
19+
[LICENSE]
20+
insert_final_newline = false
21+
22+
[Makefile]
23+
indent_style = tab

.fussyfox.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- bandit
2+
- flake8
3+
- isort
4+
- pydocstyle

.gitignore

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
env/
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
*.egg-info/
23+
.installed.cfg
24+
*.egg
25+
26+
# PyInstaller
27+
# Usually these files are written by a python script from a template
28+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
29+
*.manifest
30+
*.spec
31+
32+
# Installer logs
33+
pip-log.txt
34+
pip-delete-this-directory.txt
35+
36+
# Unit test / coverage reports
37+
htmlcov/
38+
.tox/
39+
.coverage
40+
.coverage.*
41+
.cache
42+
nosetests.xml
43+
coverage.xml
44+
*,cover
45+
46+
# Translations
47+
*.mo
48+
*.pot
49+
50+
# Django stuff:
51+
*.log
52+
db.sqlite3
53+
54+
# Sphinx documentation
55+
docs/_build/
56+
57+
# PyBuilder
58+
target/
59+
60+
61+
62+
tests/local.py
63+
docs/_build/
64+
venv
65+
.python-version

.travis.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
language: python
2+
dist: xenial
3+
python:
4+
- "3.6"
5+
- "3.7"
6+
env:
7+
- DJANGO=20
8+
- DJANGO=21
9+
- DJANGO=22
10+
- DJANGO=master
11+
12+
matrix:
13+
fast_finish: true
14+
allow_failures:
15+
- env: DJANGO=master
16+
17+
install: pip install tox-travis codecov
18+
before_script:
19+
- |
20+
if [[ -z $TOXENV ]]; then
21+
export TOXENV=py$(echo $TRAVIS_PYTHON_VERSION | sed -e 's/\.//g')-dj$DJANGO
22+
fi
23+
- echo $TOXENV
24+
script: tox
25+
after_success: codecov
26+
27+
jobs:
28+
include:
29+
- python: "3.7"
30+
env: TOXENV=docs
31+
- stage: deploy
32+
if: tag IS present
33+
python: "3.7"
34+
install: skip
35+
script: skip
36+
after_success: true
37+
deploy:
38+
provider: pypi
39+
user: codingjoe
40+
distributions: sdist bdist_wheel
41+
on:
42+
tags: true
43+
password:

CONTRIBUTING.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
============
2+
Contributing
3+
============
4+
5+
To install the development requirements simply run::
6+
7+
python setup.py develop
8+
9+
To run test suite run::
10+
11+
python setup.py test
12+
13+
... and to run the entire test suite, simply use tox::
14+
15+
pip install --upgrade tox
16+
tox
17+
18+
To build the documentation run::
19+
20+
python setup.py build_sphinx
21+
open docs/_build/html/index.html

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Johannes Hoppe
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

MANIFEST.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include *.rst
2+
recursive-include mailauth/templates *
3+
recursive-include mailauth/contrib/admin/templates *
4+
recursive-include mailauth/contrib/admin/static *
5+
recursive-include mailauth/contrib/admin/locale *
6+
prune tests

README.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
================
2+
Django Mail Auth
3+
================
4+
5+
|version| |ci| |coverage| |license|
6+
7+
.. figure:: sample.png
8+
:width: 425
9+
:alt: screenshot from a login form
10+
11+
Django Mail Auth is a lightweight authentication backend for Django,
12+
that does not require users to remember passwords.
13+
14+
Django Mail Auth features:
15+
16+
- custom user model support
17+
- drop in Django admin support
18+
- drop in Django User replacement
19+
- extendable SMS support
20+
21+
This project was originally inspired by `Is it time for password-less login?`__ by `Ben Brown`_.
22+
23+
__ http://notes.xoxco.com/post/27999787765/is-it-time-for-password-less-login
24+
.. _`Ben Brown`: http://twitter.com/benbrown
25+
26+
Installation
27+
------------
28+
29+
Run this command to install ``django-mail-auth``::
30+
31+
pip install django-mail-auth
32+
33+
34+
Setup
35+
-----
36+
37+
First add `mailauth` to you installed apps::
38+
39+
INSTALLED_APPS = [
40+
# Django's builtin apps…
41+
42+
'mailauth',
43+
'mailauth.contrib.admin', # optional
44+
'mailauth.contrib.auth', # optional
45+
46+
# other apps…
47+
]
48+
49+
`mailauth.contrib.admin` is optional and will replace the admin's login
50+
with token based authentication too.
51+
52+
`mailauth.contrib.auth` is optional and provides a new Django User model.
53+
The new User model needs to be enabled via the ``AUTH_USER_MODEL`` setting::
54+
55+
AUTH_USER_MODEL = 'mailauth.EmailUser'
56+
57+
Next you will need to add the new authentication backend::
58+
59+
AUTHENTICATION_BACKENDS = (
60+
# default, but now optional
61+
# This should be removed if
62+
'django.contrib.auth.backends.ModelBackend',
63+
64+
# The new access token based authentication backend
65+
'mailauth.backends.MailAuthBackend',
66+
)
67+
68+
Django's `ModelBackend` is only only needed, if you still want to support
69+
password based authentication. If you don't, simply remove it from the list.
70+
71+
Last but not least, go to your URL root config `urls.py` and add the following::
72+
73+
from django.urls import path
74+
75+
76+
urlpatterns = [
77+
path('accounts/', include('mailauth.urls')),
78+
]
79+
80+
That's it!
81+
82+
.. note:: Don't forget to setup you Email backend!
83+
84+
.. |version| image:: https://img.shields.io/pypi/v/django-mail-auth.svg
85+
:target: https://pypi.python.org/pypi/django-mail-auth/
86+
.. |ci| image:: https://travis-ci.com/codingjoe/django-mail-auth.svg?branch=master
87+
:target: https://travis-ci.com/codingjoe/django-mail-auth
88+
.. |coverage| image:: https://codecov.io/gh/codingjoe/django-mail-auth/branch/master/graph/badge.svg
89+
:target: https://codecov.io/gh/codingjoe/django-mail-auth
90+
.. |license| image:: https://img.shields.io/badge/license-APL2-blue.svg
91+
:target: LICENSE

docs/conf.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""Sphinx configuration file."""
2+
import os
3+
import sys
4+
5+
import django
6+
from pkg_resources import get_distribution
7+
8+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.testapp.settings")
9+
sys.path.insert(0, os.path.abspath('..'))
10+
django.setup()
11+
12+
project = "Django Mail Auth"
13+
copyright = "2019, Johannes Hoppe"
14+
release = get_distribution('django-mail-user').version
15+
version = '.'.join(release.split('.')[:2])
16+
17+
master_doc = 'index'
18+
19+
extensions = [
20+
'sphinx.ext.autodoc',
21+
'sphinx.ext.napoleon',
22+
'sphinx.ext.intersphinx',
23+
'sphinx.ext.doctest',
24+
]
25+
26+
intersphinx_mapping = {
27+
'python': ('https://docs.python.org/3', None),
28+
'django': ('https://docs.djangoproject.com/en/stable/',
29+
'https://docs.djangoproject.com/en/stable/_objects/'),
30+
}

docs/contributing.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.. include:: ../CONTRIBUTING.rst

docs/customizing.rst

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
===========
2+
Customizing
3+
===========
4+
5+
Django Mail Auth can be easily extend. Besides template adaptations it is
6+
possible to send send different messages like SMS. To make those changes you
7+
will need to write a custom login form.
8+
9+
Custom login form
10+
-----------------
11+
12+
Custom login forms need to inherit from :class:`.BaseLoginForm` and override
13+
the :meth:`save<.BaseLoginForm.save>` method.
14+
15+
The following example is for a login SMS via twilio. This will require a
16+
custom user model with a unique ``phone_number`` field::
17+
18+
from django import forms
19+
from django.contrib.auth import get_user_model
20+
from django.template import loader
21+
from mailauth.forms import BaseLoginForm
22+
23+
24+
class SmsLoginForm(BaseLoginForm):
25+
phone_number = forms.CharField()
26+
27+
template_name = 'registration/login_sms.txt
28+
from_number = None
29+
30+
def __init__(self, *args, **kwargs):
31+
self.twilio_client = TwilioRestClient(
32+
settings.TWILIO_SID,
33+
settings.TWILIO_AUTH_TOKEN
34+
)
35+
super().__init__(*args, **kwargs)
36+
37+
def save(self):
38+
phone_number = self.cleaned_data['phone_number']
39+
user = get_user_model().objects.get(
40+
phone_number=phone_number
41+
)
42+
context = self.get_context(self.request, user)
43+
44+
from_number = self.from_number or getattr(
45+
settings, 'DEFAULT_FROM_NUMBER'
46+
)
47+
sms_content = loader.render_to_string(
48+
self.template_name, context
49+
)
50+
51+
self.twilio_client.messages.create(
52+
to=user.phone_number,
53+
from_=from_number,
54+
body=sms_content
55+
)
56+
57+
58+
To add the new login form, simply add a new login view to your URL config with
59+
the custom form::
60+
61+
from django.urls import path
62+
from mailauth.views import LoginView
63+
64+
from .forms import SmsLoginForm
65+
66+
urlpatterns = [
67+
path(
68+
'login/sms/',
69+
LoginView.as_view(form_class=SmsLoginForm),
70+
name='login-sms'
71+
),
72+
]
73+
74+
API documentation
75+
-----------------
76+
77+
.. autoclass:: mailauth.forms.BaseLoginForm
78+
:members:

0 commit comments

Comments
 (0)