# Path: alertaclient/auth/github.py
# def login(client, github_url, client_id):
# 
# Path: alertaclient/auth/utils.py
# def save_token(endpoint, username, token):
#     with open(NETRC_FILE, 'a'):  # touch file
#         pass
#     try:
#         info = netrc(NETRC_FILE)
#     except Exception as e:
#         raise ConfigurationError(f'{e}')
#     info.hosts[machine(endpoint)] = (username, None, token)
#     with open(NETRC_FILE, 'w') as f:
#         f.write(dump_netrc(info))


import sys
import click
from alertaclient.auth import azure, github, gitlab, google, oidc
from alertaclient.auth.token import Jwt
from alertaclient.auth.utils import save_token
from alertaclient.exceptions import AuthError
    Basic Auth username/password instead of using an API key."""
    client = obj['client']
    provider = obj['provider']
    client_id = obj['client_id']

    try:
        if provider == 'azure':
            token = azure.login(client, obj['azure_tenant'], client_id)['token']
        elif provider == 'github':
            token = github.login(client, obj['github_url'], client_id)['token']
        elif provider == 'gitlab':
            token = gitlab.login(client, obj['gitlab_url'], client_id)['token']
        elif provider == 'google':
            if not username:
                username = click.prompt('Email')
            token = google.login(client, username, client_id)['token']
        elif provider == 'openid':
            token = oidc.login(client, obj['oidc_auth_url'], client_id)['token']
        elif provider == 'basic' or provider == 'ldap':
            if not username:
                username = click.prompt('Email')
            password = click.prompt('Password', hide_input=True)
            token = client.login(username, password)['token']
        else:
            click.echo(f'ERROR: unknown provider {provider}', err=True)
            sys.exit(1)
    except Exception as e:
        raise AuthError(e)

    jwt = Jwt()

# Path: alertaclient/auth/github.py
# def login(client, github_url, client_id):
# 
# Path: alertaclient/auth/utils.py
# def save_token(endpoint, username, token):
#     with open(NETRC_FILE, 'a'):  # touch file
#         pass
#     try:
#         info = netrc(NETRC_FILE)
#     except Exception as e:
#         raise ConfigurationError(f'{e}')
#     info.hosts[machine(endpoint)] = (username, None, token)
#     with open(NETRC_FILE, 'w') as f:
#         f.write(dump_netrc(info))


# Path: alertaclient/commands/cmd_login.py
import sys
import click
from alertaclient.auth import azure, github, gitlab, google, oidc
from alertaclient.auth.token import Jwt
from alertaclient.auth.utils import save_token
from alertaclient.exceptions import AuthError

    Basic Auth username/password instead of using an API key."""
    client = obj['client']
    provider = obj['provider']
    client_id = obj['client_id']

    try:
        if provider == 'azure':
            token = azure.login(client, obj['azure_tenant'], client_id)['token']
        elif provider == 'github':
            token = github.login(client, obj['github_url'], client_id)['token']
        elif provider == 'gitlab':
            token = gitlab.login(client, obj['gitlab_url'], client_id)['token']
        elif provider == 'google':
            if not username:
                username = click.prompt('Email')
            token = google.login(client, username, client_id)['token']
        elif provider == 'openid':
            token = oidc.login(client, obj['oidc_auth_url'], client_id)['token']
        elif provider == 'basic' or provider == 'ldap':
            if not username:
                username = click.prompt('Email')
            password = click.prompt('Password', hide_input=True)
            token = client.login(username, password)['token']
        else:
            click.echo(f'ERROR: unknown provider {provider}', err=True)
            sys.exit(1)
    except Exception as e:
        raise AuthError(e)

    jwt = Jwt()

    preferred_username = jwt.parse(token)['preferred_username']