Loading services/register/register_service/auth_utils.py 0 → 100644 +8 −0 Original line number Original line Diff line number Diff line 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 services/register/register_service/core/register_operations.py +6 −11 Original line number Original line Diff line number Diff line Loading @@ -2,11 +2,11 @@ from flask import Flask, jsonify, request, current_app from flask_jwt_extended import create_access_token from flask_jwt_extended import create_access_token from ..db.db import MongoDatabse from ..db.db import MongoDatabse from ..config import Config from ..config import Config from register_service import auth_utils import secrets import secrets import requests import requests import json import json import sys import sys import bcrypt class RegisterOperations: class RegisterOperations: Loading @@ -15,10 +15,6 @@ class RegisterOperations: self.mimetype = 'application/json' self.mimetype = 'application/json' self.config = Config().get_config() 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): def register_user(self, username, password, description, cn, role): mycol = self.db.get_col_by_name(self.db.capif_users) mycol = self.db.get_col_by_name(self.db.capif_users) Loading @@ -26,7 +22,7 @@ class RegisterOperations: if exist_user: if exist_user: return jsonify("user already exists"), 409 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=[]) 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) obj = mycol.insert_one(user_info) Loading @@ -48,14 +44,13 @@ class RegisterOperations: try: try: #exist_user = mycol.find_one({"username": username, "password": password}) exist_user = mycol.find_one({"username": username}) exist_user = mycol.find_one({"username": username}) if exist_user is None: if exist_user is None: return jsonify("No user with these credentials"), 400 return jsonify("No user with these credentials"), 400 stored_password = exist_user["password"] 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 return jsonify("No user with these credentials"), 400 access_token = create_access_token(identity=(username + " " + exist_user["role"])) access_token = create_access_token(identity=(username + " " + exist_user["role"])) Loading @@ -81,7 +76,7 @@ class RegisterOperations: return jsonify("No user with these credentials"), 400 return jsonify("No user with these credentials"), 400 stored_password = exist_user["password"] 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 return jsonify("No user with these credentials"), 400 mycol.delete_one({"username": username}) mycol.delete_one({"username": username}) Loading Loading
services/register/register_service/auth_utils.py 0 → 100644 +8 −0 Original line number Original line Diff line number Diff line 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
services/register/register_service/core/register_operations.py +6 −11 Original line number Original line Diff line number Diff line Loading @@ -2,11 +2,11 @@ from flask import Flask, jsonify, request, current_app from flask_jwt_extended import create_access_token from flask_jwt_extended import create_access_token from ..db.db import MongoDatabse from ..db.db import MongoDatabse from ..config import Config from ..config import Config from register_service import auth_utils import secrets import secrets import requests import requests import json import json import sys import sys import bcrypt class RegisterOperations: class RegisterOperations: Loading @@ -15,10 +15,6 @@ class RegisterOperations: self.mimetype = 'application/json' self.mimetype = 'application/json' self.config = Config().get_config() 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): def register_user(self, username, password, description, cn, role): mycol = self.db.get_col_by_name(self.db.capif_users) mycol = self.db.get_col_by_name(self.db.capif_users) Loading @@ -26,7 +22,7 @@ class RegisterOperations: if exist_user: if exist_user: return jsonify("user already exists"), 409 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=[]) 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) obj = mycol.insert_one(user_info) Loading @@ -48,14 +44,13 @@ class RegisterOperations: try: try: #exist_user = mycol.find_one({"username": username, "password": password}) exist_user = mycol.find_one({"username": username}) exist_user = mycol.find_one({"username": username}) if exist_user is None: if exist_user is None: return jsonify("No user with these credentials"), 400 return jsonify("No user with these credentials"), 400 stored_password = exist_user["password"] 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 return jsonify("No user with these credentials"), 400 access_token = create_access_token(identity=(username + " " + exist_user["role"])) access_token = create_access_token(identity=(username + " " + exist_user["role"])) Loading @@ -81,7 +76,7 @@ class RegisterOperations: return jsonify("No user with these credentials"), 400 return jsonify("No user with these credentials"), 400 stored_password = exist_user["password"] 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 return jsonify("No user with these credentials"), 400 mycol.delete_one({"username": username}) mycol.delete_one({"username": username}) Loading