code: async def authenticate(url, username, password, *, insecure=False):
      
      url_versn = urljoin(url, "version/")
      url_authn = urljoin(url, "../../accounts/authenticate/")
  
      def check_response_is_okay(response):
          if response.status != HTTPStatus.OK:
              raise RemoteError(
                  "{0} -> {1.status} {1.reason}".format(
                      response.url_obj.human_repr(), response))
  
      connector = aiohttp.TCPConnector(verify_ssl=(not insecure))
      session = aiohttp.ClientSession(connector=connector)
      async with session:
          # Check that this server supports `authenticate-api`.
          async with session.get(url_versn) as response:
              check_response_is_okay(response)
              version_info = await response.json()
  
          if "authenticate-api" not in version_info["capabilities"]:
              raise LoginNotSupported(
                  "Server does not support automated client log-in. "
                  "Please obtain an API token via the MAAS UI.")
  
          # POST to the `authenticate` endpoint.
          data = {
              "username": username, "password": password,
              "consumer": "%s@%s" % (getuser(), gethostname())
          }
          async with session.post(url_authn, data=data) as response:
              check_response_is_okay(response)
              token_info = await response.json()
  
          return Credentials(
              token_info["consumer_key"],
              token_info["token_key"],
              token_info["token_secret"],
          )
docs: Obtain a new API key by logging into MAAS.
  
      :param url: URL for the MAAS API (i.e. ends with ``/api/x.y/``).
      :param insecure: If true, don't verify SSL/TLS certificates.
      :return: A `Credentials` instance.
  
      :raise RemoteError: An unexpected error from the remote system.
      :raise LoginNotSupported: Server does not support API client log-in.