Skip to content

Commit

Permalink
fixed auth_service now if the access token was expired it will genera…
Browse files Browse the repository at this point in the history
…te a new one and if the refresh was expired it will generate refresh and access
  • Loading branch information
sepehr toof authored and sepehr toof committed Jun 16, 2024
1 parent 469224a commit 98018a9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Backend/auth_service/auth_service/user_auth/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Create your models here.
class UserTokens(models.Model):
username = models.CharField(unique=True)
token_data = models.JSONField()
token_data = models.JSONField(null=True)

def __str__(self):
return self.username
61 changes: 40 additions & 21 deletions Backend/auth_service/auth_service/user_auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,60 @@ def handle_response(ch, method, properties, body):
if 'error' in user_data:
return Response({'error': 'Invalid credentials'}, status=status.HTTP_401_UNAUTHORIZED)
try:
# Fetch user token data from database
user = type('User', (object,), user_data) # Mock user object with user_data
refresh = None
access = None

# Retrieve or create the UserTokens entry
user_token_entry, created = UserTokens.objects.get_or_create(username=username)
token_data = user_token_entry.token_data

if not created:
# Check if the token is expired
refresh_expiration = datetime.fromisoformat(token_data['refresh']['exp'])
if refresh_expiration > datetime.now(timezone.utc):
# Tokens are still valid
return Response({
'refresh': token_data['refresh']['token'],
'access': token_data['access']['token'],
}, status=status.HTTP_200_OK)
# If the user entry already exists, check if tokens are still valid
token_data = user_token_entry.token_data
refresh_expire_time = token_data['refresh']['exp']
access_expire_time = token_data['access']['exp']
refresh = RefreshToken(token_data['refresh']['token'])
access = token_data['access']['token']
current_time = datetime.now(timezone.utc)
if refresh_expire_time < current_time.timestamp() or access_expire_time < current_time.timestamp():
if refresh_expire_time < current_time.timestamp():
refresh = RefreshToken.for_user(user)
access = refresh.access_token
elif access_expire_time < current_time.timestamp():
refresh = RefreshToken(token_data['refresh']['token'])
access = refresh.access_token
user_token_entry.token_data = {
'refresh': {
'token': str(refresh),
'exp': int(refresh['exp']), # Store expiration as integer
},
'access': {
'token': str(access),
'exp': int(access['exp']), # Store expiration as integer
}
}
else:
# Generate new tokens if expired or new user
user = type('User', (object,), user_data) # Mock user object with user_data
# If the user entry was just created, store new tokens
refresh = RefreshToken.for_user(user)
access = refresh.access_token

# Update token data in database
user_token_entry.token_data = {
'refresh': {
'token': str(refresh),
'exp': str(refresh['exp']),
'exp': int(refresh['exp']), # Store expiration as integer
},
'access': {
'token': str(access),
'exp': str(access['exp']),
'exp': int(access['exp']), # Store expiration as integer
}
}
user_token_entry.save()

return Response({
'refresh': str(refresh),
'access': str(access),
}, status=status.HTTP_200_OK)
# Save the updated or newly created UserTokens entry
user_token_entry.save()

return Response({
'refresh': str(refresh),
'access': str(access),
}, status=status.HTTP_200_OK)

except Exception as e:
return Response({'error': 'Could not generate tokens', 'details': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
3 changes: 2 additions & 1 deletion Backend/rabbitmq/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ RUN wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.13.3/r
tar xvf rabbitmq-server-generic-unix-3.13.3.tar.xz -C /usr/local/bin/ && \
rm rabbitmq-server-generic-unix-3.13.3.tar.xz

# Add RabbitMQ binaries to PATH
# Add RabbitMQ binaries to PATH
ENV PATH=$PATH:$RABBITMQ_HOME/sbin

RUN rabbitmq-plugins enable rabbitmq_management
# Expose ports used by RabbitMQ
EXPOSE 5672 15672

Expand Down
10 changes: 0 additions & 10 deletions sample env

This file was deleted.

0 comments on commit 98018a9

Please sign in to comment.