Skip to content

move auth init to happen JIT #1164

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

Draft
wants to merge 30 commits into
base: main-3.0-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ada46d8
updates from beta feedback for improved login CLI experience.
carl-adams-planet May 23, 2025
a9f5731
plumb profile copy
carl-adams-planet May 23, 2025
7f759fe
WIP
carl-adams-planet May 27, 2025
3c12a58
update planet-auth per feedback.
carl-adams-planet May 28, 2025
b284e59
formatting for the linter
carl-adams-planet May 28, 2025
a39371d
proofreading.
carl-adams-planet May 29, 2025
60217c7
simplify examples
carl-adams-planet Jun 2, 2025
1424e0a
WIP
carl-adams-planet Jun 2, 2025
e88d495
WIP
carl-adams-planet Jun 3, 2025
85ae624
proofreading edits
carl-adams-planet Jun 3, 2025
4d839de
update links
carl-adams-planet Jun 3, 2025
d8b9c91
minor edits to example code
carl-adams-planet Jun 5, 2025
c1fb12a
reorder sections
carl-adams-planet Jun 5, 2025
f077dd3
more breadcrumbs in the 'python' housed pointer page for auth docs.
carl-adams-planet Jun 5, 2025
1ad4dee
update the auth section in non-auth sub-trees of the doc site
carl-adams-planet Jun 5, 2025
66846df
More doc edits for clarification. Adding links to the list of protoc…
carl-adams-planet Jun 5, 2025
fe919cc
accepting edit suggestion.
carl-adams-planet Jun 5, 2025
8441709
adding suggested link
carl-adams-planet Jun 5, 2025
577af9d
clarifying links page
carl-adams-planet Jun 5, 2025
c0b3e12
unhide M2M options for now
carl-adams-planet Jun 5, 2025
ddc441c
add option to control saving to storage to constructor. Doc updates
carl-adams-planet Jun 5, 2025
2aad959
formatting fixes
carl-adams-planet Jun 5, 2025
d40b8b9
reformatting examples
carl-adams-planet Jun 6, 2025
79f570b
Merge branch 'main-3.0-dev' into carl/torbin-feedback
carl-adams-planet Jun 6, 2025
55c6900
proofreading
carl-adams-planet Jun 6, 2025
9a5ed1f
small change to force a github event
carl-adams-planet Jun 6, 2025
4c2ee7e
small change to force a github event
carl-adams-planet Jun 6, 2025
2f5d8e1
fix links
carl-adams-planet Jun 6, 2025
b2e5fcd
Merge branch 'main-3.0-dev' into carl/torbin-feedback
carl-adams-planet Jul 9, 2025
b85c588
Merge branch 'carl/torbin-feedback' into carl/torbin-feedback--2--mov…
carl-adams-planet Jul 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions planet/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,56 @@
import click
import planet_auth_utils

import planet

LOGGER = logging.getLogger(__name__)


def prime_cli_auth_ctx(ctx,
auth_profile,
auth_client_id,
auth_client_secret,
auth_api_key):
# Delay creating the auth context until we need it.
# See below.
ctx.obj['PLAUTH_FACTORY_INIT_ARGS'] = {
'auth_profile_opt': auth_profile,
'auth_client_id_opt': auth_client_id,
'auth_client_secret_opt': auth_client_secret,
'auth_api_key_opt': auth_api_key,
'use_env': True,
'use_configfile': True,
}
return


def init_cli_auth_ctx_jit(ctx):
"""
init the planet-auth state stored in the click CLI JIT.
Setting this up and pulling the trigger are separated to offer a more ergonomic
experience for warnings, suppressing them if they would be irrelevant.
"""
# planet-auth library Auth context type
# Embedded click commands imported from planet_auth_utils expect
# this in the 'AUTH' context field.
if ctx.obj['PLAUTH_FACTORY_INIT_ARGS']:
ctx.obj['AUTH'] = (
planet_auth_utils.PlanetAuthFactory.initialize_auth_client_context(
**ctx.obj['PLAUTH_FACTORY_INIT_ARGS'])
)

# planet SDK Auth context type
ctx.obj['PLSDK_AUTH'] = planet.Auth._from_plauth(
pl_authlib_context=ctx.obj['AUTH'])


@click.group("auth") # type: ignore
@click.pass_context
def cmd_auth(ctx):
"""
Commands for working with Planet authentication.
"""
init_cli_auth_ctx_jit(ctx)


cmd_auth.add_command(name="login", cmd=planet_auth_utils.cmd_plauth_login)
Expand Down
32 changes: 5 additions & 27 deletions planet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,11 @@ def main(ctx,
ctx.ensure_object(dict)
ctx.obj['QUIET'] = quiet

_configure_cli_auth_ctx(ctx,
auth_profile,
auth_client_id,
auth_client_secret,
auth_api_key)


def _configure_cli_auth_ctx(ctx,
auth_profile,
auth_client_id,
auth_client_secret,
auth_api_key):
# planet-auth library Auth context type
# Embedded click commands imported from planet_auth_utils expect
# this in the 'AUTH' context field.
ctx.obj[
'AUTH'] = planet_auth_utils.PlanetAuthFactory.initialize_auth_client_context(
auth_profile_opt=auth_profile,
auth_client_id_opt=auth_client_id,
auth_client_secret_opt=auth_client_secret,
auth_api_key_opt=auth_api_key,
use_env=True,
use_configfile=True)

# planet SDK Auth context type
ctx.obj['PLSDK_AUTH'] = planet.Auth._from_plauth(
pl_authlib_context=ctx.obj['AUTH'])
auth.prime_cli_auth_ctx(ctx,
auth_profile,
auth_client_id,
auth_client_secret,
auth_api_key)


def _configure_logging(verbosity):
Expand Down
13 changes: 7 additions & 6 deletions planet/cli/session.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
"""CLI HTTP/auth sessions."""

from planet.http import Session

from planet.cli.auth import init_cli_auth_ctx_jit

class CliSession(Session):
"""Session with CLI-specific auth and identifying header"""

def __init__(self, click_ctx=None, plsdk_auth=None):
if click_ctx:
_plsdk_auth = click_ctx.obj['PLSDK_AUTH']
else:
_plsdk_auth = None

if plsdk_auth:
_plsdk_auth = plsdk_auth
else:
if click_ctx:
init_cli_auth_ctx_jit(click_ctx)
_pksdk_auth = click_ctx.obj['PLSDK_AUTH']
else:
_plsdk_auth = None

super().__init__(_plsdk_auth)
self._client.headers.update({'X-Planet-App': 'python-cli'})
Loading