The user microservice

The user microservice is responsible for handling anything related to user profile management. The service facilitates the following functionalities:

  • Registration of new users
  • Management of user profiles
  • Authentication of existing users
  • Generating unique authentication tokens for the user to log in with
  • Providing user authentication functionality to other services

For this service to operate, we need to have the following two database models:

  • User database model: The user database model is responsible for the management of the user records, such as their username, hashed passwords, and so on.
  • Token database model: The token database model is responsible for storing information about the tokens that has been generated for a particular user to use to authenticate. For our purposes, every user can only have a single token at any given point in time, and this token will be valid for only 60 minutes from the point of generation, after which the user needs to log in again.

So, let's move on to building the database models for this service.

The following code snippet shows the definition of the database models:

'''
File: models.py
Description: The models for the User service.
'''
from user_service.user_service import db
import datetime

class User(db.Model):
"""User database model.

The User database model is used to store the information related to the individual users
allowing for their identification and authentication.
"""

id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(25), unique=True, nullable=False)
password= db.Column(db.String(255), nullable=False)
email = db.Column(db.String(255), nullable=False, unique=True)

def __repr__(self):
"""Provide a representation of the Model."""
return "<User {}>".format(self.username)

class Token(db.Model):
"""User Authetication Token Model.

The authentication token model is used to store the authentication tokens
for a given user which can be used to authenticate the user with the
service.
"""

id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey(User.id))
auth_token = db.Column(db.String(64), nullable=False, unique=True)
token_timestamp = db.Column(db.DateTime, nullable=False, default=datetime.datetime.now())

def __repr__(self):
"""Provide a representation of the model."""
return "<Token {}>".format(self.id)

With this code snippet, we have defined two important data models for the user service: the user model and the token model. Here, inside the token model, we can see that the model depends upon the user model for storing and relating the authentication tokens to a user account.

Once the models have been developed, it's time for us to work on the API of the user service. The user service provides the following set of APIs to work with:

  • /auth/register: This API is responsible for taking in the details for a new user and registering them with the service.
  • /auth/login: This API is responsible for taking in the login credentials of the user and authenticating them with the service to validate the user. Once the user is validated, the endpoint generates an authentication token.
  • /auth/validate: This API endpoint is responsible for the validation of the authentication token that is given to it. Upon successful validation, the endpoint returns the user ID of the authenticated user.

The following snippet of code shows the implementation of the API endpoints:

def check_required_fields(req_fields, input_list):
"""Check if the required fields are present or not in a given list.

Keyword arguments:
req_fields -- The list of fields required
input_list -- The input list to check for

Returns:
Boolean
"""

if all(field in req_fields for field in input_list):
return True
return False

@app.route('/auth/register', methods=['POST'])
def user_registration():
"""Handle the user registration."""

required_fields = ['username', 'email', 'password']
response = {} # Initialize a response dictionary
user_data = request.get_json()
if not check_required_fields(required_fields, user_data.keys()):
response['message'] = "Required fields are missing"
return jsonify(response), 400

# Create a user object
username = user_data['username']
password = generate_password_hash(user_data['password'])
email = user_data['email']

user = User(username=username, password=password, email=email)
db.session.add(user)
try:
db.session.commit()
except Exception:
response['message'] = 'Unable to register the user'
return jsonify(response), 400

response['message'] = "User registration successful"
return jsonify(response), 200

@app.route('/auth/login', methods=['POST'])
def user_login():
"""Handle the user login."""

...
# The remaining code can be found inside the code directory of this chapter

With the preceding code, we have completed the deployment of the required API that will facilitate the communication with the user service.

Now, to get this service up and running, run the following command inside the user_service directory of the code repository for this chapter:

python3 run.py

Once this command is executed, the user service will start to run on http://localhost:5000. To test whether the service is working fine, navigate to http://localhost:5000/ping and see whether the web page shows the response PONG is generated.

Once this is done, we will be ready to build our to-do manager service, which will help us record our todo items in a list.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.163.229