Add global const TOKEN_COOKIE_NAME.

master
Ethan Dalool 2020-10-10 08:43:12 -07:00
parent 91cf3af63b
commit fa86f1b393
1 changed files with 3 additions and 2 deletions

View File

@ -53,6 +53,7 @@ PASSWORD_PROMPT_HTML = '''
''' '''
ROOT_DIRECTORY = pathclass.Path(os.getcwd()) ROOT_DIRECTORY = pathclass.Path(os.getcwd())
TOKEN_COOKIE_NAME = 'simpleserver_token'
class RequestHandler(http.server.BaseHTTPRequestHandler): class RequestHandler(http.server.BaseHTTPRequestHandler):
def __init__(self, *args, passw=None, accepted_tokens=None, **kwargs): def __init__(self, *args, passw=None, accepted_tokens=None, **kwargs):
@ -79,7 +80,7 @@ class RequestHandler(http.server.BaseHTTPRequestHandler):
if self.headers.get('Cookie'): if self.headers.get('Cookie'):
cookie = http.cookies.SimpleCookie() cookie = http.cookies.SimpleCookie()
cookie.load(self.headers.get('Cookie')) cookie.load(self.headers.get('Cookie'))
token = cookie.get('token') token = cookie.get(TOKEN_COOKIE_NAME)
if token and token.value in self.accepted_tokens: if token and token.value in self.accepted_tokens:
return True return True
@ -223,7 +224,7 @@ class RequestHandler(http.server.BaseHTTPRequestHandler):
if self.check_password(attempt): if self.check_password(attempt):
cookie = http.cookies.SimpleCookie() cookie = http.cookies.SimpleCookie()
token = random_hex(32) token = random_hex(32)
cookie['token'] = token cookie[TOKEN_COOKIE_NAME] = token
self.accepted_tokens.add(token) self.accepted_tokens.add(token)
self.send_response(302) self.send_response(302)