Authenticate user from token in cookie.
(connection, request)
| 134 | |
| 135 | |
| 136 | async def cookie_auth(connection, request): |
| 137 | """Authenticate user from token in cookie.""" |
| 138 | if "Upgrade" not in request.headers: |
| 139 | template = pathlib.Path(__file__).with_name(request.path[1:]) |
| 140 | body = template.read_bytes() |
| 141 | headers = Headers( |
| 142 | { |
| 143 | "Date": email.utils.formatdate(usegmt=True), |
| 144 | "Connection": "close", |
| 145 | "Content-Length": str(len(body)), |
| 146 | "Content-Type": CONTENT_TYPES[template.suffix], |
| 147 | } |
| 148 | ) |
| 149 | return Response(200, "OK", headers, body) |
| 150 | |
| 151 | token = get_cookie(request.headers.get("Cookie", ""), "token") |
| 152 | if token is None: |
| 153 | return connection.respond(http.HTTPStatus.UNAUTHORIZED, "Missing token\n") |
| 154 | |
| 155 | user = get_user(token) |
| 156 | if user is None: |
| 157 | return connection.respond(http.HTTPStatus.UNAUTHORIZED, "Invalid token\n") |
| 158 | |
| 159 | connection.username = user |
| 160 | |
| 161 | |
| 162 | def check_credentials(username, password): |