Enable token authentication.

This commit is contained in:
RecursiveGreen 2018-03-28 21:12:28 -04:00
parent 862bf63d6e
commit 3b62bc7b03
2 changed files with 26 additions and 0 deletions

View file

@ -1,5 +1,6 @@
# Generated by Django 2.0 on 2017-12-27 21:22 # Generated by Django 2.0 on 2017-12-27 21:22
import binascii
import os import os
from django.conf import settings from django.conf import settings
@ -11,6 +12,7 @@ from core.utils import generate_password
def create_dj_user(apps, schema_editor): def create_dj_user(apps, schema_editor):
User = apps.get_model('core', 'RadioUser') User = apps.get_model('core', 'RadioUser')
Token = apps.get_model('authtoken', 'Token')
db_alias = schema_editor.connection.alias db_alias = schema_editor.connection.alias
new_password = generate_password() new_password = generate_password()
@ -22,10 +24,17 @@ def create_dj_user(apps, schema_editor):
is_dj=True) is_dj=True)
dj.save(using=db_alias) dj.save(using=db_alias)
# Since 'post_save' is impervious to migration scripts, this is pulled
# directly from the authtoken code for key generation. Otherwise, key will
# be blank.
token = Token(key=binascii.hexlify(os.urandom(20)).decode(), user=dj)
token.save(using=db_alias)
with open(os.path.join(settings.PROJECT_DIR, '.djinfo'), 'w') as f: with open(os.path.join(settings.PROJECT_DIR, '.djinfo'), 'w') as f:
f.write('Email: {}\n'.format(dj.email)) f.write('Email: {}\n'.format(dj.email))
f.write('Name: {}\n'.format(dj.name)) f.write('Name: {}\n'.format(dj.name))
f.write('Password: {}\n'.format(new_password)) f.write('Password: {}\n'.format(new_password))
f.write('Token: {}\n'.format(token.key))
class Migration(migrations.Migration): class Migration(migrations.Migration):

View file

@ -39,6 +39,7 @@ INSTALLED_APPS = [
'authtools', 'authtools',
'rest_framework', 'rest_framework',
'rest_framework.authtoken',
'core.apps.CoreConfig', 'core.apps.CoreConfig',
'radio.apps.RadioConfig', 'radio.apps.RadioConfig',
@ -92,6 +93,22 @@ USE_TZ = True
WSGI_APPLICATION = 'savepointradio.wsgi.application' WSGI_APPLICATION = 'savepointradio.wsgi.application'
#
# Django Rest Framework settings
#
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'PAGE_SIZE': 100,
}
# #
# Radio-specific settings # Radio-specific settings
# #