Move work of extracting token from cookie to a @property.

This commit is contained in:
Ethan Dalool 2020-10-12 21:20:44 -07:00
parent 6ce69da7a6
commit f23ed27683

View file

@ -63,6 +63,19 @@ class RequestHandler(http.server.BaseHTTPRequestHandler):
self.password = password self.password = password
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@property
def auth_token(self):
cookie = self.headers.get('Cookie')
if not cookie:
return None
cookie = http.cookies.SimpleCookie(cookie)
token = cookie.get(TOKEN_COOKIE_NAME)
if not token:
return None
return token
def check_password(self, attempt): def check_password(self, attempt):
if self.password is None: if self.password is None:
return True return True
@ -79,11 +92,7 @@ class RequestHandler(http.server.BaseHTTPRequestHandler):
if self.headers.get('password', None) == self.password: if self.headers.get('password', None) == self.password:
return True return True
if self.headers.get('Cookie'): if self.accepted_tokens is not None and self.auth_token in self.accepted_tokens:
cookie = http.cookies.SimpleCookie()
cookie.load(self.headers.get('Cookie'))
token = cookie.get(TOKEN_COOKIE_NAME)
if token and token.value in self.accepted_tokens:
return True return True
return False return False