djangosnippets.org: Latest snippets tagged with 'auth'https://djangosnippets.org/tags/auth/2019-06-13T03:46:44.351320-05:00Dry basic-auth middleware using Django's AUTHENTICATION_BACKENDS and "basicauth" lib
2019-06-13T03:46:44.351320-05:00pakalhttps://djangosnippets.org/snippets/10671/<p>Requires to install "basicauth" package, which does basic-auth header encoding/decoding cleanly according to RFCs. </p>
<p>Could be improved to return a "realm" in case of http401, like in https://djangosnippets.org/snippets/1720/, although I'm not sure it's really useful in django usecases.</p>
Freely redistributableAuth decorators with 403 - Django 1.11
2018-05-23T13:37:07.609604-05:00raphaelfvhttps://djangosnippets.org/snippets/10648/<p>This decorator is based on user_passes_test, but when a user is logged in and fails the test, it will render a 403 error instead of redirecting to login - only anonymous users will be asked to login. Version working with Django 1.11, based on version found on [https://djangosnippets.org/snippets/254/] , which …</p>
Freely redistributableDjango Auth with JWT
2017-08-07T12:13:40.828318-05:00techiediarieshttps://djangosnippets.org/snippets/10618/<p>This is an example of Django auth with JWT tokens, you can find how to add <a href="https://www.techiediaries.com/django-rest-framework-jwt-tutorial/">jwt auth to Django Rest Framework in this tutorial</a></p>
Freely redistributableEmail authentication backend
2016-01-04T08:42:03.576173-06:00benjaominghttps://djangosnippets.org/snippets/10547/<p>Fixed minimal version, works with Django 1.7+, tested on Django 1.9.</p>
<p>Add the following to your settings:</p>
<pre><code>AUTHENTICATION_BACKENDS = [
'project.backends.UserModelEmailBackend', # Login w/ email
'django.contrib.auth.backends.ModelBackend', # Login w/ username
]
</code></pre>
Freely redistributable@group_required decorator
2015-07-09T14:46:33.820390-05:00sebastien247https://djangosnippets.org/snippets/10508/<p>Use :</p>
<p><code>@group_required(('toto', 'titi'))</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p><code>@group_required('toto')</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p>Note that group_required() also takes an optional login_url parameter
<code>@group_required('toto', login_url='/loginpage/')</code></p>
<p><code>def my_view(request):</code></p>
<p><code>...</code></p>
<p>As in the login_required() decorator, login_url defaults to settings.LOGIN_URL.</p>
<p>If the raise_exception parameter is given, the decorator will raise PermissionDenied, prompting the 403 (HTTP Forbidden) …</p>
Freely redistributableEmail Auth
2014-04-26T13:34:40.569149-05:00petzahhttps://djangosnippets.org/snippets/3043/<p>Yet another authentication by email address. This one is quick and dirty as we are saving email address in both Username and Email fields. For proper way how to deal with it see</p>
<p>https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#auth-custom-user</p>
Freely redistributableanonymous_required decorator
2013-07-04T07:13:22.246922-05:00yettyhttps://djangosnippets.org/snippets/2969/<p>Decorator to preserve view from logged users.</p>
Freely redistributableRegistering Facebook user by username before loggin in
2013-06-12T12:26:44.307852-05:00zjorhttps://djangosnippets.org/snippets/2927/<p>This snippet allows to create user in django auth system without logging in. After this user logs in via Facebook account social user is created and bound to existing user.</p>
Freely redistributableUse django-social-auth & Google Accounts for admin login
2012-12-04T09:27:39.814163-06:00pmdarrowhttps://djangosnippets.org/snippets/2856/<ol>
<li>Create an app and place this in <code>admin.py</code>. </li>
<li>Add <code>url(r'^login/$', 'social_auth.views.auth', {'backend': 'google'}, name='login')</code> to your <code>urls.py</code>.</li>
<li>Add the app to your <code>INSTALLED_APPS</code> after <code>django.contrib.admin</code>. </li>
<li>Set <code>USE_SOCIAL_AUTH_AS_ADMIN_LOGIN = True</code> in your <code>settings.py</code>.</li>
<li>...</li>
<li>Profit.</li>
</ol>
Freely redistributableShow logged users - keeping track of users login and logout
2012-09-05T15:02:18.391108-05:00albertorcfhttps://djangosnippets.org/snippets/2808/<p>Showing a list of logged users using the <em>user_logged_in</em> and <em>user_logged_out</em> signals.</p>
<p>See <a href="https://docs.djangoproject.com/en/1.4/topics/auth/#login-and-logout-signals">login and logout signals</a> in Django docs.</p>
Freely redistributableUser groups template tag with "else" block support (Allow checking several groups)
2012-04-29T06:44:33.301246-05:00br0th3rhttps://djangosnippets.org/snippets/2736/<p>An "if-style" template tag that checks to see if a user belongs to a one or mores groups (by name).</p>
<p>Usage:
<code>{% ifusergroup Admins %} ... {% endifusergroup %}
or
{% ifusergroup Admins Clients Programmers Managers %} ... {% else %} ... {% endifusergroup %}</code></p>
Freely redistributableCustom requests auth class for Tastypie API key authentication
2012-03-30T05:52:25.098864-05:00jezdezhttps://djangosnippets.org/snippets/2727/<p>In case you ever use <a href="http://python-requests.org/">requests</a> (or <a href="http://slumber.in/">slumber</a>) to do requests against a Tastypie API that requires API key authentication, this small custom auth class will help you.</p>
<p>Use it like that (with requests):</p>
<pre><code>auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252')
response = requests.get('http://api.foo.bar/v1/spam/', auth=auth)
</code></pre>
<p>or with slumber:</p>
<pre><code>auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252') …</code></pre>
Freely redistributabledjango piston use origin django auth
2012-01-31T00:30:43.295851-06:00lettoohttps://djangosnippets.org/snippets/2669/<p>Django-piston have two build-in authentication handlers, the HttpBasicAuthentication and OAuthAuthentication. This snippet give another choice which use the django auth. It can support ajax and normal request.</p>
Freely redistributablestaff_or_404 Decorator
2011-11-10T17:28:25.334468-06:00bkeatinghttps://djangosnippets.org/snippets/2599/<p>Sometimes I don't want to reveal a staff-only view so I created this decorator, using <code>django.contrib.admin.views.decorators.staff_member_required</code> as my boilerplate. Non staff members are kicked to the 404 curb.</p>
<p>Suggestion: Create a file, <code>decorators.py</code> in your project (or single app) and import like so: <code>from myproject.app_name.decorators import staff_or_404</code>.</p>
Freely redistributableSupport for permissions for anonymous users in django ModelBackend
2011-11-08T18:25:59.216496-06:00jbhttps://djangosnippets.org/snippets/2594/<pre><code>Model backend that enables permissions for AnonymusUsers.
I wanted it to be as simple as possible so anonymous users just forward their permission checks
to some fixed user model. This instance can be edited via django admin, assigned to groups, etc.
To control which user will represent anonymous user you …</code></pre>
Freely redistributable