-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
141 lines (109 loc) · 4.32 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
from flask import Flask, render_template
import logging
import csv
import json
import validate
import accountServices
import handshake
import os
app = Flask(__name__)
app.debug = False
# Quiet down the web requests router
weblog = logging.getLogger('werkzeug')
weblog.setLevel(logging.ERROR)
# Perform Log setup
if not os.path.isdir("log"):
os.mkdir("log")
logfile = os.path.join(os.path.abspath("log"), "accountService.log")
logging.basicConfig(level=logging.INFO, filename=logfile)
logging.info("Starting CV Account System")
def init():
configLog = logging.getLogger("config")
config = dict()
config["ACL"] = dict()
configDir = os.path.abspath("config")
with open(os.path.join(configDir, "access.list"), 'r') as f:
accounts = csv.reader(f)
for account in accounts:
if len(account) == 0:
configLog.debug("Skipping blank line in file")
continue
if any("#" in s for s in account[0]):
configLog.debug("Drop comment %s", account)
continue
configLog.debug("Loaded account record %s", account)
config["ACL"][account[2]] = [account[1], account[0]]
configLog.info("Loaded %s account records", len(config["ACL"]))
with open(os.path.join(configDir, "words.txt"), 'r') as f:
config["WORDS"] = [s.strip() for s in f.readlines()]
configLog.info("Loaded %s words", len(config["WORDS"]))
with open(os.path.join(configDir, "blacklist.txt"), 'r') as f:
config["BLACKLIST"] = [s.strip() for s in f.readlines()]
configLog.info("Loaded %s blacklisted phrases", len(config["BLACKLIST"]))
with open(os.path.join(configDir, "settings.json"), 'r') as f:
config.update(json.load(f))
configLog.info("Loaded config file")
return config
@app.route("/")
def index():
return render_template('index.html')
@app.route("/version")
def version():
return "CV Account System - Version 0.0.2"
@app.route("/ums/validate/netID/<netID>")
def realtimeNetID(netID):
return json.dumps(validate.netID(netID))
@app.route("/ums/validate/uname/<user>")
def realtimeUsername(user):
return json.dumps(validate.user(user))
@app.route("/ums/validate/exists/byNetID/<netID>")
def accountByNetID(netID):
return json.dumps(acctMgr.netIDExists(netID))
@app.route("/ums/validate/exists/byUID/<uid>")
def accountByUID(uid):
return json.dumps(acctMgr.uidExists(uid))
@app.route("/ums/provision/<netID>/<user>/")
def IDConfirm(netID, user):
if validate.netID(netID) and validate.user(user):
handshake.send(netID, user)
return json.dumps(True)
@app.route("/ums/provision/<netID>/<user>/<hmac>/<time>/")
def provisionAcct(netID, user, hmac, time):
if handshake.verify(netID, user, hmac, time):
password = acctMgr.mkPassword()
if acctMgr.provision(netID, user, password):
handshake.sendPassword(netID, user, password)
return json.dumps("Your password has been emailed to you")
else:
return json.dumps("An error occured, please contact an admin")
@app.route("/ums/changePassword/<netID>")
def passwordHandshake(netID):
if validate.netID(netID):
handshake.send(netID, acctMgr.uidFromNetID(netID), False)
return json.dumps(True)
else:
return json.dumps(False)
@app.route("/ums/changePassword/<netID>/<user>/<hmac>/<time>/")
def chPassword(netID, user, hmac, time):
if handshake.verify(netID, user, hmac, time):
password = acctMgr.mkPassword()
if acctMgr.chPassword(user, password):
handshake.sendPassword(netID, user, password)
return "Your password has been emailed to you"
else:
return "Your password could not be reset, please try again later"
else:
return "An error occured, perhaps you have an old link?"
# ------------- Begin Program Setup --------------
# Perform global config loading
config = init()
# Create core objects
validate = validate.Validate(config)
handshake = handshake.Handshake(config)
acctMgr = accountServices.Manager(config)
# If we're being loaded without uwsgi, configure the internal server
if __name__ == "__main__":
host = config["SETTINGS"]["serverAddr"].split(":")[0]
port = int(config["SETTINGS"]["serverAddr"].split(":")[1]) or 5000
app.run(host, port)