├── .ebextensions ├── db-migrate.config ├── django.config ├── environment.config ├── loadbalancer.config ├── packages.config └── rds.config ├── .elasticbeanstalk └── config.yml └── dashboard-auth ├── README.md ├── core ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py ├── custom └── jwt.py ├── enterprise_users ├── __init__.py ├── admin.py ├── apps.py ├── forms.py ├── management │ └── commands │ │ └── create_superuser.py ├── migrations │ ├── 0001_initial.py │ └── __init__.py ├── models.py ├── serializers.py ├── tests.py └── views.py ├── manage.py ├── requirements.pip ├── requirements.txt └── setup.sh /.ebextensions/db-migrate.config: -------------------------------------------------------------------------------- 1 | container_commands: 2 | 01_collectstatic: 3 | command: 'python manage.py collectstatic --settings=core.settings --noinput' 4 | 01_migrate: 5 | command: 'python manage.py makemigrations' 6 | leader_only: true 7 | 02_migrate: 8 | command: 'python manage.py migrate auth --noinput' 9 | leader_only: true 10 | 03_migrate: 11 | command: 'python manage.py migrate enterprise_users --noinput' 12 | leader_only: true 13 | 04_migrate: 14 | command: 'python manage.py migrate --noinput' 15 | leader_only: true 16 | 01_create_superuser: 17 | command: "python manage.py create_superuser" 18 | leader_only: true 19 | option_settings: 20 | aws:elasticbeanstalk:application:environment: 21 | DJANGO_SETTINGS_MODULE: core.settings 22 | -------------------------------------------------------------------------------- /.ebextensions/django.config: -------------------------------------------------------------------------------- 1 | option_settings: 2 | aws:elasticbeanstalk:container:python: 3 | WSGIPath: core/wsgi.py 4 | "aws:elasticbeanstalk:container:python:staticfiles": 5 | "/static/": "static/" 6 | 7 | container_commands: 8 | 01_wsgipass: 9 | command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf' 10 | -------------------------------------------------------------------------------- /.ebextensions/environment.config: -------------------------------------------------------------------------------- 1 | option_settings: 2 | 3 | - option_name: SUPER_USER_NAME 4 | value: sharkroot2 5 | - option_name: SUPER_USER_PASSWORD 6 | value: sharkroot2 7 | - option_name: SUPER_USER_EMAIL 8 | value: georgymarrero@gmail.com 9 | 10 | - option_name: RDS_HOSTNAME 11 | value: authdb.cwnzqu4zi2kl.us-west-1.rds.amazonaws.com 12 | - option_name: RDS_DB_NAME 13 | value: authdb 14 | - option_name: RDS_USERNAME 15 | value: sharkroot 16 | - option_name: RDS_PASSWORD 17 | value: IAmSharkRoot 18 | - option_name: RDS_PORT 19 | value: 5432 20 | -------------------------------------------------------------------------------- /.ebextensions/loadbalancer.config: -------------------------------------------------------------------------------- 1 | option_settings: 2 | aws:elb:listener:443: 3 | ListenerProtocol: HTTPS 4 | SSLCertificateId: arn:aws:acm:us-west-1:880058582700:certificate/5dafd5c9-3c1c-4455-b414-0f1d7a938f3c 5 | InstancePort: 80 6 | InstanceProtocol: HTTP 7 | aws:elb:listener:80: 8 | ListenerEnabled: false 9 | -------------------------------------------------------------------------------- /.ebextensions/packages.config: -------------------------------------------------------------------------------- 1 | packages: 2 | yum: 3 | git: [] 4 | postgresql93-devel: [] 5 | libjpeg-turbo-devel: [] 6 | -------------------------------------------------------------------------------- /.ebextensions/rds.config: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DiscreetAI/dashboard-auth/05117a78a0a0455900224286d05e5d6afb7e8470/.ebextensions/rds.config -------------------------------------------------------------------------------- /.elasticbeanstalk/config.yml: -------------------------------------------------------------------------------- 1 | branch-defaults: 2 | default: 3 | environment: beta3 4 | global: 5 | application_name: company-authentication-service 6 | branch: null 7 | default_ec2_keyname: aws-eb 8 | default_platform: Python 3.6 9 | default_region: us-west-1 10 | include_git_submodules: true 11 | instance_profile: null 12 | platform_name: null 13 | platform_version: null 14 | profile: null 15 | repository: null 16 | sc: null 17 | workspace_type: Application 18 | -------------------------------------------------------------------------------- /dashboard-auth/README.md: -------------------------------------------------------------------------------- 1 | # Dashboard Auth Service 2 | 3 | Authentication Service for DataAgora's Dashboard. 4 | 5 | ### Deploying 6 | 7 | 1. Make sure your AWS is properly configured on your local machine. 8 | 9 | 2. Install the EB CLI following [these instructions.](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3-install.html) 10 | 11 | 3. Run the following command to deploy a new version: 12 | 13 | ``` 14 | eb deploy eauth-env 15 | ``` 16 | 17 | -------------------------------------------------------------------------------- /dashboard-auth/core/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DiscreetAI/dashboard-auth/05117a78a0a0455900224286d05e5d6afb7e8470/dashboard-auth/core/__init__.py -------------------------------------------------------------------------------- /dashboard-auth/core/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for demo project. 3 | 4 | For more information on this file, see 5 | https://docs.djangoproject.com/en/1.7/topics/settings/ 6 | 7 | For the full list of settings and their values, see 8 | https://docs.djangoproject.com/en/1.7/ref/settings/ 9 | """ 10 | 11 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 12 | import os 13 | import datetime 14 | 15 | from corsheaders.defaults import default_headers 16 | 17 | BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = os.environ['SECRET_KEY'] if 'SECRET_KEY' in os.environ \ 24 | else 'datajbsnmd5h84rbewvzx6*cax^jgmqw@m3$ds_%z-4*qy0n44fjr5shark' 25 | 26 | # SECURITY WARNING: don't run with debug turned on in production! 27 | DEBUG = False 28 | 29 | ALLOWED_HOSTS = ['*'] # Insecure... should change to actual host. 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = ( 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | # 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'django.contrib.sites', 41 | 42 | 'rest_framework', 43 | #'rest_framework.authtoken', 44 | 'rest_auth', 45 | 46 | 'enterprise_users', 47 | 'allauth', 48 | 'allauth.account', 49 | 'rest_auth.registration', 50 | 'rest_framework_swagger', 51 | 'corsheaders', 52 | ) 53 | 54 | MIDDLEWARE = ( 55 | 'corsheaders.middleware.CorsMiddleware', 56 | 'django.middleware.common.CommonMiddleware', 57 | 'django.contrib.sessions.middleware.SessionMiddleware', 58 | 'django.middleware.common.CommonMiddleware', 59 | #'django.middleware.csrf.CsrfViewMiddleware', 60 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 61 | 'django.contrib.messages.middleware.MessageMiddleware', 62 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 63 | ) 64 | 65 | # For backwards compatibility for Django 1.8 66 | MIDDLEWARE_CLASSES = MIDDLEWARE 67 | 68 | ROOT_URLCONF = 'core.urls' 69 | 70 | WSGI_APPLICATION = 'core.wsgi.application' 71 | 72 | # Database 73 | # https://docs.djangoproject.com/en/1.7/ref/settings/#databases 74 | 75 | if 'RDS_HOSTNAME' in os.environ: 76 | DATABASES = { 77 | 'default': { 78 | 'ENGINE': 'django.db.backends.postgresql_psycopg2', 79 | 'NAME': os.environ['RDS_DB_NAME'], 80 | 'USER': os.environ['RDS_USERNAME'], 81 | 'PASSWORD': os.environ['RDS_PASSWORD'], 82 | 'HOST': os.environ['RDS_HOSTNAME'], 83 | 'PORT': os.environ['RDS_PORT'], 84 | } 85 | } 86 | else: 87 | DATABASES = { 88 | 'default': { 89 | 'ENGINE': 'django.db.backends.sqlite3', 90 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 91 | } 92 | } 93 | 94 | # Internationalization 95 | # https://docs.djangoproject.com/en/1.7/topics/i18n/ 96 | 97 | LANGUAGE_CODE = 'en-us' 98 | 99 | TIME_ZONE = 'UTC' 100 | 101 | USE_I18N = True 102 | 103 | USE_L10N = True 104 | 105 | USE_TZ = True 106 | 107 | # Static files (CSS, JavaScript, Images) 108 | # https://docs.djangoproject.com/en/1.7/howto/static-files/ 109 | 110 | STATIC_ROOT = os.path.join(BASE_DIR, 'static/') 111 | 112 | STATIC_URL = '/static/' 113 | 114 | STATICFILES_FINDERS = ( 115 | 'django.contrib.staticfiles.finders.FileSystemFinder', 116 | 'django.contrib.staticfiles.finders.AppDirectoriesFinder', 117 | # 'django.contrib.staticfiles.finders.DefaultStorageFinder', 118 | ) 119 | 120 | TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')] 121 | 122 | TEMPLATES = [ 123 | { 124 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 125 | 'DIRS': [os.path.join(BASE_DIR, 'templates'), ], 126 | 'APP_DIRS': True, 127 | 'OPTIONS': { 128 | 'context_processors': [ 129 | 'django.template.context_processors.debug', 130 | 'django.template.context_processors.request', 131 | 'django.contrib.auth.context_processors.auth', 132 | 'django.contrib.messages.context_processors.messages', 133 | ], 134 | }, 135 | }, 136 | ] 137 | 138 | REST_SESSION_LOGIN = False 139 | EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' 140 | ACCOUNT_EMAIL_REQUIRED = True 141 | ACCOUNT_USERNAME_REQUIRED = False 142 | ACCOUNT_AUTHENTICATION_METHOD = 'email' 143 | ACCOUNT_EMAIL_VERIFICATION = 'none' 144 | 145 | AUTHENTICATION_BACKENDS = ( 146 | # Needed to login by username in Django admin, regardless of `allauth` 147 | "django.contrib.auth.backends.ModelBackend", 148 | 149 | # `allauth` specific authentication methods, such as login by e-mail 150 | "allauth.account.auth_backends.AuthenticationBackend", 151 | ) 152 | 153 | REST_FRAMEWORK = { 154 | 'DEFAULT_PERMISSION_CLASSES': ( 155 | 'rest_framework.permissions.IsAuthenticated', 156 | ), 157 | 'DEFAULT_AUTHENTICATION_CLASSES': ( 158 | 'rest_framework_jwt.authentication.JSONWebTokenAuthentication', 159 | 'rest_framework.authentication.SessionAuthentication', 160 | 'rest_framework.authentication.BasicAuthentication', 161 | #'rest_framework.authentication.TokenAuthentication', 162 | ), 163 | 'COERCE_DECIMAL_TO_STRING': False 164 | } 165 | 166 | AUTH_PASSWORD_VALIDATORS = [ 167 | { 168 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 169 | }, 170 | { 171 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 172 | }, 173 | { 174 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 175 | }, 176 | { 177 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 178 | }, 179 | ] 180 | 181 | REST_AUTH_SERIALIZERS = { 182 | 'USER_DETAILS_SERIALIZER': 'enterprise_users.serializers.UserSerializer' 183 | } 184 | 185 | REST_AUTH_REGISTER_SERIALIZERS = { 186 | 'REGISTER_SERIALIZER': 'enterprise_users.serializers.CustomRegisterSerializer', 187 | } 188 | 189 | SWAGGER_SETTINGS = { 190 | 'LOGIN_URL': 'login', 191 | 'LOGOUT_URL': 'logout', 192 | } 193 | 194 | SITE_ID = 2 195 | 196 | REST_USE_JWT = True 197 | JWT_AUTH = { 198 | "JWT_PAYLOAD_HANDLER": 'custom.jwt.jwt_payload_handler', 199 | "JWT_RESPONSE_PAYLOAD_HANDLER": 'custom.jwt.jwt_response_payload_handler', 200 | "JWT_ALLOW_REFRESH": True, 201 | "JWT_ISSUER": "buy", 202 | 'JWT_EXPIRATION_DELTA': datetime.timedelta(weeks=1), 203 | } 204 | 205 | SESSION_COOKIE_DOMAIN = ".dataagora.com" 206 | CSRF_COOKIE_DOMAIN = ".dataagora.com" 207 | 208 | CORS_ALLOW_CREDENTIALS = True 209 | CORS_ALLOW_HEADERS = default_headers + ( 210 | 'cache', 211 | ) 212 | CORS_ORIGIN_REGEX_WHITELIST = (r'^(https?://)?(\w+\.)?dataagora\.com$', ) 213 | 214 | CORS_ORIGIN_WHITELIST = ( 215 | 'buy.dataagora.com', 216 | 'beta.dataagora.com', 217 | 'localhost:3000' 218 | ) 219 | -------------------------------------------------------------------------------- /dashboard-auth/core/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | from django.conf.urls import include, url 3 | from django.contrib import admin 4 | from django.views.generic import TemplateView, RedirectView 5 | 6 | from rest_framework.routers import DefaultRouter 7 | from rest_framework_swagger.views import get_swagger_view 8 | from rest_framework.documentation import include_docs_urls 9 | 10 | from rest_framework_jwt.views import obtain_jwt_token, refresh_jwt_token, verify_jwt_token 11 | 12 | urlpatterns = [ 13 | url(r'^docs/', include_docs_urls(title='Enterprise Auth API', description='RESTful API for Authentication.')), 14 | path('secret_admin_/', admin.site.urls), 15 | 16 | # User Authentication 17 | url(r'^auth/', include('rest_auth.urls')), 18 | url(r'^auth/registration/', include('rest_auth.registration.urls')), 19 | url(r'^auth/token/obtain', obtain_jwt_token), 20 | url(r'^auth/token/refresh', refresh_jwt_token), 21 | url(r'^auth/token/verify', verify_jwt_token), 22 | ] 23 | -------------------------------------------------------------------------------- /dashboard-auth/core/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for demo project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") 12 | 13 | print("START") 14 | from django.core.wsgi import get_wsgi_application 15 | application = get_wsgi_application() 16 | -------------------------------------------------------------------------------- /dashboard-auth/custom/jwt.py: -------------------------------------------------------------------------------- 1 | import jwt 2 | import uuid 3 | import warnings 4 | 5 | from django.contrib.auth import get_user_model 6 | 7 | from calendar import timegm 8 | from datetime import datetime 9 | 10 | from rest_framework_jwt.settings import api_settings 11 | from enterprise_users.serializers import UserSerializer 12 | 13 | def jwt_response_payload_handler(token, user=None, request=None): 14 | """ Custom response payload handler. 15 | 16 | This function controlls the custom payload after login or token refresh. This data is returned through the web API. 17 | """ 18 | 19 | # Here you can use other serializers or custom logic, it's up to you! 20 | 21 | return { 22 | 'token': token, 23 | 'user': UserSerializer(user, context={'request': request}).data, 24 | } 25 | 26 | 27 | def jwt_payload_handler(user): 28 | warnings.warn( 29 | 'The following fields will be removed in the future: ' 30 | '`email` and `user_id`. ', 31 | DeprecationWarning 32 | ) 33 | 34 | payload = { 35 | 'pk': user.pk, 36 | 'username': user.username, 37 | 'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA 38 | } 39 | 40 | if hasattr(user, 'email'): 41 | payload['email'] = user.email 42 | if isinstance(user.pk, uuid.UUID): 43 | payload['pk'] = str(user.pk) 44 | 45 | if hasattr(user, 'first_name'): 46 | payload['first_name'] = user.first_name 47 | if hasattr(user, 'last_name'): 48 | payload['last_name'] = user.last_name 49 | if hasattr(user, 'enterpriseuserprofile'): 50 | profile = user.enterpriseuserprofile 51 | if hasattr(profile, 'company'): 52 | payload['company'] = profile.company 53 | if hasattr(profile, 'occupation'): 54 | payload['occupation'] = profile.occupation 55 | 56 | # Include original issued at time for a brand new token, 57 | # to allow token refresh 58 | if api_settings.JWT_ALLOW_REFRESH: 59 | payload['orig_iat'] = timegm( 60 | datetime.utcnow().utctimetuple() 61 | ) 62 | 63 | if api_settings.JWT_AUDIENCE is not None: 64 | payload['aud'] = api_settings.JWT_AUDIENCE 65 | 66 | if api_settings.JWT_ISSUER is not None: 67 | payload['iss'] = api_settings.JWT_ISSUER 68 | 69 | return payload 70 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DiscreetAI/dashboard-auth/05117a78a0a0455900224286d05e5d6afb7e8470/dashboard-auth/enterprise_users/__init__.py -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.contrib.auth.models import User 3 | 4 | from enterprise_users.models import EnterpriseUserProfile 5 | 6 | admin.site.register(EnterpriseUserProfile) 7 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | class UsersConfig(AppConfig): 4 | name = 'users' 5 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/forms.py: -------------------------------------------------------------------------------- 1 | from django.contrib.auth.models import User 2 | from django.contrib.auth.forms import UserCreationForm 3 | from django.contrib.auth.admin import UserAdmin 4 | from django.contrib.auth import forms 5 | 6 | class MyUserCreationForm(UserCreationForm): 7 | def clean_username(self): 8 | # Since User.username is unique, this check is redundant, 9 | # but it sets a nicer error message than the ORM. See #13147. 10 | username = self.cleaned_data["username"] 11 | try: 12 | User._default_manager.get(username=username) 13 | except User.DoesNotExist: 14 | return username 15 | raise forms.ValidationError(self.error_messages['duplicate_username']) 16 | 17 | class Meta(UserCreationForm.Meta): 18 | model = User 19 | 20 | class MyUserAdmin(UserAdmin): 21 | add_form = MyUserCreationForm 22 | 23 | admin.site.register(User,MyUserAdmin) 24 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/management/commands/create_superuser.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from django.core.management.base import BaseCommand 4 | 5 | from django.contrib.auth.models import User 6 | #from users.models import UserProfile 7 | 8 | class Command(BaseCommand): 9 | 10 | def handle(self, *args, **options): 11 | username = os.environ['SUPER_USER_NAME'] 12 | if not User.objects.filter(username=username).exists(): 13 | User.objects.create_superuser(username, 14 | os.environ['SUPER_USER_EMAIL'], 15 | os.environ['SUPER_USER_PASSWORD']) 16 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 2.0.2 on 2018-03-12 07:54 2 | 3 | from django.conf import settings 4 | from django.db import migrations, models 5 | import django.db.models.deletion 6 | 7 | 8 | class Migration(migrations.Migration): 9 | 10 | initial = True 11 | 12 | dependencies = [ 13 | migrations.swappable_dependency(settings.AUTH_USER_MODEL), 14 | ] 15 | 16 | operations = [ 17 | migrations.CreateModel( 18 | name='EnterpriseUserProfile', 19 | fields=[ 20 | ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 21 | ('occupation', models.CharField(default='N/A', max_length=100)), 22 | ('company', models.CharField(default='N/A', max_length=100)), 23 | ('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), 24 | ], 25 | ), 26 | ] 27 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DiscreetAI/dashboard-auth/05117a78a0a0455900224286d05e5d6afb7e8470/dashboard-auth/enterprise_users/migrations/__init__.py -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | from django.db.models.signals import post_save 3 | from django.dispatch import receiver 4 | from django.contrib.auth.models import User 5 | 6 | class EnterpriseUserProfile(models.Model): 7 | user = models.OneToOneField(User, on_delete=models.CASCADE) 8 | occupation = models.CharField(default="N/A", blank=False, null=False, max_length=100) 9 | company = models.CharField(default="N/A", blank=False, null=False, max_length=100) 10 | 11 | @receiver(post_save, sender=User) 12 | def create_user_profile(sender, instance, created, **kwargs): 13 | if created: 14 | EnterpriseUserProfile.objects.create(user=instance) 15 | 16 | @receiver(post_save, sender=User) 17 | def save_user_profile(sender, instance, **kwargs): 18 | instance.enterpriseuserprofile.save() 19 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/serializers.py: -------------------------------------------------------------------------------- 1 | import boto3 2 | 3 | from rest_framework import serializers 4 | from rest_auth.serializers import UserDetailsSerializer 5 | import os 6 | 7 | print("HEY 1") 8 | 9 | class UserSerializer(UserDetailsSerializer): 10 | occupation = serializers.CharField(min_length=3, max_length=100, source="enterpriseuserprofile.occupation") 11 | company = serializers.CharField(min_length=3, max_length=100, source="enterpriseuserprofile.company") 12 | 13 | class Meta(UserDetailsSerializer.Meta): 14 | fields = UserDetailsSerializer.Meta.fields + ('occupation', 'company', ) 15 | 16 | def update(self, instance, validated_data): 17 | profile_data = validated_data.pop('enterpriseuserprofile', {}) 18 | 19 | instance = super(UserSerializer, self).update(instance, validated_data) 20 | 21 | # Get and update user profile. 22 | if profile_data: 23 | profile = instance.enterpriseuserprofile 24 | 25 | occupation = profile_data.get('occupation') 26 | if occupation != None: 27 | profile.occupation = occupation 28 | 29 | company = profile_data.get('company') 30 | if company != None: 31 | profile.company = company 32 | 33 | profile.save() 34 | return instance 35 | 36 | from rest_auth.registration.serializers import RegisterSerializer 37 | from allauth.account import app_settings as allauth_settings 38 | from allauth.utils import email_address_exists 39 | from allauth.account.adapter import get_adapter 40 | from allauth.account.utils import setup_user_email 41 | 42 | class CustomRegisterSerializer(RegisterSerializer): 43 | print("HEY 2") 44 | first_name = serializers.CharField(required=True, write_only=True) 45 | last_name = serializers.CharField(required=True, write_only=True) 46 | company = serializers.CharField(source="enterpriseuserprofile.company", required=True, min_length=3, max_length=100, write_only=True) 47 | occupation = serializers.CharField(source="enterpriseuserprofile.occupation", required=True, min_length=3, max_length=100, write_only=True) 48 | 49 | def get_cleaned_data(self): 50 | profile_data = self.validated_data.pop('enterpriseuserprofile', {}) 51 | return { 52 | 'first_name': self.validated_data.get('first_name', ''), 53 | 'last_name': self.validated_data.get('last_name', ''), 54 | 'password1': self.validated_data.get('password1', ''), 55 | 'email': self.validated_data.get('email', ''), 56 | 'occupation': profile_data.get('occupation', "N/A"), 57 | 'company': profile_data.get('company', "N/A") 58 | } 59 | 60 | def save(self, request): 61 | print("Save called!") 62 | adapter = get_adapter() 63 | user = adapter.new_user(request) 64 | self.cleaned_data = self.get_cleaned_data() 65 | adapter.save_user(request, user, self) 66 | setup_user_email(request, user, []) 67 | user.save() 68 | 69 | profile = user.enterpriseuserprofile 70 | occupation = self.cleaned_data.get('occupation') 71 | if occupation != None: 72 | profile.occupation = occupation 73 | 74 | company = self.cleaned_data.get('company') 75 | if company != None: 76 | profile.company = company 77 | profile.save() 78 | print("Creating user!", user.id) 79 | self._createUserData(user.id) 80 | print("Success!") 81 | return user 82 | 83 | def _createUserData(self, user_id): 84 | """Only creates it if doesn't exist already.""" 85 | try: 86 | access_key = os.environ["ACCESS_KEY_ID"] 87 | secret_key = os.environ["SECRET_ACCESS_KEY"] 88 | dynamodb = boto3.resource('dynamodb', region_name='us-west-1', aws_access_key_id=access_key, aws_secret_access_key=secret_key) 89 | table = dynamodb.Table("UsersDashboardData") 90 | item = { 91 | 'UserId': user_id, 92 | 'ReposRemaining': 3, 93 | } 94 | table.put_item( 95 | Item=item, 96 | ConditionExpression="attribute_not_exists(UserId)" 97 | ) 98 | except Exception as e: 99 | print(str(e)) 100 | raise Exception("Error while creating the user dashboard data.") 101 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /dashboard-auth/enterprise_users/views.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DiscreetAI/dashboard-auth/05117a78a0a0455900224286d05e5d6afb7e8470/dashboard-auth/enterprise_users/views.py -------------------------------------------------------------------------------- /dashboard-auth/manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | import sys 4 | 5 | if __name__ == "__main__": 6 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") 7 | 8 | from django.core.management import execute_from_command_line 9 | 10 | execute_from_command_line(sys.argv) 11 | -------------------------------------------------------------------------------- /dashboard-auth/requirements.pip: -------------------------------------------------------------------------------- 1 | django>=1.9.0 2 | django-rest-auth==0.9.2 3 | djangorestframework>=3.7.0 4 | django-allauth==0.35.0 5 | django-cors-headers==2.2.0 6 | six==1.9.0 7 | django-rest-swagger==2.0.7 8 | psycopg2==2.7.3.2 9 | coreapi==2.3.3 10 | coreapi-cli==1.0.6 11 | djangorestframework-jwt==1.11.0 12 | -------------------------------------------------------------------------------- /dashboard-auth/requirements.txt: -------------------------------------------------------------------------------- 1 | certifi==2017.11.5 2 | chardet==3.0.4 3 | click==6.7 4 | coreapi==2.3.3 5 | coreapi-cli==1.0.6 6 | coreschema==0.0.4 7 | defusedxml==0.5.0 8 | Django==2.0 9 | django-allauth==0.35.0 10 | django-rest-auth==0.9.2 11 | django-rest-swagger==2.0.7 12 | djangorestframework==3.7.7 13 | django-cors-headers==2.2.0 14 | djangorestframework-jwt==1.11.0 15 | idna==2.6 16 | itypes==1.1.0 17 | Jinja2==2.10 18 | Markdown==2.6.10 19 | MarkupSafe==1.0 20 | oauthlib==2.0.6 21 | openapi-codec==1.3.2 22 | psycopg2==2.7.3.2 23 | Pygments==2.2.0 24 | python3-openid==3.1.0 25 | pytz==2017.3 26 | requests==2.18.4 27 | requests-oauthlib==0.8.0 28 | simplejson==3.13.2 29 | six==1.9.0 30 | uritemplate==3.0.0 31 | urllib3==1.22 32 | boto3==1.9.145 33 | -------------------------------------------------------------------------------- /dashboard-auth/setup.sh: -------------------------------------------------------------------------------- 1 | python manage.py collectstatic --settings=core.settings --noinput && python manage.py makemigrations && python manage.py migrate enterprise_users --noinput && python manage.py migrate --noinput && python manage.py create_superuser 2 | --------------------------------------------------------------------------------