Skip to content

Commit 84c498f

Browse files
minor tweaks (#46)
* cherry-picking some small incidental changes from a larger feature branch to front-run the main MR. * more details test assertions for token validator * flake fixes * test util improvements * minor code de-dupe in utils * fix typo
1 parent 988d269 commit 84c498f

File tree

8 files changed

+163
-52
lines changed

8 files changed

+163
-52
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ site
1616
*.venv
1717
venv*
1818
version-with-buildnum.txt
19+
.vscode
1920
__pycache__

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ build-backend = "setuptools.build_meta"
106106
source = [
107107
"planet_auth",
108108
"planet_auth_utils",
109+
"planet_auth_config_injection",
109110
# "tests",
110111
]
111112
branch = true

src/planet_auth/oidc/multi_validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def validate_access_token(
286286
"""
287287

288288
if not token:
289-
raise InvalidArgumentException(message="Cannot validate empty string as a token")
289+
raise InvalidArgumentException(message="Cannot decode empty string as a token")
290290

291291
validator = self._select_validator(token)
292292
local_validation, remote_validation = self._check_access_token(

src/planet_auth/oidc/token_validator.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import jwt
1616
import time
17-
from typing import Dict, List
17+
from typing import Any, Dict, List, Tuple
1818

1919
import planet_auth.logging.auth_logger
2020
from planet_auth.auth_exception import AuthException, InvalidTokenException
@@ -181,7 +181,7 @@ def validate_token(
181181
"""
182182
# PyJWT should enforce this, but we have unit tests in case...
183183
if not token_str:
184-
raise InvalidArgumentException(message="Cannot validate empty string as a token")
184+
raise InvalidArgumentException(message="Cannot decode empty string as a token")
185185
if not issuer:
186186
# PyJWT does not seem to raise if the issuer is explicitly None, even when
187187
# verify_iss was selected.
@@ -257,9 +257,18 @@ def validate_token(
257257
return validated_claims
258258

259259
@staticmethod
260-
def hazmat_unverified_decode(token_str):
261-
# WARNING: Treat unverified token claims like toxic waste.
262-
# Nothing can be trusted until the token is verified.
260+
@InvalidArgumentException.recast(jwt.exceptions.DecodeError)
261+
def hazmat_unverified_decode(token_str) -> Tuple[Dict, Dict, Any]:
262+
"""
263+
Decode a JWT without verifying the signature or any claims.
264+
265+
!!! Warning
266+
Treat unverified token claims with extreme caution.
267+
Nothing can be trusted until the token is verified.
268+
269+
Returns:
270+
Returns the decoded JWT header, payload, and signature
271+
"""
263272
unverified_complete = jwt.decode_complete(token_str, options={"verify_signature": False}) # nosemgrep
264273
return unverified_complete["header"], unverified_complete["payload"], unverified_complete["signature"]
265274

src/planet_auth/request_authenticator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def update_credential_data(self, new_credential_data: Dict) -> None:
126126
self._credential.set_data(new_credential_data)
127127
self._credential.save() # Clobber old data that may be saved to disk.
128128
# Clear-out auth material when a new credential is set.
129-
# child classes are expected to populate it JIT for auth
129+
# Child classes are expected to populate it JIT for auth
130130
# requests.
131131
self._token_body = None
132132

@@ -135,7 +135,7 @@ def credential(self, refresh_if_needed: bool = False) -> Optional[Credential]:
135135
Return the current credential.
136136
137137
This may not be the credential the authenticator was constructed with.
138-
Request Authenticators are free to refresh credentials depending in the
138+
Request Authenticators are free to refresh credentials depending on the
139139
needs of the implementation. This may happen upon this request,
140140
or may happen as a side effect of RequestAuthenticator operations.
141141
"""

src/planet_auth/storage_utils.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,17 @@ def update_data(self, sparse_update_data):
364364
new_data = sparse_update_data
365365
self.set_data(new_data)
366366

367-
def set_data(self, data):
367+
def set_data(self, data, copy_data: bool = True):
368368
"""
369369
Set the current in memory data. The data will be checked for validity
370370
before in memory values are set. Invalid data will result in an exception
371371
being thrown and no change being made to the in memory object.
372372
"""
373373
self.check_data(data)
374-
self._data = data.copy()
374+
if copy_data:
375+
self._data = data.copy()
376+
else:
377+
self._data = data
375378
self._load_time = int(time.time())
376379

377380
def check_data(self, data):
@@ -459,9 +462,7 @@ def load(self):
459462
return # we now allow in memory operation. Should we raise an error if the current data is invalid?
460463

461464
new_data = self._object_storage_provider.load_obj(self._file_path)
462-
self.check_data(new_data)
463-
self._data = new_data
464-
self._load_time = int(time.time())
465+
self.set_data(new_data, copy_data=False)
465466

466467
def lazy_load(self):
467468
"""

0 commit comments

Comments
 (0)