Skip to content

Commit b70af51

Browse files
auth reset command (#11)
* reset command * WIP * formatting and test fix * cleanup * remove debug code * update comments --------- Co-authored-by: Carl A. Adams <[email protected]>
1 parent 9e897a6 commit b70af51

File tree

7 files changed

+59
-5
lines changed

7 files changed

+59
-5
lines changed

src/planet_auth/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
TOKEN_FILE_PLAIN = "token.json"
2020
TOKEN_FILE_SOPS = "token.sops.json"
2121
USER_CONFIG_FILE = ".planet.json"
22+
PROFILE_DIR = ".planet"
2223
X_PLANET_APP = f"planet-auth-library-{importlib.metadata.version('planet-auth')}"
2324
X_PLANET_APP_HEADER = "X-Planet-App"

src/planet_auth/storage_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def obj_exists(self, key: ObjectStorageProvider_KeyType) -> bool:
6363
Check whether a given object exists in storage.
6464
"""
6565

66+
@abstractmethod
67+
def obj_rename(self, src: ObjectStorageProvider_KeyType, dst: ObjectStorageProvider_KeyType) -> None:
68+
"""
69+
Rename/Move an object from the source path to the destination path.
70+
"""
71+
6672
@staticmethod
6773
def _default_storage_provider():
6874
# Always create JIT to better handle cases where runtime changes to
@@ -175,6 +181,11 @@ def obj_exists(self, key: ObjectStorageProvider_KeyType) -> bool:
175181
obj_filepath = self._obj_filepath(key)
176182
return obj_filepath.exists()
177183

184+
def obj_rename(self, src: ObjectStorageProvider_KeyType, dst: ObjectStorageProvider_KeyType) -> None:
185+
src_filepath = self._obj_filepath(src)
186+
dst_filepath = self._obj_filepath(dst)
187+
src_filepath.rename(dst_filepath)
188+
178189

179190
class FileBackedJsonObjectException(AuthException):
180191
"""

src/planet_auth_utils/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
2121
"""
2222

23-
from .commands.cli.main import cmd_plauth_embedded, cmd_plauth_login
23+
from .commands.cli.main import (
24+
cmd_plauth_embedded,
25+
cmd_plauth_login,
26+
cmd_plauth_reset,
27+
cmd_plauth_version,
28+
)
2429
from .commands.cli.planet_legacy_auth_cmd import (
2530
cmd_pllegacy,
2631
cmd_pllegacy_login,
@@ -91,6 +96,8 @@
9196
__all__ = [
9297
"cmd_plauth_embedded",
9398
"cmd_plauth_login",
99+
"cmd_plauth_reset",
100+
"cmd_plauth_version",
94101
"cmd_jwt",
95102
"cmd_jwt_decode",
96103
"cmd_jwt_validate_oauth",

src/planet_auth_utils/commands/cli/main.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
import logging
1717
import importlib.metadata
1818
import sys
19+
import time
1920

20-
from planet_auth import Auth, AuthException, setStructuredLogging
21+
from planet_auth import Auth, AuthException, setStructuredLogging, ObjectStorageProvider, ObjectStorageProvider_KeyType
22+
from planet_auth.constants import USER_CONFIG_FILE
2123

2224
from planet_auth_utils.plauth_factory import PlanetAuthFactory
25+
from planet_auth_utils.profile import Profile
2326

2427
from .options import (
2528
opt_organization,
@@ -119,6 +122,29 @@ def _pkg_display_version(pkg_name):
119122
print(f"planet : {_pkg_display_version('planet')}")
120123

121124

125+
@cmd_plauth.command("reset")
126+
def cmd_plauth_reset():
127+
"""
128+
Reset saved auth state.
129+
130+
Old auth state is not deleted. It is moved aside and preserved.
131+
"""
132+
save_tag = time.strftime("%Y-%m-%d-%H%M%S")
133+
134+
# The CLI only supports the default storage provider right now.
135+
storage_provider = ObjectStorageProvider._default_storage_provider()
136+
137+
user_conf_objkey = ObjectStorageProvider_KeyType(USER_CONFIG_FILE)
138+
if storage_provider.obj_exists(user_conf_objkey):
139+
user_conf_objkey_offname = ObjectStorageProvider_KeyType(USER_CONFIG_FILE + f"-{save_tag}")
140+
storage_provider.obj_rename(user_conf_objkey, user_conf_objkey_offname)
141+
142+
profile_dir_objkey = ObjectStorageProvider_KeyType(Profile.profile_root())
143+
if storage_provider.obj_exists(profile_dir_objkey):
144+
profile_dir_objkey_offname = ObjectStorageProvider_KeyType(Profile.profile_root().name + f"-{save_tag}")
145+
storage_provider.obj_rename(profile_dir_objkey, profile_dir_objkey_offname)
146+
147+
122148
@cmd_plauth.command("login")
123149
@opt_open_browser
124150
@opt_show_qr_code

src/planet_auth_utils/commands/cli/options.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import click
1616
import pathlib
1717

18+
from planet_auth import constants
1819
from planet_auth_utils.constants import EnvironmentVariables
1920

2021

@@ -76,7 +77,7 @@ def opt_profile(function):
7677
type=str,
7778
envvar=EnvironmentVariables.AUTH_PROFILE,
7879
help="Select the client profile to use. User created profiles are "
79-
" defined by creating a subdirectory ~/.planet/. Additionally, a number of"
80+
f" defined by creating a subdirectory ~/{constants.PROFILE_DIR}/. Additionally, a number of"
8081
' built-in profiles are understood. See the "profile list" command'
8182
" for defined profiles. The auth profile controls how the software"
8283
" interacts with authentication services, as well as how it"

src/planet_auth_utils/profile.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from planet_auth.constants import (
2222
AUTH_CONFIG_FILE_SOPS,
2323
AUTH_CONFIG_FILE_PLAIN,
24+
PROFILE_DIR,
2425
)
2526
from planet_auth.storage_utils import ObjectStorageProvider
2627

@@ -43,13 +44,13 @@ def profile_root() -> pathlib.Path:
4344
"""
4445
Root storage directory used for profile data.
4546
"""
46-
# return pathlib.Path.home().joinpath(".planet")
47+
# return pathlib.Path.home().joinpath(PROFILE_DIR)
4748
# We used to assume file storage. We now support pluggable storage implementation.
4849
# We let the storage provider determine the real path (if there is one) and in this
4950
# Profile class we now (mostly) deal in the abstract idea of the path as an object
5051
# identifier that may or may not be file system based, depending on the storage
5152
# provider in use.
52-
return pathlib.Path(".planet")
53+
return pathlib.Path(PROFILE_DIR)
5354

5455
@staticmethod
5556
def get_profile_dir_path(profile: str) -> pathlib.Path:

tests/test_planet_auth/unit/auth/util.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,10 @@ def save_obj(self, key: ObjectStorageProvider_KeyType, data: dict) -> None:
454454

455455
def obj_exists(self, key: ObjectStorageProvider_KeyType) -> bool:
456456
return key in self._mock_storage
457+
458+
def obj_rename(self, src: ObjectStorageProvider_KeyType, dst: ObjectStorageProvider_KeyType) -> None:
459+
if self._mock_storage[src] is not None:
460+
self._mock_storage[dst] = self._mock_storage[src]
461+
del self._mock_storage[src]
462+
else:
463+
del self._mock_storage[dst]

0 commit comments

Comments
 (0)