docs: async def login(
          sess: aiohttp.ClientSession,
          registry_url: yarl.URL,
          credentials: dict,
          scope: str) -> dict:
      '''
      Authorize to the docker registry using the given credentials and token scope, and returns a set
      of required aiohttp.ClientSession.request() keyword arguments for further API requests.
  
      Some registry servers only rely on HTTP Basic Authentication without token-based access controls
      (usually via nginx proxy). We do support them also. :)
      '''
      if credentials.get('username') and credentials.get('password'):
          basic_auth = aiohttp.BasicAuth(
              credentials['username'], credentials['password'],
          )
      else:
          basic_auth = None
      realm = registry_url / 'token'  # fallback
      service = 'registry'            # fallback
      async with sess.get(registry_url / 'v2/', auth=basic_auth) as resp:
          ping_status = resp.status
          www_auth_header = resp.headers.get('WWW-Authenticate')
          if www_auth_header:
              match = re.search(r'realm="([^"]+)"', www_auth_header)
              if match:
                  realm = match.group(1)
              match = re.search(r'service="([^"]+)"', www_auth_header)
              if match:
                  service = match.group(1)
      if ping_status == 200:
          log.debug('docker-registry: {0} -> basic-auth', registry_url)
          return {'auth': basic_auth, 'headers': {}}
      elif ping_status == 404:
          raise RuntimeError(f'Unsupported docker registry: {registry_url}! '
                             '(API v2 not implemented)')
      elif ping_status == 401:
          params = {
              'scope': scope,
              'offline_token': 'true',
              'client_id': 'docker',
              'service': service,
          }
          async with sess.get(realm, params=params, auth=basic_auth) as resp:
              log.debug('docker-registry: {0} -> {1}', registry_url, realm)
              if resp.status == 200:
                  data = json.loads(await resp.read())
                  token = data.get('token', None)
                  return {'auth': None, 'headers': {
                      'Authorization': f'Bearer {token}'
                  }}
      raise RuntimeError('authentication for docker registry '
                         'f{registry_url} failed')