-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Build-Squad/GEn-71-Refactor-User-Auth
Gen 71 refactor user auth
- Loading branch information
Showing
15 changed files
with
554 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
14 changes: 14 additions & 0 deletions
14
src/api/marketplace/accounts/migrations/0006_merge_20231207_0634.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [ | ||
] |
70 changes: 70 additions & 0 deletions
70
src/api/marketplace/accounts/migrations/0007_role_twitteraccount_description_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.