fix auth method

This commit is contained in:
Robert Kaussow 2018-06-09 23:54:08 +02:00
parent ddfa0a8a5a
commit 34211dec87
1 changed files with 33 additions and 7 deletions

View File

@ -8,6 +8,8 @@ import os
import json
import urlparse
import configparser
import sys
import logging
def api_auth(user, passwd, host):
@ -16,27 +18,51 @@ def api_auth(user, passwd, host):
data["password"] = passwd
json_data = json.dumps(data)
url = urlparse.urljoin(host, "auth", "token")
url = urlparse.urljoin(host, os.path.join("auth", "token"))
r = requests.post(url, data=json_data)
return r
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("~"), "config.ini")
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 configparser.Error:
return
except KeyError, e:
logger.error("Key %s not found in config" % (e))
sys.exit(0)
auth_token = api_auth(API_USER, PASSWORD, API_HOST)
print auth_token
auth = api_auth(API_USER, PASSWORD, API_HOST)
print auth["token"]
if __name__ == "__main__":