Skip to content

Commit 5e2d902

Browse files
authored
Merge pull request #1627 from tonypzy/refactor/check-auth-handler-helper
refactor: route check_auth through handler authorization helper
2 parents a1d6356 + 48c0413 commit 5e2d902

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

py/visdom/server/handlers/base_handlers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import logging
1414
import traceback
1515
import http.client
16+
import time
1617

1718
import tornado.web
1819
import tornado.websocket
@@ -65,6 +66,14 @@ def initialize(self, app=None):
6566
self.max_old_content = app.max_old_content
6667
self.max_image_history = app.max_image_history
6768

69+
def is_authorized(self):
70+
"""Update access time and validate authentication for protected methods."""
71+
self.last_access = time.time()
72+
if self.login_enabled and not self.current_user:
73+
self.set_status(401)
74+
return False
75+
return True
76+
6877
def __init__(self, *request, **kwargs):
6978
self.include_host = False
7079
super(BaseHandler, self).__init__(*request, **kwargs)

py/visdom/utils/server_utils.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import json
2121
import logging
2222
import os
23-
import time
2423
import errno
2524
from collections import OrderedDict
2625

@@ -51,10 +50,7 @@ def check_auth(f):
5150
"""
5251

5352
def _check_auth(handler, *args, **kwargs):
54-
# TODO this should call a shared method of the handler
55-
handler.last_access = time.time()
56-
if handler.login_enabled and not handler.current_user:
57-
handler.set_status(401)
53+
if not handler.is_authorized():
5854
return
5955
f(handler, *args, **kwargs)
6056

0 commit comments

Comments
 (0)