]> Softwares of Agnibho - librevax.git/blob - auth.py
Database dump
[librevax.git] / auth.py
1 # LibreVax
2 # Copyright (C) 2024 Dr. Agnibho Mondal
3 # This file is part of LibreVax.
4 # LibreVax is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5 # LibreVax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6 # You should have received a copy of the GNU General Public License along with LibreVax. If not, see <https://www.gnu.org/licenses/>.
7
8 from flask import session
9 from collections import namedtuple
10 import bcrypt, base64
11
12 _auth=namedtuple("auth", ["NONE", "ALL"])
13 auth=_auth("NONE", "ALL")
14
15 def login(cursor, user, password, center):
16 try:
17 result=cursor.execute("SELECT * FROM users WHERE user=?", (user,))
18 record=result.fetchone()
19 if(record is not None and bcrypt.checkpw(password.encode("utf-8"), base64.b64decode(record["hash"]))):
20 session["user"]=record["user"]
21 session["auth"]=_parse_auth(record["auth"])
22 result=cursor.execute("SELECT * FROM multicenter WHERE mid=?", (center,))
23 record=result.fetchone()
24 session["mid"]=record["mid"]
25 session["center"]=record["center"]
26 return True
27 else:
28 return False
29 except Exception as e:
30 raise(e)
31 return False
32
33 def changePass(cursor, user, new, old=None):
34 try:
35 if(old is not None):
36 result=cursor.execute("SELECT * FROM users WHERE user=?", (user,))
37 record=result.fetchone()
38 if(record is not None and bcrypt.checkpw(old.encode("utf-8"), base64.b64decode(record["hash"]))):
39 cursor.execute("UPDATE users SET hash=? WHERE user=?", (base64.b64encode(bcrypt.hashpw(new.encode("utf-8"), bcrypt.gensalt())), user))
40 return True
41 else:
42 return False
43 else:
44 cursor.execute("UPDATE users SET hash=? WHERE user=?", (base64.b64encode(bcrypt.hashpw(new.encode("utf-8"), bcrypt.gensalt())), user))
45 return True
46 except Exception as e:
47 raise(e)
48 return False
49
50 def changeAuth(cursor, user, auth):
51 try:
52 result=cursor.execute("SELECT * FROM users WHERE user=?", (user,))
53 record=result.fetchone()
54 if(record is not None):
55 cursor.execute("UPDATE users SET auth=? WHERE user=?", (auth, user))
56 return True
57 else:
58 return False
59 return True
60 except Exception as e:
61 raise(e)
62 return False
63
64 def new(cursor, user, pwd, auth):
65 try:
66 cursor.execute("INSERT INTO users (user, hash, auth) VALUES (?, ?, ?)", (user, base64.b64encode(bcrypt.hashpw(pwd.encode("utf-8"), bcrypt.gensalt())), auth))
67 return True
68 except Exception as e:
69 raise(e)
70 return False
71
72 def delete(cursor, user):
73 try:
74 cursor.execute("DELETE FROM users WHERE user=?", (user,))
75 return True
76 except Exception as e:
77 raise(e)
78 return False
79
80 def switch(cursor, center):
81 try:
82 result=cursor.execute("SELECT * FROM multicenter WHERE mid=?", (center["center"],))
83 record=result.fetchone()
84 session["mid"]=record["mid"]
85 session["center"]=record["center"]
86 return True
87 except Exception as e:
88 raise(e)
89 return False
90
91 def list(cursor):
92 try:
93 result=cursor.execute("SELECT * FROM users")
94 record=result.fetchall()
95 return record
96 except Exception as e:
97 return False
98
99 def _parse_auth(text):
100 try:
101 if(text.upper()==auth.ALL):
102 return auth.ALL
103 else:
104 return auth.NONE
105 return auth.NONE
106 except Exception as e:
107 return auth.NONE
108
109 def access():
110 try:
111 return session["auth"]
112 except:
113 return "none"
114
115 def logout():
116 try:
117 del session["user"]
118 del session["auth"]
119 del session["mid"]
120 del session["center"]
121 except:
122 pass