-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentity.py
292 lines (237 loc) · 8.74 KB
/
identity.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# flake8: noqa
from dataclasses import dataclass, field
from typing import Dict, List
from everyclass.rpc import ensure_slots
from everyclass.rpc.http import HttpRpc
BASE_URL = 'everyclass-identity'
def set_base_url(base_url: str) -> None:
global BASE_URL
BASE_URL = base_url
@dataclass
class GeneralResponse:
success: bool
err_code: field(default_factory=int)
message: str
@classmethod
def make(cls, dct: Dict) -> "GeneralResponse":
return cls(**ensure_slots(cls, dct))
@dataclass
class EmailSetPasswordResponse:
success: bool
err_code: field(default_factory=int)
message: str
student_id: field(default_factory=str)
@classmethod
def make(cls, dct: Dict) -> "EmailSetPasswordResponse":
return cls(**ensure_slots(cls, dct))
@dataclass
class RegisterByPasswordResponse:
success: bool
err_code: field(default_factory=int)
message: str
request_id: field(default_factory=str)
@classmethod
def make(cls, dct: Dict) -> "RegisterByPasswordResponse":
return cls(**ensure_slots(cls, dct))
@dataclass
class PasswordStrengthResponse:
success: bool
strong: bool
score: str
@classmethod
def make(cls, dct: Dict) -> "PasswordStrengthResponse":
return cls(**ensure_slots(cls, dct))
@dataclass
class Visitor:
name: str
student_id: str
last_semester: str
visit_time: str
@classmethod
def make(cls, dct: Dict) -> "Visitor":
return cls(**ensure_slots(cls, dct))
@dataclass
class VisitorsResponse:
success: bool
count: int
visitors: List[Visitor]
@classmethod
def make(cls, dct: Dict) -> "VisitorsResponse":
dct['visitors'] = [Visitor.make(x) for x in dct['visitors']]
return cls(**ensure_slots(cls, dct))
# err_code 除了以下每个注释里写的之外还包括 408,500,400
class Login:
@classmethod
def login(cls, student_id: str, password: str, captcha_ticket: str, captcha_rand: str, remote_addr: str):
"""登录
4001 用户名为空
4002 密码空
4003 验证码验证未通过
4004 学号不存在
4005 此学生未注册
4006 密码错误
"""
if not student_id:
ValueError("Empty Student ID")
if not password:
raise ValueError("Empty password")
resp = HttpRpc.call(method='GET',
url=f'{BASE_URL}/login',
data={'student_id' : student_id,
'password' : password,
'captcha_ticket': captcha_ticket,
'captcha_rand' : captcha_rand,
'remote_addr' : remote_addr},
retry=True)
return GeneralResponse.make(resp)
class Register:
@classmethod
def register(cls, student_id: str):
"""检查学号是否已注册
4001 用户名为空
4007 已经注册过了
"""
if not student_id:
raise ValueError("Empty student ID")
resp = HttpRpc.call(method='GET',
url=f'{BASE_URL}/register',
data={'student_id': student_id},
retry=True)
return GeneralResponse.make(resp)
@classmethod
def register_by_email(cls, student_id: str):
"""使用邮箱验证注册
4001 用户名为空
4007 已经注册过了
4501 未定义的错误
"""
if not student_id:
raise ValueError("Empty student ID")
resp = HttpRpc.call(method='POST',
url=f'{BASE_URL}/register/byEmail',
data={'student_id': student_id},
retry=True)
return GeneralResponse.make(resp)
@classmethod
def verify_email_token(cls, token: str):
"""验证邮箱 token
4008 token 为空
4009 token 无效
"""
if not token:
raise ValueError("Empty token")
resp = HttpRpc.call(method='GET',
url=f'{BASE_URL}/register/emailVerification',
data={'token': token},
retry=True)
return GeneralResponse.make(resp)
@classmethod
def email_set_password(cls, token: str, password: str):
"""邮件验证 设置密码
4008 token 为空
4002 密码为空
4009 token 无效
4010 密码强度太低
"""
if not token:
raise ValueError("Empty token")
if not password:
raise ValueError("Empty password")
resp = HttpRpc.call(method='POST',
url=f'{BASE_URL}/register/emailVerification',
data={'token' : token,
'password': password},
retry=True)
return EmailSetPasswordResponse.make(resp)
@classmethod
def register_by_password(cls, student_id: str, password: str, jw_password: str, captcha_ticket: str,
captcha_rand: str, remote_addr: str):
"""使用密码注册
4001 学号为空
4002 密码为空
4010 密码太弱
4003 验证码无效
4501 everyclass-auth 服务错误
"""
if not student_id:
raise ValueError("Empty student ID")
if not password:
raise ValueError("Empty password")
if not jw_password:
raise ValueError("Empty JW password")
resp = HttpRpc.call(method='POST',
url=f'{BASE_URL}/register/byPassword',
data={'student_id' : student_id,
'password' : password,
'jw_password' : jw_password,
'captcha_ticket': captcha_ticket,
'captcha_rand' : captcha_rand,
'remote_addr' : remote_addr},
retry=True)
return RegisterByPasswordResponse.make(resp)
@classmethod
def check_password_strength(cls, password: str):
"""检查密码强度"""
if not password:
raise ValueError("Empty password")
resp = HttpRpc.call(method='GET',
url=f'{BASE_URL}/register/passwordStrengthCheck',
data={'password': password},
retry=True)
return PasswordStrengthResponse.make(resp)
@classmethod
def password_verification_status(cls, request_id: str):
"""检查密码验证的状态
4011 请求 ID 为空
4100 无效的请求 ID
4007 已经注册过了
4200 验证成功
4201 下次查询
4202 密码错误
"""
if not request_id:
raise ValueError("Empty request ID")
resp = HttpRpc.call(method='GET',
url=f'{BASE_URL}/register/byPassword/statusRefresh',
data={'request_id': request_id},
retry=True)
return GeneralResponse.make(resp)
class UserCentre:
@classmethod
def set_privacy_level(cls, student_id: str, privacy_level: int):
"""设置隐私级别
4101 无效的隐私级别
4100 无效的请求
"""
if not student_id:
raise ValueError("Empty student ID")
if privacy_level is None: # avoid 0
raise ValueError("Empty privacy level")
resp = HttpRpc.call(method='POST',
url=f'{BASE_URL}/setPreference',
data={'privacy_level': privacy_level},
headers={'STUDENT_ID': student_id},
retry=True)
return GeneralResponse.make(resp)
@classmethod
def reset_calendar_token(cls, student_id: str):
"""清空日历 token
"""
if not student_id:
raise ValueError("Empty student ID")
resp = HttpRpc.call(method='POST',
url=f'{BASE_URL}/resetCalendarToken',
headers={'STUDENT_ID': student_id},
retry=True)
return GeneralResponse.make(resp)
@classmethod
def get_visitors(cls, student_id: str):
"""获得访客列表
"""
if not student_id:
raise ValueError("Empty student ID")
resp = HttpRpc.call(method='GET',
url=f'{BASE_URL}/visitors',
headers={'STUDENT_ID': student_id},
retry=True)
return VisitorsResponse.make(resp)