-
Notifications
You must be signed in to change notification settings - Fork 1
Auth system (JWT) #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
darkgl06
wants to merge
5
commits into
master
Choose a base branch
from
feature/auth_system
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f037186
feat: added new queryzen user model
darkgl06 963e3f9
feat: added jwt authentication
darkgl06 7d92100
feat: added IsAuthorized to all endpoints. Refactored httpx client to…
darkgl06 e7552cb
fix: removed unused functions
darkgl06 cab3098
imp: renamed email param and added more details to auth exception
darkgl06 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or 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,7 @@ | ||
| # pylint: disable=C0114 | ||
| from django.apps import AppConfig | ||
|
|
||
|
|
||
| class AuthConfig(AppConfig): | ||
| default_auto_field = 'django.db.models.BigAutoField' | ||
| name = 'apps.authentication' |
31 changes: 31 additions & 0 deletions
31
queryzen-api/apps/authentication/migrations/0001_initial.py
This file contains hidden or 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,31 @@ | ||
| # Generated by Django 5.1.6 on 2025-07-24 15:07 | ||
|
|
||
| import uuid | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [ | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='QueryzenUser', | ||
| fields=[ | ||
| ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)), | ||
| ('password', models.CharField(max_length=128, verbose_name='password')), | ||
| ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), | ||
| ('email', models.EmailField(max_length=254, unique=True)), | ||
| ('created_at', models.DateTimeField(auto_now_add=True)), | ||
| ('is_superuser', models.BooleanField(default=False)), | ||
| ('is_staff', models.BooleanField(default=False)), | ||
| ('is_active', models.BooleanField(default=True)), | ||
| ], | ||
| options={ | ||
| 'abstract': False, | ||
| }, | ||
| ), | ||
| ] |
Empty file.
This file contains hidden or 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,52 @@ | ||
| # pylint: disable=C0114 | ||
| from apps.shared.mixins import UUIDMixin | ||
| from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager | ||
| from django.db import models | ||
|
|
||
|
|
||
| class QueryzenUserManager(BaseUserManager): | ||
| """Custom manager for QueryzenUser model.""" | ||
| def create_user(self, email, password, **extra_fields): | ||
| """Create and return a regular user with the given email and password.""" | ||
| if not email: | ||
| raise ValueError('Users must have an email address') | ||
| if not password: | ||
| raise ValueError('Users must have a password') | ||
|
|
||
| email = self.normalize_email(email) | ||
| user = self.model(email=email, **extra_fields) | ||
| user.set_password(password) | ||
| user.save(using=self._db) | ||
| return user | ||
|
|
||
| def create_superuser(self, email, password, **extra_fields): | ||
| """Create and return a superuser with the given email and password.""" | ||
| extra_fields.setdefault('is_superuser', True) | ||
| extra_fields.setdefault('is_staff', True) | ||
| extra_fields.setdefault('is_active', True) | ||
|
|
||
| return self.create_user(email, password, **extra_fields) | ||
|
|
||
|
|
||
| class QueryzenUser(AbstractBaseUser, UUIDMixin): | ||
| """Custom user model that uses email as the unique identifier.""" | ||
| email = models.EmailField(unique=True) | ||
| created_at = models.DateTimeField(auto_now_add=True) | ||
|
|
||
| is_superuser = models.BooleanField(default=False) | ||
| is_staff = models.BooleanField(default=False) # Needed for admin access | ||
| is_active = models.BooleanField(default=True) # Needed for login system | ||
|
|
||
| objects = QueryzenUserManager() | ||
|
|
||
| USERNAME_FIELD = 'email' | ||
| REQUIRED_FIELDS = [] | ||
|
|
||
| def __str__(self): | ||
| return str(self.email) | ||
|
|
||
| def has_perm(self): | ||
| return self.is_superuser | ||
|
|
||
| def has_module_perms(self): | ||
| return self.is_superuser |
This file contains hidden or 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,11 @@ | ||
| # pylint: disable=C0114 | ||
| from django.urls import path | ||
| from rest_framework_simplejwt.views import ( | ||
| TokenObtainPairView, | ||
| TokenRefreshView, | ||
| ) | ||
|
|
||
| urlpatterns = [ | ||
| path('auth/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), | ||
| path('auth/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), | ||
| ] |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
| """ | ||
| This module defines a wrapper class for httpx.Client to handle | ||
| authenticated HTTP requests with optional bearer token support. | ||
| """ | ||
| import httpx | ||
|
|
||
|
|
||
| class HttpxWrapper: | ||
| """ | ||
| Wrapper around httpx.Client to provide centralized handling of | ||
| authentication headers and request execution logic. | ||
| """ | ||
| access_token: str | None = None | ||
|
|
||
| def __init__(self, client: httpx.Client | None = None, **kwargs): | ||
| self._client = client or httpx.Client(**kwargs) | ||
|
|
||
| def _get_headers(self) -> dict[str, str]: | ||
| return {'Authorization': f'Bearer {self.access_token}'} if self.access_token else {} | ||
|
|
||
| def _handle_request(self, method: str, url: str, **kwargs): | ||
| return getattr(self._client, method)(url, headers=self._get_headers(), **kwargs) | ||
|
|
||
| def get(self, url, **kwargs): | ||
| return self._handle_request('get', url, **kwargs) | ||
|
|
||
| def post(self, url, **kwargs): | ||
| return self._handle_request('post', url, **kwargs) | ||
|
|
||
| def put(self, url, **kwargs): | ||
| return self._handle_request('put', url, **kwargs) | ||
|
|
||
| def delete(self, url, **kwargs): | ||
| return self._handle_request('delete', url, **kwargs) | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm unsure if wee need a wrapper over httpx just to add a header token
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it'll be useful for future features such as who run the query (python-client, through http directly...)