# __ Imports #
## ___Standered imports__
import os
import sys
## __ non statered imports
from flask import Flask
import secrets
import hashlib
from FileWrite import FileWrite
push = FileWrite.FileWriter.push
# End of __imports__
hashedKeys = []
for file in os.scandir('keys/API_KEYS'):
file = open(file, 'r')
hashedKeys.append(file.read(1))
file.close()
app = Flask('app')
@app.route('/api/<string:key>/<string:username>')
def Api(Key, username):
key = checkkey(Key, username)
if key:
return 'Welcome to the api'
@app.route('/api/User/<string:status>/<string:username>/<string:password>')
def user(status, username, password):
#return "broken"
folder = 'keys/Accounts'
if status == "temp":
for file in os.scander(folder):
if file.isfile:
if username in file:
file = open(folder + file, 'r')
pswd_hsh = file.read(1)
if hashlib.pbkdf2_hmac(user, password,
file.read(2)) == pswd_hsh:
userfound = True
temp_Api_Key = generate_api_key
return temp_Api_Key
else:
return "wrong username or password"
if not (userfound): return "wrong username or password"
elif status == "sign_up":
salt = secrets.token_hex(16)
psw_hash = hashlib.pbkdf2_hmac('sha256', password.encode(), salt.encode(),5)
push(folder + username, psw_hash, '.psw')
push(folder + username, salt, '.psw')
apikey = generate_api_key()
api_hash = hashlib.pbkdf2_hmac('sha256', apikey.encode(), salt.encode(),5)
push('keys/API_KEYS/' + username + psw_hash, api_hash, 'psw')
global hashedKeys
hashedKeys.append(api_hash)
return apikey
def checkkey(key, username):
salt = open('.keys.Accounts.' + username + '.psw')
key = hashlib.pbkdf2_hmac('sha256', key, salt.read(2).encode('utf-8'),5)
salt.close
if key in hashedKeys:
return True
else:
return False
def generate_api_key():
return secrets.token_urlsafe(16)
app.debug = True
app.run(host='0.0.0.0', port=8080,debug = True)
import datetime
import os
class functionsclass:
global path
path = os.path.realpath('')
def __init__(self):
#functions.__init startup function
print("functions active")
def log1(
self,
What,
Where,
):
#log1 is a function that writes log messages to the log file
tim = datetime.time().__str__()
try:
with open(os.path.join(path + "log.txt"), "x") as f:
f.write("time:" + tim + " " + What + " at " + Where + "\n")
except:
with open(os.path.join(path + "log.txt"), 'r') as f :
f.write("time:" + tim + " " + What + " at " + Where + "\n")
def push(self,filename,send,exention):
try:
with open(os.path.join(path + "/" + filename + "." + exention ), 'x') as f:
f.write(send + "\n")
functionsclass.log1("created and wrote " + send, " at " + filename)
except:
with open(os.path.join(path + "/" + filename + "." + exention), 'a') as f:
f.append(send + "\n")
f.close()
functionsclass.log1("wrote " + send, " at " + path + "/" + filename)
from functions1.funtions import functionsclass
functions = functionsclass(functionsclass)
class FileWriter:
# ----------------------------------------------------------------
# front class Log push , and file push
def push(filename,Content,exention):
functions.push(filename,Content,exention)
def logs1(What,Where,):
functions.logs1(What,Where)
def __init__(self):
#setup the file writer
self.__filename
self.logs1("init", "logs1.txt")
self.push("start", "Welcome to file writer",".txt")
def Start(self):
print("Started")