Skip to content

Commit

Permalink
Add client_id to SessionToken and SessionTokenSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
lnagel committed Jul 29, 2022
1 parent 4306bbf commit fcf357c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions rest_framework_sso/migrations/0003_sessiontoken_client_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.7 on 2021-10-26 09:51

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("rest_framework_sso", "0002_sessiontoken_last_used_at"),
]

operations = [
migrations.AddField(
model_name="sessiontoken",
name="client_id",
field=models.CharField(blank=True, max_length=1000),
),
]
1 change: 1 addition & 0 deletions rest_framework_sso/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class SessionToken(models.Model):

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, db_index=True)
user = models.ForeignKey(to=AUTH_USER_MODEL, related_name="+", on_delete=models.CASCADE, verbose_name=_("user"))
client_id = models.CharField(max_length=1000, blank=True)
ip_address = models.GenericIPAddressField(null=True, blank=True, db_index=True)
user_agent = models.CharField(max_length=1000, blank=True)
last_used_at = models.DateTimeField(null=True, blank=True, db_index=True)
Expand Down
1 change: 1 addition & 0 deletions rest_framework_sso/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class SessionTokenSerializer(serializers.Serializer):
username = serializers.CharField(label=_("Username"))
password = serializers.CharField(label=_("Password"), style={"input_type": "password"})
client_id = serializers.CharField(label=_("Client ID"), allow_blank=True, required=False, default="")

def validate(self, attrs):
username = attrs.get("username")
Expand Down
10 changes: 8 additions & 2 deletions rest_framework_sso/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data["user"]
session_token = SessionToken.objects.active().filter(user=user).with_user_agent(request=request).first()
client_id = serializer.validated_data["client_id"]
session_token = (
SessionToken.objects.active()
.filter(user=user, client_id=client_id)
.with_user_agent(request=request)
.first()
)
if session_token is None:
session_token = SessionToken(user=user)
session_token = SessionToken(user=user, client_id=client_id)
session_token.update_attributes(request=request)
session_token.save()
payload = create_session_payload(session_token=session_token, user=user)
Expand Down

0 comments on commit fcf357c

Please sign in to comment.