build.resources.models.verify_hash()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nop 2
1
from flask import Flask
2
import datetime
3
import psycopg2
4
5
from passlib.hash import pbkdf2_sha256 as sha256
6
from run import create_app
7
from config import dbconfig, filename ,section, app_config, DATABASE_URL
8
from flask_bcrypt import Bcrypt
9
10
app = Flask(__name__)
11
12
def insert_to_db(self, username, password, firstname, lastname, role, created_on):
13
    """insert a new user into database"""
14
15
    query = """INSERT INTO tb_users (username, password, firstname, lastname,role, created_on)
16
                VALUES(%s,%s,%s,%s,%s,%s)"""
17
    
18
    conn = None
19
    user_id = None
20
    try:
21
        #params = dbconfig(filename, section)
22
        conn = psycopg2.connect(DATABASE_URL())
23
24
        cur = conn.cursor()
25
        cur.execute(query,(username, password, firstname, 
26
                    lastname,role, created_on,))
27
28
               
29
        conn.commit()
30
        user_id = cur.fetchone()[0]
31
        cur.close()
32
    except (Exception, psycopg2.DatabaseError) as error:
33
        return error
34
    finally:
35
        if conn is not None:
36
            conn.close()
37
    return user_id
38
39
def find_by_username(username):
40
    query = """SELECT username,password FROM tb_users WHERE username=(%s)"""
41
    
42
    conn = None
43
    result = None
44
    try:
45
        #params = dbconfig(filename, section)
46
        conn = psycopg2.connect(DATABASE_URL())
47
48
        cur = conn.cursor()
49
        cur.execute(query,(username,))
50
51
        result = cur.fetchone()
52
        
53
        #print(result)
54
        
55
        cur.close()
56
57
    except (Exception, psycopg2.DatabaseError) as error:
58
        print(error)
59
    finally:
60
        if conn is not None:
61
            conn.close()
62
    return result
63
64
def return_all():
65
    query = """select array_to_json(array_agg(row_to_json(t))) from (  
66
                SELECT * FROM tb_users) t"""
67
    
68
    conn = None
69
    result = None
70
71
    try:
72
        #params = dbconfig(filename, section)
73
74
        conn = psycopg2.connect(DATABASE_URL())
75
76
        cur = conn.cursor()
77
        cur.execute(query)
78
79
        result = cur.fetchall()
80
81
        cur.close()
82
83
84
    except (Exception, psycopg2.DatabaseError) as error:
85
        print (error)
86
    finally:
87
        if conn is not None:
88
            conn.close()
89
    return result
90
91
92
def hash_password(password):
93
    #generate hashed string to store in db
94
    return sha256.hash(password)
95
96
97
def verify_hash(password, hash):
98
    #check the given pass
99
    return sha256.verify(password, hash)
100
101
class RevokedTokenModel(object):
102
    pass