$ pip install djangorestframework-jwt
In settings.py:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
In urls.py:
from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token
urlpatterns = patterns(
# ...
url(r'^api-token-auth/', obtain_jwt_token),
url(r'^api-token-refresh/', refresh_jwt_token)
)
To test:
$ curl -X POST -d "username=admin&password=password123" http://localhost:8000/api-token-auth/
$ curl -X POST -H "Content-Type: application/json" -d '{"username":"admin","password":"password123"}' http://localhost:8000/api-token-auth/
$ curl -H "Authorization: JWT " http://localhost:8000/protected-url/
10.26.2016
DRF JWT Authentication
i.e., Django REST Framework with JSON Web Token Authentication.
Got the solution from here:
http://getblimp.github.io/django-rest-framework-jwt/
http://zqpythonic.qiniucdn.com/data/20141006233346/index.html
10.25.2016
Django LDAP Integration
These posts helped me:
http://kacperdziubek.pl/python/django-ldap-open-directory-integration/
https://pythonhosted.org/django-auth-ldap/
Install:
$ pip install django-auth-ldap
Then in settings.py, add:
import ldap
from django_auth_ldap.config import LDAPSearch
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'django_auth_ldap.backend.LDAPBackend',
)
AUTH_LDAP_SERVER_URI = "ldap://my.appauth.com" # ip or host name of Open Directory server
AUTH_LDAP_BIND_DN = "CN=Accounts,OU=US Security,DC=corp,DC=com"
AUTH_LDAP_BIND_PASSWORD = "MySecurePassword"
AUTH_LDAP_USER_SEARCH = LDAPSearch("OU=US Users,dc=corp,dc=com",
ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")
AUTH_LDAP_CONNECTION_OPTIONS = {
# make search fast
ldap.OPT_REFERRALS: 0
}
AUTH_LDAP_USER_ATTR_MAP = {
"username": "sAMAccountName",
"first_name": "givenName",
"last_name": "sn",
"email": "mail"
}
The mapping above would result into automatically saving the information to the users table.
10.20.2016
Django Notes
1. Backup and seed data using fixtures # Save data $ python manage.py dumpdata --format=json myapp > myapp/fixtures/initial_data.json # Load data $ python manage.py loaddata vlanapi/fixtures/initial_data.json 2. Open python shell - interactive console $ python manage.py shell Python 2.7.12 (default, Jul 1 2016, 15:12:24) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> 3. Start a project $ django-admin startproject mysite 4. Start initial migration $ python manage.py migrate 5. Start server $ python manage.py runserver 6. Create an app $ python manage.py startapp newapp 7. Create and run a migration $ python manage.py makemigrations $ python manage.py migrate
Subscribe to:
Comments (Atom)