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

70 lines
1.5 KiB
Python
Raw Normal View History

2018-06-04 22:32:16 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2018-06-06 19:38:20 +00:00
"""API client to core-networks"""
import requests
import os
import json
import urlparse
import configparser
2018-06-09 21:54:08 +00:00
import sys
import logging
2018-06-06 19:38:20 +00:00
def api_auth(user, passwd, host):
data = {}
data["login"] = user
data["password"] = passwd
json_data = json.dumps(data)
2018-06-09 21:54:08 +00:00
url = urlparse.urljoin(host, os.path.join("auth", "token"))
2018-06-06 19:38:20 +00:00
r = requests.post(url, data=json_data)
2018-06-09 21:54:08 +00:00
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
2018-06-06 19:38:20 +00:00
def main():
"""Main logic entrypoint"""
2018-06-09 21:54:08 +00:00
logger = setup_logger()
2018-06-09 20:46:31 +00:00
try:
2018-06-09 21:54:08 +00:00
config_path = os.path.join(
os.path.expanduser("~"),
".certbot_dns_corenetworks",
"config.ini"
)
2018-06-09 20:46:31 +00:00
config = configparser.ConfigParser()
config.read(config_path)
API_HOST = config['API']['HOST']
API_USER = config['API']['USER']
PASSWORD = config['API']['PASSWORD']
2018-06-09 21:54:08 +00:00
except KeyError, e:
logger.error("Key %s not found in config" % (e))
sys.exit(0)
2018-06-09 21:54:08 +00:00
auth = api_auth(API_USER, PASSWORD, API_HOST)
print auth["token"]
2018-06-06 19:38:20 +00:00
if __name__ == "__main__":
main()