def __init__()

in pathology/dicom_proxy/annotations_util.py [0:0]


  def __init__(self, dicom_web_base_url: dicom_url_util.DicomWebBaseURL):
    """Inits UserAuth credentials using the Proxy server service account.

    Args:
      dicom_web_base_url: Base URL of DICOM Annotations store for request.

    Raises:
      _UnableToAuthenticateUserError: If user cannot be authenticated.
    """
    studies_query = (
        f'{dicom_url_util._HEALTHCARE_API_URL}/{dicom_web_base_url}'
        '/studies?limit=1'
    )
    try:
      super().__init__(flask_util.get_headers())
      base_log = {
          proxy_const.LogKeywords.USER_EMAIL: self.email,
          proxy_const.LogKeywords.EMAIL_REGEX: _EMAIL_VALIDATION_REGEX,
          proxy_const.LogKeywords.AUTHORIZATION: self.authorization,
          proxy_const.LogKeywords.AUTHORITY: self.authority,
      }
      response = requests.get(
          studies_query,
          headers=self.add_to_header({_ACCEPT: _APPLICATION_DICOM_JSON}),
          stream=False,
      )
      try:
        response.raise_for_status()
        if self.email and re.fullmatch(
            _EMAIL_VALIDATION_REGEX,
            _normalize_email(self.email),
        ):
          self._user_email = self.email
          self._user_authorization = self.authorization
          self._user_authority = self.authority
          cloud_logging_client.info(
              'Authenticated user has read access to annotation DICOM store.',
              base_log,
          )
          self._init_to_service_account_credentials()
          return
        else:
          cloud_logging_client.error(
              'Could not authenticate user. User email empty or formattted'
              ' unexpectedly.',
              base_log,
          )
      except requests.exceptions.HTTPError as exp:
        cloud_logging_client.error(
            'User does not have access or cannot be authenticated.',
            base_log,
            exp,
        )
        raise _UnableToAuthenticateUserError(
            'User does not have access or cannot be authenticated.'
        ) from exp
    except user_auth_util.UserEmailRetrievalError as exp:
      cloud_logging_client.error(
          'User does not have access or cannot be authenticated.',
          {
              proxy_const.LogKeywords.USER_EMAIL: self.email,
              proxy_const.LogKeywords.EMAIL_REGEX: _EMAIL_VALIDATION_REGEX,
              proxy_const.LogKeywords.AUTHORIZATION: self.authorization,
              proxy_const.LogKeywords.AUTHORITY: self.authority,
          },
          exp,
      )
      raise _UnableToAuthenticateUserError(
          'User does not have access or cannot be authenticated.'
      ) from exp
    cloud_logging_client.error(
        'User does not have access or cannot be authenticated.'
    )
    raise _UnableToAuthenticateUserError(
        'User does not have access or cannot be authenticated.'
    )