Skip to content

Commit

Permalink
send invalidate-tokens post request to token-service endpoint upone d…
Browse files Browse the repository at this point in the history
…elete_user in user-service views function, updated readme files in token_service and user_service
  • Loading branch information
mtoof committed Aug 15, 2024
1 parent 9cf0d6c commit c86cd47
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
16 changes: 14 additions & 2 deletions Backend/token_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Also the token service is responsible for refreshing the access token.

## Docker container configuration

Every single REST API endpoint has their own database. The database is a PostgreSQL database. The database name for all the endpoints is `postgres`. The database user and password for all the endpoints is inside the env file. The database port for all the endpoints is `5432`.
Every single REST API endpoint has their own database. The database is a PostgreSQL database. The database name for this endpoints is `token_service`. The database user and password for all the endpoints is inside the env file. The database port for all the endpoints is `5432`.

The requirements package is inside the requirements.txt file.
The tools.sh file is used to run the init_database.sh file and run the API.
Expand All @@ -15,5 +15,17 @@ The API runs on port 8000 and exposed to 8001.

## Tutorial to use the token_service

You can use the token_service by sending a POST request to the https://localhost:3000/auth/token/refresh/ endpoint with the refresh token in the header as a Bearer token and it will return a new access token.
There are three endpoints in the token_service. The endpoints are:
- `auth/token/refresh/` - This endpoint is used to refresh the access token.
- `auth/token/gen-tokens/` - This endpoint is used to generate the refresh and access tokens.
- `auth/token/invalidate-tokens/` - This endpoint is used by user-service logout or delete user to invalidate the refresh and access tokens.
- `auth/token/validate-token/` - This endpoint is used to validate the access token.

## The UserTokens model

The UserTokens model is used to store the refresh and access tokens. The UserTokens model has the following fields:
| Field | Type | Description |
| ---------- | ---------| ----------------------------- |
| id | Integer | The id of the user token |
| username | String | The username of the user |
| token_data | JSON | The refresh and access tokens |
53 changes: 41 additions & 12 deletions Backend/user_service/user_service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ Upon GET,PUT and DELETE requests for a user record, the user service will retrie

## Docker container configuration

Every single REST API endpoint has their own database. The database is a PostgreSQL database. The database name for all the endpoints is `postgres`. The database user and password for all the endpoints is inside the env file. The database port for all the endpoints is `5432`.
Every single REST API endpoint has their own database. The database is a PostgreSQL database. The database name for this endpoints is `user_service`. The database user and password for all the endpoints is inside the env file. The database port for all the endpoints is `5432`.

The requirements package is inside the requirements.txt file.
The run_consumer.sh file is used to run the consumer.py file inside the user_management/user_management folder. The consumer.py file is used to consume the message from the RabbitMQ message broker and check if the username and password are correct.
The tools.sh and run_consumer.sh files are running inside the Docker container using the supervisord service. The supervisord service is used to run multiple services inside the Docker container.
The tools.sh file is used to run the init_database.sh file and run the API.
The tools.sh file is used to run the API.
The API runs inside a virtual environment. The virtual environment is created inside the Docker container using command python3.12 -m venv venv. The virtual environment is activated using command source venv/bin/activate inside the tools.sh file.

The API runs on port 8000 and exposed to 8000.
The API runs on port 8000.

## Tutorial to use the user_service

Expand All @@ -33,7 +31,22 @@ You should send a JSON object with the following fields:

- `http://localhost:3000/user/` "List users records using GET method"
- `http://localhost:3000/user/<int:pk>/` "without angel brackets" "retrieve, update and delete user record using GET, PUT and DELETE methods respectively"
You can enable otp by sending a JSON object with the following fields:
```JSON
{
"otp_status": "True"
}
```
- `http://localhost:3000/user/login/` "login user using POST method"
- `http://localhost:3000/user/verifyotp/` "send user otp using POST method"
You should send a JSON object with the following fields:
```JSON
{
"username": "username",
"password": "password",
"otp": "otp"
}
```
- `http://localhost:3000/user/logout/` "logout user using POST method"
- `http://localhost:3000/user/<int:user_pk>/friends/` "List friends of a user using GET method"
The endpoint will return value is a JSON object with the following fields:
Expand All @@ -49,7 +62,7 @@ The endpoint will return value is a JSON object with the following fields:
- `http://localhost:3000/user/<int:user_pk>/request/` send friend request to a user in a JSON object using POST method the JSON object should contain the following fields:
```JSON
{
"username": "username",
"username": "username"
}
```
- `http://localhost:3000/user/<int:user_pk>/accept/<int:pk>/` accept friend request PUT method
Expand All @@ -76,12 +89,17 @@ The username and email are unique.
The User table consists of the following fields:
You can find it in user_management/user_management/users/models.py

| Field Name | Data Type | Description |
| ---------- | --------- | ---------------------------------- |
| id | Integer | Primary Key |
| username | String | User Name |
| email | String | User Email |
| password | String | User Password (Password is hashed) |
| Field Name | Data Type | Description |
| --------------- | --------- | ---------------------------------- |
| id | Integer | Primary Key |
| username | String | User Name |
| email | String | User Email |
| password | String | User Password (Password is hashed) |
| friends | ManyToMany| Friends of the user |
| avatar | Image | User Avatar |
| otp_status | Boolean | OTP Status |
| otp | Integer | OTP |
| otp_expiry_time | DateTime | OTP Expiry Time |

Later I will limit the access to the API using nginx reverse proxy and only the frontend will be able to access the API.

Expand All @@ -93,6 +111,15 @@ This document provides an overview of how WebSocket integration has been impleme

### Backend Setup

## GameRoom model
The GameRoom model is used to store the game room information. The GameRoom model consists of the following fields:

| Field Name | Data Type | Description |
| ---------- | --------------------------- | ----------- |
| room_name | String | Room Name |
| player1 | ForeignKey from UserProfile | Player 1 |
| player2 | ForeignKey from UserProfile | Player 2 |

#### Dependencies

Django Channels and Redis were installed:
Expand Down Expand Up @@ -289,3 +316,5 @@ To integrate WebSocket connections in the frontend, follow these steps:
### Summary

The setup included configuring Django Channels, Redis, and Nginx to support WebSocket connections. The frontend can connect to the WebSocket service and handle events to provide real-time updates to users.


Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def login(self, request):
# send post request to token-service
if user.otp_status:
user.otp = generate_password()
user.otp_expiry_time = now() + timedelta(seconds=30)
user.otp_expiry_time = now() + timedelta(minutes=1)
user.save()
send_mail(
'Verification Code',
Expand Down

0 comments on commit c86cd47

Please sign in to comment.