Skip to content

Commit

Permalink
Merge pull request #29 from Build-Squad/GEn-71-Refactor-User-Auth
Browse files Browse the repository at this point in the history
Gen 71 refactor user auth
  • Loading branch information
varsha1305nav authored Dec 7, 2023
2 parents 6f77df1 + 266c185 commit df06b5b
Show file tree
Hide file tree
Showing 15 changed files with 554 additions and 43 deletions.
5 changes: 3 additions & 2 deletions src/api/marketplace/accounts/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.contrib import admin

from .models import User, TwitterAccount, CategoryMaster, AccountCategory
from .models import User, TwitterAccount, CategoryMaster, AccountCategory, Role
# Register your models here.

admin.site.register(User)
admin.site.register(TwitterAccount)
admin.site.register(CategoryMaster)
admin.site.register(AccountCategory)
admin.site.register(AccountCategory)
admin.site.register(Role)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 4.2.7 on 2023-12-07 06:34

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('accounts', '0005_merge_20231128_0707'),
('accounts', '0005_merge_20231129_0511'),
]

operations = [
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Generated by Django 4.2.7 on 2023-12-07 06:34

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

dependencies = [
('accounts', '0006_merge_20231207_0634'),
]

operations = [
migrations.CreateModel(
name='Role',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='Role ID')),
('name', models.CharField(blank=True, max_length=255, null=True)),
],
options={
'db_table': 'role',
},
),
migrations.AddField(
model_name='twitteraccount',
name='description',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AddField(
model_name='twitteraccount',
name='followers_count',
field=models.IntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='twitteraccount',
name='following_count',
field=models.IntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='twitteraccount',
name='listed_count',
field=models.IntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='twitteraccount',
name='profile_image_url',
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AddField(
model_name='twitteraccount',
name='tweet_count',
field=models.IntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='twitteraccount',
name='verified',
field=models.BooleanField(blank=True, default=False, null=True),
),
migrations.AlterField(
model_name='bankaccount',
name='id',
field=models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='Bank Account ID'),
),
migrations.AlterField(
model_name='user',
name='role',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='user_role_id', to='accounts.role'),
),
]
29 changes: 23 additions & 6 deletions src/api/marketplace/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ class TwitterAccount(models.Model):
name = models.CharField(max_length=100, blank=True, null=True)
user_name = models.CharField(max_length=100, blank=True, null=True)
access_token = models.CharField(max_length=255, blank=True, null=True)
description = models.CharField(max_length=255, blank=True, null=True)
profile_image_url = models.CharField(max_length=255, blank=True, null=True)
followers_count = models.IntegerField(blank=True, null=True)
following_count = models.IntegerField(blank=True, null=True)
tweet_count = models.IntegerField(blank=True, null=True)
listed_count = models.IntegerField(blank=True, null=True)
verified = models.BooleanField(default=False, blank=True, null=True)

class Meta:
db_table = "twitter_account"

def __str__(self):
return self.name

class CategoryMaster(models.Model):
id = models.UUIDField(primary_key=True, verbose_name='Category Master ID', default=uuid.uuid4, editable=False)
name = models.CharField(max_length=255, blank=True, null=True)
Expand All @@ -35,24 +45,30 @@ class AccountCategory(models.Model):
class Meta:
db_table = "account_category"


class Role(models.Model):
id = models.UUIDField(
primary_key=True, verbose_name='Role ID', default=uuid.uuid4, editable=False)
name = models.CharField(max_length=255, blank=True, null=True)

class Meta:
db_table = "role"

class User(AbstractUser):

STATUS_CHOICES = (
('active', 'active'),
('inactive', 'inactive')
)

ROLE_CHOICES = (
('business_owner', 'business_owner'),
('influencer', 'influencer')
)

id = models.UUIDField(primary_key=True, verbose_name='User ID', default=uuid.uuid4, editable=False)
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=100, blank=True, null=True)
last_name = models.CharField(max_length=100, blank=True, null=True)
status = models.CharField(choices=STATUS_CHOICES, max_length=25, blank=True, null=True)
role = models.CharField(choices=ROLE_CHOICES, max_length=50, blank=True)
role = models.ForeignKey(
Role, related_name='user_role_id', on_delete=models.PROTECT, null=True, blank=True)
joined_at = models.DateTimeField(auto_now_add=True)
last_login = models.DateTimeField(auto_now=True, blank=True)
otp = models.CharField(max_length=25, blank=True, null=True)
Expand All @@ -69,7 +85,8 @@ class Meta:

