-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add auth app from prefeitura do rio (#576)
- Loading branch information
Showing
16 changed files
with
776 additions
and
3 deletions.
There are no files selected for viewing
Empty file.
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,31 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.contrib import admin | ||
|
||
from bd_api.apps.account_auth.models import ( | ||
Access, | ||
Domain, | ||
Token, | ||
) | ||
|
||
|
||
class AccessInline(admin.TabularInline): | ||
model = Access | ||
|
||
|
||
class DomainAdmin(admin.ModelAdmin): | ||
list_display = ("name", "description", "is_active") | ||
inlines = [AccessInline] | ||
|
||
|
||
class TokenAdmin(admin.ModelAdmin): | ||
list_display = ("user", "domain", "is_active") | ||
inlines = [AccessInline] | ||
|
||
|
||
class AccessAdmin(admin.ModelAdmin): | ||
list_display = ("timestamp", "success", "domain") | ||
|
||
|
||
admin.site.register(Domain, DomainAdmin) | ||
admin.site.register(Token, TokenAdmin) | ||
admin.site.register(Access, AccessAdmin) |
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,7 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.apps import AppConfig | ||
|
||
|
||
class AuthConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "bd_api.apps.account_auth" |
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,106 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 4.2.10 on 2024-03-24 14:37 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="Domain", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
), | ||
), | ||
("name", models.CharField(max_length=255)), | ||
("description", models.TextField(blank=True, null=True)), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
("is_active", models.BooleanField(default=True)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name="Token", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
), | ||
), | ||
("token", models.CharField(editable=False, max_length=255, unique=True)), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("is_active", models.BooleanField(default=True)), | ||
("expiry_date", models.DateTimeField(blank=True, null=True)), | ||
( | ||
"domain", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="tokens", | ||
to="account_auth.domain", | ||
), | ||
), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="tokens", | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name="Access", | ||
fields=[ | ||
( | ||
"id", | ||
models.BigAutoField( | ||
auto_created=True, primary_key=True, serialize=False, verbose_name="ID" | ||
), | ||
), | ||
("timestamp", models.DateTimeField(auto_now_add=True)), | ||
("success", models.BooleanField()), | ||
( | ||
"domain", | ||
models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="accesses", | ||
to="account_auth.domain", | ||
), | ||
), | ||
( | ||
"token", | ||
models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="accesses", | ||
to="account_auth.token", | ||
), | ||
), | ||
( | ||
"user", | ||
models.ForeignKey( | ||
blank=True, | ||
null=True, | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to=settings.AUTH_USER_MODEL, | ||
), | ||
), | ||
], | ||
), | ||
] |
Empty file.
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,60 @@ | ||
# -*- coding: utf-8 -*- | ||
from uuid import uuid4 | ||
|
||
from django.conf import settings | ||
from django.db import models | ||
|
||
USER_MODEL = settings.AUTH_USER_MODEL | ||
|
||
|
||
class Domain(models.Model): | ||
name = models.CharField(max_length=255) | ||
description = models.TextField(null=True, blank=True) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
is_active = models.BooleanField(default=True) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
|
||
class Token(models.Model): | ||
user = models.ForeignKey(USER_MODEL, on_delete=models.CASCADE, related_name="tokens") | ||
domain = models.ForeignKey(Domain, on_delete=models.CASCADE, related_name="tokens") | ||
token = models.CharField(max_length=255, editable=False, unique=True) | ||
created_at = models.DateTimeField(auto_now_add=True) | ||
is_active = models.BooleanField(default=True) | ||
expiry_date = models.DateTimeField(null=True, blank=True) | ||
|
||
def __str__(self): | ||
return f"{self.user.username} - {self.domain} - {self.token}" | ||
|
||
def generate_token(self): | ||
return str(uuid4()) | ||
|
||
def save(self): | ||
self.token = self.generate_token() | ||
super().save() | ||
|
||
|
||
class Access(models.Model): | ||
user = models.ForeignKey(USER_MODEL, on_delete=models.CASCADE, null=True, blank=True) | ||
token = models.ForeignKey( | ||
Token, on_delete=models.CASCADE, related_name="accesses", null=True, blank=True | ||
) | ||
domain = models.ForeignKey( | ||
Domain, | ||
on_delete=models.CASCADE, | ||
related_name="accesses", | ||
null=True, | ||
blank=True, | ||
) | ||
timestamp = models.DateTimeField(auto_now_add=True) | ||
success = models.BooleanField() | ||
|
||
def __str__(self): | ||
return ( | ||
f"{self.timestamp} - {'OK' if self.success else 'ERR'} - " | ||
f"{self.domain if self.domain else 'NO_DOMAIN'} - " | ||
f"{self.token.user.username if self.token else 'NO_TOKEN'}" | ||
) |
Oops, something went wrong.