This repository has been archived on 2021-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
certbot_dns_corenetworks/authenticator.py
2018-06-09 23:54:08 +02:00

70 lines
1.5 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""API client to core-networks"""
import requests
import os
import json
import urlparse
import configparser
import sys
import logging
def api_auth(user, passwd, host):
data = {}
data["login"] = user
data["password"] = passwd
json_data = json.dumps(data)
url = urlparse.urljoin(host, os.path.join("auth", "token"))
r = requests.post(url, data=json_data)
return r.json()
def setup_logger():
# seup logging
logger = logging.getLogger("certbot_dns_corenetworks")
logging.basicConfig(level=logging.INFO)
# create console handler
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s- %(levelname)s - %(message)s')
ch.setFormatter(formatter)
# add handler to logger
logger.addHandler(ch)
logger.propagate = False
return logger
def main():
"""Main logic entrypoint"""
logger = setup_logger()
try:
config_path = os.path.join(
os.path.expanduser("~"),
".certbot_dns_corenetworks",
"config.ini"
)
config = configparser.ConfigParser()
config.read(config_path)
API_HOST = config['API']['HOST']
API_USER = config['API']['USER']
PASSWORD = config['API']['PASSWORD']
except KeyError, e:
logger.error("Key %s not found in config" % (e))
sys.exit(0)
auth = api_auth(API_USER, PASSWORD, API_HOST)
print auth["token"]
if __name__ == "__main__":
main()