docstr: Performs end to end authentication flow.
  
  Returns
  ----------
  authorization header to use when authentication completes.
docstr_tok: ["Performs","end","to","end","authentication","flow",".","Returns","--","--","--","--","--","authorization","header","to","use","when","authentication","completes","."]
code: def authenticate(self) -> Optional[str]:
      """Performs end to end authentication flow.
  
      Returns
      ----------
      authorization header to use when authentication completes.
  
      """
      if not self.load():
          # First try to authenticate from env
          for key in Keys:
              setattr(self, key.suffix, os.environ.get(key.value, None))
  
          if self.user_id and self.api_key:
              self.save("", self.user_id, self.api_key, self.user_id)
              logger.info(
                  "Credentials loaded from environment variables"
              )
              return self.auth_header
          if self.api_key or self.user_id:
              raise ValueError(
                  "To use env vars for authentication both "
                  f"{Keys.USER_ID.value} and {Keys.API_KEY.value} should be set."
              )
  
          logger.debug(
              "failed to load credentials, opening browser to get new."
          )
          self._run_server()
          return self.auth_header
  
      if self.user_id and self.api_key:
          return self.auth_header
  
      raise ValueError(
          "We couldn't find any credentials linked to your account. "
          "Please try logging in using the CLI command `lightning login`"
      )
  
imports: import base64
  import json
  import os
  import pathlib
  from dataclasses import dataclass
  from enum import Enum
  from time import sleep
  from typing import Optional
  from urllib.parse import urlencode
  import click
  import requests
  import uvicorn
  from fastapi import FastAPI, Query, Request
  from starlette.background import BackgroundTask
  from starlette.responses import RedirectResponse
  from lightning.app.core.constants import LIGHTNING_CREDENTIAL_PATH, get_lightning_cloud_url
  from lightning.app.utilities.app_helpers import Logger
  from lightning.app.utilities.network import find_free_network_port
label_desc: Use image node_id 6 for calling the Auth obj's underlying member method code with example usage: obj.authenticate() and returns: self, self, self