# Path: lib/credentials_adapters.py
# class St2UserCredentials(AbstractCredentials):
#     def __init__(self, username=None, password=None):
#         self.username = username
#         self.password = password
# 
#     def __repr__(self):
#         return "".join([self.username, " : ", "".join(["*" for c in self.password])])
# 
#     def requests(self, st2_x_auth=False):
#         # TODO: FIX: Find a cleaner way for requests to produce the header.
#         headers = (
#             HTTPBasicAuth(self.username, self.password)
#             .__call__(SimpleNamespace(**{"headers": {}}))
#             .headers
#         )
#         if st2_x_auth:
#             headers["X-Authenticate"] = headers["Authorization"]
#             del headers["Authorization"]
#         return headers
# 
#     def st2client(self):
#         raise NotImplementedError
# 
# class St2UserToken(AbstractCredentials):
#     def __init__(self, token=None):
#         self.token = None
#         if token:
#             self.token = token
# 
#     def __repr__(self):
#         return self.token
# 
#     def requests(self):
#         return {"X-Auth-Token": self.token}
# 
#     def st2client(self):
#         return {"token": self.token}
# 
# class St2ApiKey(AbstractCredentials):
#     def __init__(self, apikey=None):
#         self.apikey = None
#         if apikey:
#             self.apikey = apikey
# 
#     def __repr__(self):
#         return self.apikey
# 
#     def requests(self):
#         return {"St2-Api-Key": self.apikey}
# 
#     def st2client(self):
#         return {"api_key": self.apikey}
# 
# Path: lib/authentication_controller.py
# class BotPluginIdentity(object):
#     """
#     For internal use only by err-stackstorm.  The object is used by methods that will create a
#     session and authenticate err-stackstorm credentials with StackStorm.
#     """
# 
#     def __init__(self, name="errbot%service", secret=generate_password(16)):
#         self.name = name
#         self.secret = secret
# 
#     def __repr__(self):
#         return str(self.secret)
# 
# Path: lib/errors.py
# class SessionInvalidError(Error):
#     def __init__(self, message="Session is invalid."):
#         super(SessionInvalidError, self).__init__()
#         self.message = message


import abc
import requests
import logging
from urllib.parse import urlparse, urljoin
from lib.credentials_adapters import St2UserCredentials, St2UserToken, St2ApiKey
from lib.authentication_controller import BotPluginIdentity
from lib.errors import SessionInvalidError
        token = False
        response = self._http_request(
            "GET",
            self.cfg.auth_url,
            path="/tokens/validate",
            headers=bot_creds.requests(),
            payload=creds.st2client(),
        )
        if response == requests.codes.ok:
            token = response.json().get("token", False)
            if token is not False:
                token = St2UserToken(token)
        return token

    def authenticate_key(self, creds, bot_creds):
        """
        Use StackStorm API to validate the API key is valid.
        """
        api_key = False
        response = self._http_request("GET", self.cfg.api_url, path="/", header=creds.requests())
        if response.status_code in [requests.codes.ok]:
            # Since the API Key is valid, return it to be used for requests.
            api_key = creds

        return api_key

    def authenticate(self, chat_user=None, st2_creds=None, bot_creds=None):
        token = None
        if isinstance(st2_creds, St2UserCredentials):
            token = self.authenticate_user(st2_creds, bot_creds)

# Path: lib/credentials_adapters.py
# class St2UserCredentials(AbstractCredentials):
#     def __init__(self, username=None, password=None):
#         self.username = username
#         self.password = password
# 
#     def __repr__(self):
#         return "".join([self.username, " : ", "".join(["*" for c in self.password])])
# 
#     def requests(self, st2_x_auth=False):
#         # TODO: FIX: Find a cleaner way for requests to produce the header.
#         headers = (
#             HTTPBasicAuth(self.username, self.password)
#             .__call__(SimpleNamespace(**{"headers": {}}))
#             .headers
#         )
#         if st2_x_auth:
#             headers["X-Authenticate"] = headers["Authorization"]
#             del headers["Authorization"]
#         return headers
# 
#     def st2client(self):
#         raise NotImplementedError
# 
# class St2UserToken(AbstractCredentials):
#     def __init__(self, token=None):
#         self.token = None
#         if token:
#             self.token = token
# 
#     def __repr__(self):
#         return self.token
# 
#     def requests(self):
#         return {"X-Auth-Token": self.token}
# 
#     def st2client(self):
#         return {"token": self.token}
# 
# class St2ApiKey(AbstractCredentials):
#     def __init__(self, apikey=None):
#         self.apikey = None
#         if apikey:
#             self.apikey = apikey
# 
#     def __repr__(self):
#         return self.apikey
# 
#     def requests(self):
#         return {"St2-Api-Key": self.apikey}
# 
#     def st2client(self):
#         return {"api_key": self.apikey}
# 
# Path: lib/authentication_controller.py
# class BotPluginIdentity(object):
#     """
#     For internal use only by err-stackstorm.  The object is used by methods that will create a
#     session and authenticate err-stackstorm credentials with StackStorm.
#     """
# 
#     def __init__(self, name="errbot%service", secret=generate_password(16)):
#         self.name = name
#         self.secret = secret
# 
#     def __repr__(self):
#         return str(self.secret)
# 
# Path: lib/errors.py
# class SessionInvalidError(Error):
#     def __init__(self, message="Session is invalid."):
#         super(SessionInvalidError, self).__init__()
#         self.message = message


# Path: lib/authentication_handler.py
import abc
import requests
import logging
from urllib.parse import urlparse, urljoin
from lib.credentials_adapters import St2UserCredentials, St2UserToken, St2ApiKey
from lib.authentication_controller import BotPluginIdentity
from lib.errors import SessionInvalidError

        token = False
        response = self._http_request(
            "GET",
            self.cfg.auth_url,
            path="/tokens/validate",
            headers=bot_creds.requests(),
            payload=creds.st2client(),
        )
        if response == requests.codes.ok:
            token = response.json().get("token", False)
            if token is not False:
                token = St2UserToken(token)
        return token

    def authenticate_key(self, creds, bot_creds):
        """
        Use StackStorm API to validate the API key is valid.
        """
        api_key = False
        response = self._http_request("GET", self.cfg.api_url, path="/", header=creds.requests())
        if response.status_code in [requests.codes.ok]:
            # Since the API Key is valid, return it to be used for requests.
            api_key = creds

        return api_key

    def authenticate(self, chat_user=None, st2_creds=None, bot_creds=None):
        token = None
        if isinstance(st2_creds, St2UserCredentials):
            token = self.authenticate_user(st2_creds, bot_creds)

        if isinstance(st2_creds, St2UserToken):