Skip to content

Commit

Permalink
Merge pull request #679 from jjjake/config-troubleshoot
Browse files Browse the repository at this point in the history
Config troubleshoot
  • Loading branch information
jjjake authored Feb 12, 2025
2 parents 0ac174d + cf6c2a7 commit dc89c93
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Release History
---------------

5.3.0 (?)
+++++++++

5.2.1 (2025-02-12)
++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion internetarchive/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '5.2.1'
__version__ = '5.3.0.dev1'
48 changes: 48 additions & 0 deletions internetarchive/cli/ia_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from __future__ import annotations

import argparse
import json
import netrc
import sys

Expand All @@ -40,6 +41,8 @@ def setup(subparsers):
aliases=["co"],
help=("configure 'ia' with your "
"archive.org credentials"))
config_action_group = parser.add_mutually_exclusive_group()

parser.add_argument("--username", "-u",
help=("provide username as an option rather than "
"providing it interactively"))
Expand All @@ -49,6 +52,18 @@ def setup(subparsers):
parser.add_argument("--netrc", "-n",
action="store_true",
help="use netrc file for login")
config_action_group.add_argument("--show", "-s",
action="store_true",
help=("print the current configuration in JSON format, "
"redacting secrets and cookies"))
config_action_group.add_argument("--check", "-C",
action="store_true",
help="validate IA-S3 keys (exits 0 if valid, 1 otherwise)")
config_action_group.add_argument("--whoami", "-w",
action="store_true",
help=("uses your IA-S3 keys to retrieve account "
"information from archive.org "
"about the associated account"))
parser.add_argument("--print-cookies", "-c",
action="store_true",
help="print archive.org logged-in-* cookies")
Expand Down Expand Up @@ -76,6 +91,39 @@ def main(args: argparse.Namespace) -> None:
sys.exit(1)
print(f"logged-in-user={user}; logged-in-sig={sig}")
sys.exit()

if args.show:
config = args.session.config.copy()
# Redact S3 secret
if 's3' in config:
s3_config = config['s3'].copy()
if 'secret' in s3_config:
s3_config['secret'] = 'REDACTED'
config['s3'] = s3_config
# Redact logged-in-secret cookie
if 'cookies' in config:
cookies = config['cookies'].copy()
if 'logged-in-sig' in cookies:
cookies['logged-in-sig'] = 'REDACTED'
config['cookies'] = cookies
print(json.dumps(config))
sys.exit()

if args.whoami:
whoami_info = args.session.whoami()
print(json.dumps(whoami_info))
sys.exit()

if args.check:
whoami_info = args.session.whoami()
if whoami_info.get('success') is True:
user = whoami_info['value']['username']
print(f'The credentials for "{user}" are valid')
sys.exit(0)
else:
print('Your credentials are invalid, check your configuration and try again')
sys.exit(1)

try:
# Netrc
if args.netrc:
Expand Down
17 changes: 17 additions & 0 deletions internetarchive/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from typing import Iterable, Mapping, MutableMapping
from urllib.parse import unquote, urlparse

import requests
import requests.sessions
from requests import Response
from requests.adapters import HTTPAdapter
Expand Down Expand Up @@ -231,6 +232,22 @@ def set_file_logger(

_log.addHandler(fh)

def whoami(self) -> str:
"""Return the logged-in user email address.
:returns: The logged-in user email address.
"""
u = 'https://archive.org/services/user.php'
p = {'op': 'whoami'}

# Do not use self/Session.get() here,
# to make sure S3 keys are used for validation -- not cookies.
r = requests.get(u, params=p,
auth=auth.S3Auth(self.access_key, self.secret_key),
timeout=12)

return r.json()

def get_item(self,
identifier: str,
item_metadata: Mapping | None = None,
Expand Down

0 comments on commit dc89c93

Please sign in to comment.