forked from Marcelo-Theodoro/lambdatotp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (34 loc) · 1.22 KB
/
app.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
from chalice import Chalice, Response
from chalice import BadRequestError, ConflictError, NotFoundError
from chalicelib.services import create_new_user, verify_user_code, delete_user
app = Chalice(app_name='lambdatotp')
@app.route('/', methods=['POST'])
def create_user():
body = app.current_request.json_body
try:
user_id = body['user']
except KeyError:
raise BadRequestError('No user found in the request body')
try:
response = create_new_user(user_id)
except ValueError:
raise ConflictError('User already exists')
return Response(body=response, status_code=201)
@app.route('/verify', methods=['GET'])
def verify():
user = app.current_request.query_params.get('user')
code = app.current_request.query_params.get('code')
if not code or not user:
raise BadRequestError('No code or user found in the query string')
try:
return bool(verify_user_code(user, code))
except ValueError:
raise NotFoundError('User not found')
@app.route('/', methods=['DELETE'])
def delete(user):
try:
delete_user(user)
except ValueError:
raise NotFoundError('User not found')
else:
return Response(body='', status_code=204)