-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract AuthTicketCookieHelper class
- Loading branch information
Showing
8 changed files
with
298 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,56 @@ | ||
from base64 import urlsafe_b64encode | ||
from functools import lru_cache | ||
from os import urandom | ||
|
||
from pyramid.request import Request | ||
from webob.cookies import SignedCookieProfile | ||
|
||
from h.security.identity import Identity | ||
from h.services.auth_ticket import AuthTicketService | ||
|
||
|
||
def is_api_request(request) -> bool: | ||
"""Return True if `request` is an API request.""" | ||
return bool(request.matched_route and request.matched_route.name.startswith("api.")) | ||
|
||
|
||
class AuthTicketCookieHelper: | ||
def identity( | ||
self, cookie: SignedCookieProfile, request: Request | ||
) -> Identity | None: | ||
userid, ticket_id = self.get_cookie_value(cookie) | ||
|
||
user = request.find_service(AuthTicketService).verify_ticket(userid, ticket_id) | ||
|
||
if (not user) or user.deleted: | ||
return None | ||
|
||
return Identity.from_models(user=user) | ||
|
||
def remember(self, cookie: SignedCookieProfile, request: Request, userid: str): | ||
ticket_id = urlsafe_b64encode(urandom(32)).rstrip(b"=").decode("ascii") | ||
request.find_service(AuthTicketService).add_ticket(userid, ticket_id) | ||
return cookie.get_headers([userid, ticket_id]) | ||
|
||
def forget(self, cookie: SignedCookieProfile, request: Request): | ||
request.session.invalidate() | ||
_, ticket_id = self.get_cookie_value(cookie) | ||
|
||
if ticket_id: | ||
request.find_service(AuthTicketService).remove_ticket(ticket_id) | ||
|
||
return cookie.get_headers(None, max_age=0) | ||
|
||
@staticmethod | ||
@lru_cache # Ensure we only add this once per request | ||
def add_vary_by_cookie(request: Request): | ||
def vary_add(request, response): # pylint:disable=unused-argument | ||
vary = set(response.vary if response.vary is not None else []) | ||
vary.add("Cookie") | ||
response.vary = list(vary) | ||
|
||
request.add_response_callback(vary_add) | ||
|
||
@staticmethod | ||
def get_cookie_value(cookie: SignedCookieProfile): | ||
return cookie.get_value() or (None, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.