from tendril.utils.config import ConfigOption
from tendril.utils import log
logger = log.get_logger(__name__, log.DEFAULT)

depends = ['tendril.config.core']


config_elements_auth = [
    ConfigOption(
        'AUTH_PROVIDER',
        "'auth0'",
        "AuthN Provider"
    ),
    ConfigOption(
        'AUTHZ_PROVIDER',
        "AUTH_PROVIDER",
        "AuthZ Provider"
    ),
    ConfigOption(
        "AUTH_AUDIENCE_BASE",
        "'https://tendril.link/domain'",
        "Base URL for Audiences"
    ),
    ConfigOption(
        "AUTH_ISSUER_BASE",
        "'https://tendril.link/auth'",
        "Base URL for Issuers"
    ),
    ConfigOption(
        "AUTH_COMPONENT",
        "'actual'",
        "Component for Audiences and Issuers"
    ),
    ConfigOption(
        "AUTH_AUDIENCE",
        "os.path.join(AUTH_AUDIENCE_BASE, AUTH_COMPONENT)",
        "Audience required by client JWTs to interact with this component"
    ),
    ConfigOption(
        "AUTH_ISSUER",
        "os.path.join(AUTH_ISSUER_BASE, AUTH_COMPONENT)",
        "Issuer specified in JWTs generated by this component"
    ),
    ConfigOption(
        'AUTH_PATCH_SCOPES_ON_STARTUP',
        'False',
        "Whether to patch scopes on the AuthZ provider at startup."
    ),
    ConfigOption(
        'AUTH_USERINFO_CACHING',
        "None",
        "Whether to cache userinfo acquired from the auth provider. "
        "Set to 'platform' for using platform-level caching, using redis. "
        "Other options not presently implemented."
    ),
]

config_elements_jwt = [
    ConfigOption(
        'AUTH_JWT_API_ENABLED',
        "True",
        "Whether to enable JWT generation API"
    ),
    ConfigOption(
        'AUTH_JWKS_PATH',
        'os.path.join(INSTANCE_ROOT, "jwt", "jwks.json")',
        "Path to the jwks.json file to serve."
    ),
    ConfigOption(
        'AUTH_JWT_PRIVATE_KEY',
        'os.path.join(INSTANCE_ROOT, "jwt", "privateKey.pem")',
        "Path to the private key to use to sign JWTs."
    ),
    ConfigOption(
        'AUTH_JWT_PUBLIC_KEY',
        'os.path.join(INSTANCE_ROOT, "jwt", "publicKey.pem")',
        "Path to the public key to use to verify JWTs."
    ),
    ConfigOption(
        'AUTH_JWT_ISSUER',
        'AUTH_ISSUER',
        "Issuer to specify in the issued JWT claims."
    ),
    ConfigOption(
        'AUTH_JWT_TTL',
        '0',
        "TTL in seconds before the issued JWT can be used."
    ),
    ConfigOption(
        'AUTH_JWT_VALIDITY',
        '3600',
        "Duration in seconds before the issued JWT is expired."
    ),
    ConfigOption(
        'AUTH_JWT_ALGORITHMS',
        '["RS256", "ES256", "HS256"]',
        "Algorithm to use to sign or verify JWTs. The first element used for encode."
    )
]

config_elements_mechanized = [
    ConfigOption(
        'AUTH_MECHANIZED_PROVIDER',
        'AUTH_PROVIDER',
        "Provider to use for mechanized users, if different."
    ),
    ConfigOption(
        'AUTH_MECHANIZED_USER_DOMAIN',
        '"tendril.link"',
        "Domain to use for mechanized user email addresses."
    ),
]

def load(manager):
    logger.debug("Loading {0}".format(__name__))
    manager.load_elements(config_elements_auth,
                          doc="Authentication Configuration")
    manager.load_elements(config_elements_jwt,
                          doc="JWT Infrastructure Configuration")
    manager.load_elements(config_elements_mechanized,
                          doc="Mechanized Users Configuration")
