-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add utility methods to User class #1259
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -81,6 +81,39 @@ def fill_defaults(self): | |||||
if not self.display_name: | ||||||
self.display_name = self.name | ||||||
|
||||||
def to_string(self) -> str: | ||||||
"""Serialize a user to a string for storage in a cookie | ||||||
If overriding in a subclass, make sure to define from_string as well. | ||||||
Default is just the user's username. | ||||||
""" | ||||||
# default: username is enough | ||||||
Comment on lines
+87
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how this comment comes into play here. Is the help string and comment valid? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment and docstring are out of date, behavior changed in #863. |
||||||
cookie = json.dumps( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No cookie...
Suggested change
|
||||||
{ | ||||||
"username": self.username, | ||||||
"name": self.name, | ||||||
"display_name": self.display_name, | ||||||
"initials": self.initials, | ||||||
"color": self.color, | ||||||
} | ||||||
) | ||||||
return cookie | ||||||
|
||||||
@classmethod | ||||||
def from_string(cls, serialized_user: str) -> User: | ||||||
"""Inverse of user_to_cookie""" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
user = json.loads(serialized_user) | ||||||
try: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the try/except necessary? Since |
||||||
return User( | ||||||
user["username"], | ||||||
user["name"], | ||||||
user["display_name"], | ||||||
user["initials"], | ||||||
None, | ||||||
user["color"], | ||||||
) | ||||||
except: | ||||||
raise | ||||||
|
||||||
|
||||||
def _backward_compat_user(got_user: Any) -> User: | ||||||
"""Backward-compatibility for LoginHandler.get_user | ||||||
|
@@ -290,37 +323,6 @@ def get_handlers(self) -> list: | |||||
handlers.append((r"/logout", self.logout_handler_class)) | ||||||
return handlers | ||||||
|
||||||
def user_to_cookie(self, user: User) -> str: | ||||||
"""Serialize a user to a string for storage in a cookie | ||||||
|
||||||
If overriding in a subclass, make sure to define user_from_cookie as well. | ||||||
|
||||||
Default is just the user's username. | ||||||
""" | ||||||
# default: username is enough | ||||||
cookie = json.dumps( | ||||||
{ | ||||||
"username": user.username, | ||||||
"name": user.name, | ||||||
"display_name": user.display_name, | ||||||
"initials": user.initials, | ||||||
"color": user.color, | ||||||
} | ||||||
) | ||||||
return cookie | ||||||
|
||||||
def user_from_cookie(self, cookie_value: str) -> User | None: | ||||||
"""Inverse of user_to_cookie""" | ||||||
user = json.loads(cookie_value) | ||||||
return User( | ||||||
user["username"], | ||||||
user["name"], | ||||||
user["display_name"], | ||||||
user["initials"], | ||||||
None, | ||||||
user["color"], | ||||||
) | ||||||
|
||||||
def get_cookie_name(self, handler: JupyterHandler) -> str: | ||||||
"""Return the login cookie name | ||||||
|
||||||
|
@@ -347,7 +349,7 @@ def set_login_cookie(self, handler: JupyterHandler, user: User) -> None: | |||||
cookie_options.setdefault("secure", True) | ||||||
cookie_options.setdefault("path", handler.base_url) | ||||||
cookie_name = self.get_cookie_name(handler) | ||||||
handler.set_secure_cookie(cookie_name, self.user_to_cookie(user), **cookie_options) | ||||||
handler.set_secure_cookie(cookie_name, user.to_string(), **cookie_options) | ||||||
|
||||||
def _force_clear_cookie( | ||||||
self, handler: JupyterHandler, name: str, path: str = "/", domain: str | None = None | ||||||
|
@@ -404,7 +406,7 @@ def get_user_cookie(self, handler: JupyterHandler) -> User | None | Awaitable[Us | |||||
user_cookie = _user_cookie.decode() | ||||||
# TODO: try/catch in case of change in config? | ||||||
try: | ||||||
return self.user_from_cookie(user_cookie) | ||||||
return User.from_string(user_cookie) | ||||||
except Exception as e: | ||||||
# log bad cookie itself, only at debug-level | ||||||
self.log.debug(f"Error unpacking user from cookie: cookie={user_cookie}", exc_info=True) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not assume this is for cookie usage...