#!/usr/bin/python3
"""Install local PAM extensions or change the caller's public security-key credential."""
import json
import os
import sys
from local_auth import key_change, refresh
try:
    if os.geteuid() != 0: raise ValueError('Administrator authorization is required.')
    if sys.argv[1:] == ['refresh']:
        refresh()
        print(json.dumps(dict(ok=True)))
    elif len(sys.argv) == 2 and sys.argv[1] in ('add', 'remove'):
        print(json.dumps(key_change(sys.argv[1], sys.stdin)))
    else: raise ValueError('Choose a supported local authentication action.')
except (OSError, ValueError, KeyError) as error:
    print(json.dumps(dict(ok=False, errors=str(error))))
    sys.exit(1)
