|
|
|
from flask import Flask, request, make_response
|
|
|
|
import json
|
|
|
|
import dao
|
|
|
|
import sr_jwt as jwt
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/login', methods=['POST'])
|
|
|
|
def login():
|
|
|
|
cookie_jwt = request.cookies.get('jwt')
|
|
|
|
if cookie_jwt is not None:
|
|
|
|
if jwt.check_jwt(cookie_jwt):
|
|
|
|
return make_response(json.dumps({'result': 'success'}))
|
|
|
|
rdata = json.loads(request.data.decode()) # request data
|
|
|
|
username = rdata['username']
|
|
|
|
password = rdata['password']
|
|
|
|
if dao.valid(username, password):
|
|
|
|
response = make_response(json.dumps({'result': 'success'}))
|
|
|
|
response.set_cookie('jwt', jwt.generate_jwt(username))
|
|
|
|
return response
|
|
|
|
else:
|
|
|
|
return make_response(json.dumps({'result': 'fail'}))
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/register', methods=['POST'])
|
|
|
|
def register():
|
|
|
|
cookie_jwt = request.cookies.get('jwt')
|
|
|
|
if cookie_jwt is not None:
|
|
|
|
username = jwt.check_jwt(cookie_jwt)
|
|
|
|
else:
|
|
|
|
return make_response(json.dumps({'result': 'fail'}))
|
|
|
|
rdata = json.loads(request.data.decode()) # request data
|
|
|
|
program_type = rdata['program_type']
|
|
|
|
program_name = rdata['program_name']
|
|
|
|
if dao.register(username, program_type, program_name):
|
|
|
|
return make_response(json.dumps({'result': 'success'}))
|
|
|
|
return make_response(json.dumps({'result': 'fail'}))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run()
|