diff --git a/services/register/register_service/auth_utils.py b/services/register/register_service/auth_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..f799772997bc614be1644391d0fed838cfd8534b --- /dev/null +++ b/services/register/register_service/auth_utils.py @@ -0,0 +1,8 @@ +import bcrypt + +def hash_password(password): + hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) + return hashed_password + +def check_password(input_password, stored_password): + return bcrypt.checkpw(input_password.encode('utf-8'), stored_password) \ No newline at end of file diff --git a/services/register/register_service/core/register_operations.py b/services/register/register_service/core/register_operations.py index d1d092181b1bc3e093891410d8e814b897a89edc..4cc5c37d09019f43cb8a7923c2525d6c3f48179a 100644 --- a/services/register/register_service/core/register_operations.py +++ b/services/register/register_service/core/register_operations.py @@ -2,12 +2,12 @@ from flask import Flask, jsonify, request, current_app from flask_jwt_extended import create_access_token from ..db.db import MongoDatabse from ..config import Config +from register_service import auth_utils import secrets import requests import json import sys -import bcrypt - + class RegisterOperations: def __init__(self): @@ -15,10 +15,6 @@ class RegisterOperations: self.mimetype = 'application/json' self.config = Config().get_config() - def hash_password(self, password): - hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) - return hashed_password - def register_user(self, username, password, description, cn, role): mycol = self.db.get_col_by_name(self.db.capif_users) @@ -26,7 +22,7 @@ class RegisterOperations: if exist_user: return jsonify("user already exists"), 409 - hashed_password = self.hash_password(password) + hashed_password = auth_utils.hash_password(password) user_info = dict(_id=secrets.token_hex(7), username=username, password=hashed_password, role=role, description=description, cn=cn, list_invokers=[], list_providers=[]) obj = mycol.insert_one(user_info) @@ -48,15 +44,14 @@ class RegisterOperations: try: - #exist_user = mycol.find_one({"username": username, "password": password}) exist_user = mycol.find_one({"username": username}) if exist_user is None: return jsonify("No user with these credentials"), 400 stored_password = exist_user["password"] - if not bcrypt.checkpw(password.encode('utf-8'), stored_password): - return jsonify("No user with these credentials"), 400 + if not auth_utils.check_password(password, stored_password): + return jsonify("No user with these credentials"), 400 access_token = create_access_token(identity=(username + " " + exist_user["role"])) url = f"http://{self.config['ca_factory']['url']}:{self.config['ca_factory']['port']}/v1/secret/data/ca" @@ -81,7 +76,7 @@ class RegisterOperations: return jsonify("No user with these credentials"), 400 stored_password = exist_user["password"] - if not bcrypt.checkpw(password.encode('utf-8'), stored_password): + if not auth_utils.check_password(password, stored_password): return jsonify("No user with these credentials"), 400 mycol.delete_one({"username": username})