-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
196 lines (183 loc) · 7.52 KB
/
auth.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# import functools
# import datetime
# import uuid
# import os
# from flask import Blueprint, request
# from mongoengine import Document, StringField, ReferenceField, DateTimeField,EmbeddedDocumentField,ListField,EmbeddedDocument
# from werkzeug.utils import secure_filename
# from flask import Flask,request
# from flask_mongoengine import MongoEngine
# #from werkzeug.security import check_password_hash, generate_password_hash
# bp = Blueprint('auth', __name__, url_prefix='/auth')
# class User(Document):
# username = StringField(required=True)
# password = StringField(required=True)
# classname = StringField(required=True)
# phonenum = StringField(required=True)
# type_ = StringField(required=True)
# class Token(Document):
# user = ReferenceField('User',required=True)
# token = StringField(required=True)
# expire = DateTimeField(required=True)
# # @bp.route('/')
# # def hello_world():
# # return 'Hello!'
# @bp.route('/user_register',methods=['POST'])
# def user_register():
# if 'username' in request.form:
# username = request.form['username']
# else:
# return {'status':'failed','data':'username required'}
# if 'password' in request.form:
# password = request.form['password']
# else:
# return {'status':'failed','data':'password required'}
# # if 'confirm_password' in request.form:
# # confirm_password = request.form['confirm_password']
# # else:
# # return {'status':'failed','data':'confirm password required'}
# if 'type_' in request.form:
# type_ = request.form['type_']
# else:
# return {'status':'failed','data':'type required'}
# if 'classname' in request.form:
# classname = request.form['classname']
# if 'phonenum' in request.form:
# phonenum = request.form['phonenum']
# if len(phonenum)!=11 or ' ' in phonenum:
# return {'status':'failed','data':'invalid phonenum'}
# if ' ' in username:
# return {'status':'failed','data':'invalid username'}
# if type_ not in ['student','teacher']:
# return {'status':'failed','data':'invalid type'}
# # if password != confirm_password:
# # return {'status':'failed','data':'the confirm password differs from the former one'}
# if len(password)<8 or len(password)>16:
# return {'status':'failed','data':'the length of password should be between 8 and 16'}
# if ' ' in password:
# return {'status':'failed',"data":'illegal character in password'}
# if User.objects(username=username).count()>0:
# return {'status':'failed',"data":"duplicated username"}
# User(username=username,password=password,type_=type_,classname=classname,phonenum=phonenum).save()
# return {'status':"success"}
# def auth(username,password):
# users = User.objects(username=username)
# if users.count()==0:
# return False
# if users[0].password != password: #?为何要【0】
# return False
# return users[0]
# def auth_by_token(token):
# token=Token.objects(token=token).first()
# if not token:
# return False
# if datetime.datetime.now()>token.expire:
# return False
# return token.user
# #是不是要改成by token?
# @bp.route('/user_login',methods=['POST'])
# def user_login():
# if 'username' in request.form:
# username = request.form['username']
# else:
# return {'status':'failed','data':'username required'}
# if 'password' in request.form:
# password = request.form['password']
# else:
# return {'status':'failed','data':"password required"}
# user = auth(username,password)
# if not user:
# return {"status":"failed","data":'invalid username or password'}
# #id = str(uuid.uuid4()).replace('-','')
# id = str(uuid.uuid4())
# token = Token(user=user,token=id,expire=datetime.datetime.now()+datetime.timedelta(days=30))
# token.save()
# return {"status":"success","data":{
# "token":id
# }}
# #改密码要改token吗?
# @bp.route('/change_password',methods=['POST'])
# def change_password():
# token = request.form.get('token')
# if not token:
# return {"status":"failed","data":"token required"}
# user = auth_by_token(token)
# if not user:
# return {"status":"failed",'data':"invalid token"}
# if 'old_password' in request.form:
# old_password = request.form['old_password']
# else:
# return {'status':'failed','data':'old password required'}
# if 'new_password' in request.form:
# new_password = request.form['new_password']
# else:
# return {'status':'failed','data':'new password required'}
# # 前端来做
# # if 'confirm_password' in request.form:
# # confirm_password = request.form['confirm_password']
# # else:
# # return {'status':'failed','data':'confirm password required'}
# # if new_password != confirm_password:
# # return {'status':'failed','data':'the confirm password differs from the former one'}
# if old_password == new_password:
# return {'status':'failed','data':'the new password is the same as the old'}
# if len(new_password)<8 or len(new_password)>16:
# return {'status':'failed','data':'the length of password should be between 8 and 16'}
# if ' ' in new_password:
# return {'status':'failed',"data":'illegal character in password'}
# user.password=new_password
# user.save()
# return {'status':'success','data':user.password}
# #修改其余个人资料,是点一个属性的修改按钮修改一个请求吗?
# @bp.route('/change_username',methods=['POST'])
# def change_username():
# token = request.form.get('token')
# if not token:
# return {"status":"failed","data":"token required"}
# user = auth_by_token(token)
# if not user:
# return {"status":"failed",'data':"invalid token"}
# if 'new_username' in request.form:
# new_username = request.form['new_username']
# user.username=new_username
# user.save()
# return {'status':'success'}
# else:
# return {'status':'failed','data':'new_username required'}
# @bp.route('/change_classname',methods=['POST'])
# def change_classname():
# token = request.form.get('token')
# if not token:
# return {"status":"failed","data":"token required"}
# user = auth_by_token(token)
# if not user:
# return {"status":"failed",'data':"invalid token"}
# if 'new_classname' in request.form:
# new_classname = request.form['new_classname']
# user.classname=new_classname
# user.save()
# return {'status':'success'}
# else:
# return {'status':'failed','data':'new_classname required'}
# @bp.route('/change_phonenum',methods=['POST'])
# def change_phonenum():
# token = request.form.get('token')
# if not token:
# return {"status":"failed","data":"token required"}
# user = auth_by_token(token)
# if not user:
# return {"status":"failed",'data':"invalid token"}
# if 'new_phonenum' in request.form:
# new_phonenum = request.form['new_phonenum']
# user.phonenum=new_phonenum
# user.save()
# return {'status':'success'}
# else:
# return {'status':'failed','data':'new_phonenum required'}
# @bp.route('/user_logout',methods=['POST'])
# def user_logout():
# token = request.form.get('token')
# if not token:
# return {"status":"failed","data":"token required"}
# Token.objects(token=token).delete()
# return {'status':'success'}