Merge pull request #1217 from coreos-inc/v2pagination
Fix V2 catalog and tag pagination
This commit is contained in:
commit
81a36ee3b8
7 changed files with 105 additions and 38 deletions
26
util/pagination.py
Normal file
26
util/pagination.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
import datetime
|
||||
import json
|
||||
|
||||
from app import app
|
||||
from util.security.crypto import encrypt_string, decrypt_string
|
||||
|
||||
# TTL (in seconds) for page tokens.
|
||||
_PAGE_TOKEN_TTL = datetime.timedelta(days=2).total_seconds()
|
||||
|
||||
def decrypt_page_token(token_string):
|
||||
if token_string is None:
|
||||
return None
|
||||
|
||||
unencrypted = decrypt_string(token_string, app.config['PAGE_TOKEN_KEY'], ttl=_PAGE_TOKEN_TTL)
|
||||
if unencrypted is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
return json.loads(unencrypted)
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def encrypt_page_token(page_token):
|
||||
return encrypt_string(json.dumps(page_token), app.config['PAGE_TOKEN_KEY'])
|
||||
|
|
@ -11,7 +11,7 @@ def decrypt_string(string, key, ttl=None):
|
|||
""" Decrypts an encrypted string with the specified key. The key must be 32 raw bytes. """
|
||||
f = Fernet(base64.urlsafe_b64encode(key))
|
||||
try:
|
||||
return f.decrypt(string, ttl=ttl)
|
||||
return f.decrypt(str(string), ttl=ttl)
|
||||
except InvalidToken:
|
||||
return None
|
||||
except TypeError:
|
||||
|
|
Reference in a new issue