Skip to content

Commit

Permalink
feat: add auth app from prefeitura do rio (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Mar 27, 2024
1 parent c8debc6 commit cc95d2b
Show file tree
Hide file tree
Showing 16 changed files with 776 additions and 3 deletions.
Empty file.
31 changes: 31 additions & 0 deletions bd_api/apps/account_auth/admin.py
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)
7 changes: 7 additions & 0 deletions bd_api/apps/account_auth/apps.py
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"
106 changes: 106 additions & 0 deletions bd_api/apps/account_auth/migrations/0001_initial.py
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.
60 changes: 60 additions & 0 deletions bd_api/apps/account_auth/models.py
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'}"
)
Loading

0 comments on commit cc95d2b

Please sign in to comment.