Commit 102093d2 authored by Alex Kakiris's avatar Alex Kakiris
Browse files

Refactor "Register user password must be hashed before store on DB"

parent ef1a2740
Loading
Loading
Loading
Loading
Loading
+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
+6 −11
Original line number Original line Diff line number Diff line
@@ -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:


@@ -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)
@@ -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)


@@ -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"]))
@@ -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})