class BankAccount(models.Model):

id = models.UUIDField(primary_key=True, verbose_name='Account Category ID', default=uuid.uuid4, editable=False)
id = models.UUIDField(
primary_key=True, verbose_name='Bank Account ID', default=uuid.uuid4, editable=False)
influencer = models.ForeignKey(User, related_name='bank_acc_influencer_id', on_delete=SET_NULL, null=True)
account_holder_name = models.CharField(max_length=150, blank=True, null=True)
account_number = models.CharField(max_length=50, blank=True, null=True)
Expand Down
29 changes: 28 additions & 1 deletion src/api/marketplace/accounts/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework import serializers
from .models import TwitterAccount, CategoryMaster, AccountCategory, User, BankAccount
from .models import TwitterAccount, CategoryMaster, AccountCategory, User, BankAccount, Role


class TwitterAccountSerializer(serializers.ModelSerializer):
Expand All @@ -21,6 +21,23 @@ class Meta:


class UserSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(read_only=True)
username = serializers.CharField(read_only=True)
email = serializers.EmailField(read_only=True)
first_name = serializers.CharField(read_only=True)
last_name = serializers.CharField(read_only=True)
status = serializers.CharField(read_only=True)
joined_at = serializers.DateTimeField(read_only=True)
last_login = serializers.DateTimeField(read_only=True)
role = serializers.CharField(read_only=True)
twitter_account = TwitterAccountSerializer(read_only=True)

class Meta:
model = User
fields = "__all__"


class UserCreateSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = "__all__"
Expand All @@ -30,3 +47,13 @@ class BankAccountSerializer(serializers.ModelSerializer):
class Meta:
model = BankAccount
fields = "__all__"


class TwitterAuthSerializer(serializers.Serializer):
role = serializers.CharField(max_length=100)


class RoleSerializer(serializers.ModelSerializer):
class Meta:
model = Role
fields = "__all__"
61 changes: 61 additions & 0 deletions src/api/marketplace/accounts/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from tweepy import Client, OAuth2UserHandler
from decouple import config
import datetime


class TwitterAuthenticationService:
def __init__(self):
# Add scopes here
self.SCOPES = [
"offline.access",
"tweet.read",
"tweet.write",
"users.read",
"follows.read",
"follows.write",
"mute.read",
]

self.USER_FIELDS = [
"description",
"profile_image_url",
"public_metrics",
"verified",
]

self.callback_url = f"{config('SERVER')}account/twitter-auth"

# This is OAuth2.0 PKCE authentication instance that'll be used to interact with Client for V2 version of API
self.oauth2_user_handler = OAuth2UserHandler(
client_id=config("CLIENT_ID"),
redirect_uri=self.callback_url,
scope=self.SCOPES,
client_secret=config("CLIENT_SECRET"),
)

def get_twitter_oauth_url(self):
auth_url = self.oauth2_user_handler.get_authorization_url()
return auth_url

def get_twitter_access_token(self, authorization_response_url):
access_token_obj = self.oauth2_user_handler.fetch_token(
authorization_response_url)
access_token = access_token_obj["access_token"]
return access_token

def get_twitter_client_data(self, request):
authorization_response_url = request.build_absolute_uri()
access_token = self.get_twitter_access_token(
authorization_response_url)
client = Client(access_token)
userData = client.get_me(
user_auth=False, user_fields=self.USER_FIELDS).data
return userData

def get_jwt_payload(self, twitter_user):
payload = {
"id": twitter_user.id,
"exp": datetime.datetime.utcnow() + datetime.timedelta(seconds=86400),
"iat": datetime.datetime.utcnow(),
}
return payload
13 changes: 12 additions & 1 deletion src/api/marketplace/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,21 @@
UserList,
UserDetail,
BankAccountList,
BankAccountDetail
BankAccountDetail,
TwitterAuth,
RoleList,
RoleDetail,
UserAuth
)

urlpatterns = [
path("", UserAuth.as_view(), name="user-auth"),

path("role/", RoleList.as_view(), name="role-list"),
path("role/<uuid:pk>/", RoleDetail.as_view(), name="role-detail"),

path("twitter-auth/", TwitterAuth.as_view(), name="twitter-auth"),

path("twitter-account/", TwitterAccountList.as_view(), name="twitter-account-list"),
path("twitter-account/<uuid:pk>/", TwitterAccountDetail.as_view(), name="twitter-account-detail"),

Expand Down
Loading

0 comments on commit df06b5b

Please sign in to comment.