1#!/usr/bin/env python3
2"""Configure Shale guest login using an existing provider registration.
3
4Register a GitHub OAuth App at https://github.com/settings/developers with homepage
5https://shale.paperclover.net and callback
6https://snowglobe.paperclover.net/auth/guest/callback/github.
7For Astheno Identity, register a confidential OpenID Connect client with callback
8https://snowglobe.paperclover.net/auth/guest/callback/astheno and openid/profile scopes.
9The same client signs in Snow Globe accounts linked to an Astheno username on the Users page.
10GitHub requests only read:user. Neither provider grants repository access.
11
12Run: python3 tools/guest-provider.py github --client-id CLIENT_ID
13The secret is read with a hidden prompt and passed over SSH stdin, never in argv.
14Disable: python3 tools/guest-provider.py github --disable
15"""
16import argparse
17import getpass
18import json
19import os
20import subprocess
21
22parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
23parser.add_argument('provider', choices=['github', 'astheno'])
24parser.add_argument('--client-id')
25parser.add_argument('--disable', action='store_true')
26parser.add_argument('--host', default=os.environ.get('SNOWGLOBE_DEPLOY_HOST', 'root@10.0.0.1'))
27parser.add_argument('--port', default=os.environ.get('SNOWGLOBE_DEPLOY_PORT', '22'))
28args = parser.parse_args()
29if not args.disable and not args.client_id:
30 parser.error('--client-id is required unless --disable is used')
31payload = {'provider': args.provider, 'enabled': not args.disable}
32if not args.disable:
33 payload.update(clientId=args.client_id, clientSecret=getpass.getpass('Client secret: '))
34result = subprocess.run(['ssh', '-p', args.port, args.host,
35 'podman exec -i snowglobe-dashboard /bin/home-dashboard --guest-provider'],
36 input=json.dumps(payload), text=True, capture_output=True)
37if result.returncode:
38 raise SystemExit('Provider configuration did not complete. Check the dashboard and SSH connection.')
39print(result.stdout.strip())