()
| 15 | |
| 16 | @server.route('/login', methods=['POST']) |
| 17 | def login(): |
| 18 | auth_table_name = os.getenv('AUTH_TABLE') |
| 19 | auth = request.authorization |
| 20 | if not auth or not auth.username or not auth.password: |
| 21 | return 'Could not verify', 401, {'WWW-Authenticate': 'Basic realm="Login required!"'} |
| 22 | |
| 23 | conn = get_db_connection() |
| 24 | cur = conn.cursor() |
| 25 | query = f"SELECT email, password FROM {auth_table_name} WHERE email = %s" |
| 26 | res = cur.execute(query, (auth.username,)) |
| 27 | |
| 28 | if res is None: |
| 29 | user_row = cur.fetchone() |
| 30 | email = user_row[0] |
| 31 | password = user_row[1] |
| 32 | |
| 33 | if auth.username != email or auth.password != password: |
| 34 | return 'Could not verify', 401, {'WWW-Authenticate': 'Basic realm="Login required!"'} |
| 35 | else: |
| 36 | return CreateJWT(auth.username, os.environ['JWT_SECRET'], True) |
| 37 | else: |
| 38 | return 'Could not verify', 401, {'WWW-Authenticate': 'Basic realm="Login required!"'} |
| 39 | |
| 40 | def CreateJWT(username, secret, authz): |
| 41 | return jwt.encode( |
nothing calls this directly
no test coverage detected