Skip to content

DRAFT: jwt validator commands #7

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions src/planet_auth_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
opt_extra,
opt_human_readable,
opt_issuer,
opt_key,
opt_key_file,
opt_loglevel,
opt_long,
opt_open_browser,
Expand Down Expand Up @@ -133,6 +135,8 @@
"opt_extra",
"opt_human_readable",
"opt_issuer",
"opt_key",
"opt_key_file",
"opt_loglevel",
"opt_long",
"opt_open_browser",
Expand Down
40 changes: 38 additions & 2 deletions src/planet_auth_utils/commands/cli/jwt_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import click
import json
import jwt
import pathlib
import sys
import textwrap
Expand All @@ -30,6 +31,8 @@
from .options import (
opt_audience,
opt_issuer,
opt_key,
opt_key_file,
opt_token,
opt_token_file,
opt_human_readable,
Expand Down Expand Up @@ -230,12 +233,45 @@ def cmd_jwt_validate_oauth(ctx, token, token_file, audience, issuer, human_reada
@opt_human_readable
@opt_token
@opt_token_file
@opt_key
@opt_key_file
@recast_exceptions_to_click(AuthException, FileNotFoundError, NotImplementedError)
def cmd_jwt_validate_rs256(ctx, token, token_file, human_readable):
def cmd_jwt_validate_rs256(ctx, token, token_file, key, key_file, human_readable):
"""
Validate a JWT signed with a RS256 signature
"""
# token_to_validate = _get_token_or_fail(token_opt=token, token_file_opt=token_file)
# The TokenValidator is geared for OAuth2 JWTs.
# This helper is lower level, and doesn't make the same assumptions.
# TODO: it might be nice to still have this more adjacent to the TokenValidator
# to keep practices aligned.
# validator = TokenValidator()
token_to_validate = _get_token_or_fail(token_opt=token, token_file_opt=token_file)
signing_key: jwt.PyJWK = None
# required_claims = []
validated_complete = jwt.decode_complete( # Throws when invalid.
token_to_validate,
signing_key,
algorithms=["rs256", "RS256"],
# issuer=issuer,
# audience=audience,
options={
# "require": required_claims,
# "verify_aud": True,
"verify_exp": True,
# "verify_iss": True,
"verify_signature": True,
},
)
# XXX check - validation throws on error
click.echo("TOKEN OK")
print_jwt_parts(
raw=token_to_validate,
header=validated_complete["header"],
body=validated_complete["payload"],
signature=validated_complete["signature"],
human_readable=human_readable,
)

raise NotImplementedError("Command not implemented")


Expand Down
32 changes: 32 additions & 0 deletions src/planet_auth_utils/commands/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,38 @@ def opt_token_file(function):
return function


def opt_key(function):
"""
Click option for specifying a key literal.
"""
function = click.option(
"--key",
help="Key string.",
type=str,
# envvar=EnvironmentVariables.AUTH_KEY,
show_envvar=False,
show_default=False,
)(function)
return function


def opt_key_file(function):
"""
Click option for specifying a key file location for the
planet_auth package's click commands.
"""
function = click.option(
"--key-file",
type=click.Path(exists=True, file_okay=True, readable=True, path_type=pathlib.Path),
# envvar=EnvironmentVariables.AUTH_KEY_FILE,
help="File containing a key.",
default=None,
show_envvar=False,
show_default=True,
)(function)
return function


def opt_issuer(required=False):
def decorator(function):
"""
Expand Down
Loading