#!/usr/bin/env python3

import json
import os
import requests

host = os.environ.get('DS_HOST', 'localhost')
port = os.environ.get('DS_PORT', 8443)
ca_pem = os.environ.get('DS_CA_PEM', './ca-cert.pem')

def authenticate(username, password):
    response = requests.post(
        f'https://{host}:{port}/hdap/{username}?_action=authenticate',
        headers={ 'Content-Type': 'application/json' },
        json={ 'password': password },
        verify=ca_pem)
    return json.loads(response.text)['access_token']
