Browse Source

Register actions

master
alfred 1 month ago
parent
commit
26ed9a498b

+ 6
- 6
apps/authentication/authenticators.py View File

@@ -38,14 +38,14 @@ class Authenticator(DJaWThObject):
38 38
         identity.save()
39 39
         return HttpResponse()  # TODO: HttpResponseRedirect(redirect_to=project.identity_confirmed_url)
40 40
 
41
-    def perform_authorization(self, project, identity, request, **kwargs):
41
+    def valid_login(self, project, identity, request, **kwargs):
42
+        from apps.project.models import Record
43
+        Record.register(action='valid login', request=request, identity=identity)
42 44
         return project.authorization.authorize(project, identity, request, **kwargs)
43 45
 
44
-    def valid_login(self):
45
-        pass
46
-
47
-    def invalid_login(self):
48
-        pass
46
+    def invalid_login(self, project, identity, request, **kwargs):
47
+        from apps.project.models import Record
48
+        Record.register(action='invalid login', request=request, identity=identity)
49 49
 
50 50
 
51 51
 class PasswordlessAuthenticator(mixins.PasswordlessMixin, Authenticator):

+ 7
- 4
apps/authentication/mixins/passwordless.py View File

@@ -40,7 +40,10 @@ class PasswordlessMixin:
40 40
         return HttpResponse()
41 41
 
42 42
     def authorize(self, project, request, **kwargs):
43
-        data = json.loads(request.body)
44
-        identity = project.identities.get(pk=data['user'])
45
-        assert identity.credentials.token == data['token']
46
-        return self.perform_authorization(project, identity, request, **kwargs)
43
+        try:
44
+            data = json.loads(request.body)
45
+            identity = project.identities.get(pk=data['user'])
46
+            assert identity.credentials.token == data['token']
47
+            return self.valid_login(project, identity, request, **kwargs)
48
+        except AssertionError:
49
+            return self.invalid_login(project, identity, request, **kwargs)

+ 7
- 4
apps/authentication/mixins/userpassword.py View File

@@ -6,10 +6,13 @@ import json
6 6
 
7 7
 class UserPasswordMixin:
8 8
     def login(self, project, request):
9
-        data = json.loads(request.body)
10
-        identity = project.identities.get(email__address=data['username'])
11
-        assert check_password(data['password'], identity.credentials.password)
12
-        return self.perform_authorization(project, identity, request)
9
+        try:
10
+            data = json.loads(request.body)
11
+            identity = project.identities.get(email__address=data['username'])
12
+            assert check_password(data['password'], identity.credentials.password)
13
+            return self.valid_login(project, identity, request)
14
+        except AssertionError:
15
+            return self.invalid_login(project, identity, request)
13 16
 
14 17
     def confirm_identity(self, project, identity, token, request, **kwargs):
15 18
         from django.contrib.auth.forms import SetPasswordForm

+ 7
- 0
apps/base/utils.py View File

@@ -0,0 +1,7 @@
1
+def get_ip(request):
2
+    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
3
+    if x_forwarded_for:
4
+        ip = x_forwarded_for.split(',')[0]
5
+    else:
6
+        ip = request.META.get('REMOTE_ADDR')
7
+    return ip

+ 21
- 0
apps/project/models.py View File

@@ -9,6 +9,9 @@ from apps.project import utils
9 9
 
10 10
 
11 11
 class Project(models.Model):
12
+    class Meta:
13
+        db_table = 'platforms'
14
+
12 15
     name = models.CharField(max_length=255, blank=False, null=False)
13 16
     owner = models.ForeignKey(base_models.User, on_delete=models.CASCADE, related_name='projects')
14 17
     identifier = models.CharField(max_length=265, blank=False, null=False, unique=True)
@@ -78,6 +81,9 @@ class Project(models.Model):
78 81
 
79 82
 
80 83
 class Email(models.Model):
84
+    class Meta:
85
+        db_table = 'emails'
86
+
81 87
     uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
82 88
     address = models.EmailField(unique=True)
83 89
     created = models.DateTimeField(auto_now_add=True)
@@ -97,6 +103,7 @@ class Email(models.Model):
97 103
 
98 104
 class Identity(models.Model):
99 105
     class Meta:
106
+        db_table = 'identities'
100 107
         unique_together = ['email', 'project']
101 108
 
102 109
     uuid = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False)
@@ -135,9 +142,23 @@ class Identity(models.Model):
135 142
 
136 143
 
137 144
 class Record(models.Model):
145
+    class Meta:
146
+        db_table = 'registry'
147
+
138 148
     uuid = models.UUIDField(primary_key=True, unique=True, default=uuid.uuid4, editable=False)
139 149
     identity = models.ForeignKey(Identity, on_delete=models.CASCADE, related_name='history', null=True)
140 150
     ip = models.GenericIPAddressField()
141 151
     action = models.CharField(max_length=100, blank=True, null=True)
142 152
     date = models.DateTimeField(auto_now_add=True)
143 153
     data = JSONField(default=dict)
154
+
155
+    @classmethod
156
+    def register(cls, action, request, identity=None, **kwargs):
157
+        from apps.base import utils
158
+        record = Record(
159
+            identity=identity,
160
+            ip=utils.get_ip(request),
161
+            action=action,
162
+            data=kwargs
163
+        )
164
+        record.save()

Loading…
Cancel
Save