pyalarmdotcom / alarmdotcom

Custom component to allow Home Assistant to interface with Alarm.com
MIT License
119 stars 36 forks source link

Getting an authentication error: "Unable to extract ajax key from Alarm.com" #207

Closed roooodcastro closed 1 year ago

roooodcastro commented 1 year ago

I've recently installed this integration, but I'm getting constant authentication errors from it. The integration is almost unusable to me, it keeps asking me to login again just minutes after I configured it.

There are some errors in my logs as it seems it can't read the API's responses (masked 2FA token for privacy):

# First error (formatted the headers with line breaks for ease of reading):

Unable to extract ajax key from Alarm.com. Response: <ClientResponse(https://www.alarm.com/login?m=no_session&ReturnUrl=/web/system/) [200 OK]> <CIMultiDictProxy(
  'Cache-Control': 'no-cache',
  'Pragma': 'no-cache',
  'Content-Type': 'text/html; charset=utf-8',
  'Content-Encoding': 'gzip',
  'Expires': '-1',
  'Vary': 'Accept-Encoding',
  'Set-Cookie': 'adc_e_alarm_locale=en-ie; domain=.alarm.com; expires=Sun, 28-Jan-2024 01:22:49 GMT; path=/; secure; SameSite=Strict',
  'Set-Cookie': 'adc_e_donottrack=true; domain=.alarm.com; expires=Sun, 28-Jan-2024 01:22:49 GMT; path=/; secure; SameSite=Strict',
  'Set-Cookie': 'adc_e_origin_locale=en-ie; domain=.alarm.com; expires=Sun, 28-Jan-2024 01:22:49 GMT; path=/; secure; SameSite=Strict',
  'Set-Cookie': 'adc_e_cookie_banner=false; domain=.alarm.com; expires=Sun, 28-Jan-2024 01:22:49 GMT; path=/; secure; SameSite=Strict',
  'Set-Cookie': 'adc_e_donottrack=true; domain=.alarm.com; expires=Sun, 28-Jan-2024 01:22:49 GMT; path=/; secure; SameSite=Strict',
  'Content-Security-Policy': "default-src 'self' *.google-analytics.com; font-src 'self' fonts.gstatic.com fonts.googleapis.com; media-src *; img-src * data:; style-src 'self' fonts.googleapis.com 'unsafe-inline' *.cloudfront.net; script-src 'self' *.google-analytics.com *.googletagmanager.com *.google.com *.gstatic.com *.greenhouse.io *.googleadservices.com *.hotjar.com *.facebook.net *.mathtag.com *.licdn.com *.tvsquared.com *.nextdoor.com 'unsafe-inline' 'unsafe-eval'; frame-ancestors 'self' *.alarm.com *.adt.com adt.com.es www.adt.cl www.adt.co.cr www.adt.co.uk www.adt.com.ar www.adt.com.br www.adt.com.mx www.adt.com.uy www.adt.my www.adtsecurity.com.au www.adtsecurity.co.nz www.secomsmart.com.sg www.sakralarm.se lightfootmechanical.com www.secomsmart.com.my smartsecurity.secom.plc.uk www.secom.co.th smartservices.adt.co.uk smartservices.adt.ie infinitysecurity.ca www.protek.com.py www.nos.pt www.chubbhomesecurity.com.au www.alert360.com www.securityinc.net i-wonder.co.jp iqconnect.qolsys.com; frame-src 'self' *.alarm.com *.youtube.com academy-alarm.com *.google.com *.greenhouse.io *.mathtag.com *.hotjar.com;",
  'X-Frame-Options': 'DENY',
  'Content-Security-Policy': "frame-ancestors 'none'",
  'X-Frame-Options': 'DENY',
  'Content-Security-Policy': "frame-ancestors 'none'",
  'Set-Cookie': 'twoFactorAuthenticationId={{MASKED}}; domain=.alarm.com; expires=Sun, 28-Jan-2024 01:22:49 GMT; path=/; secure; HttpOnly',
  'P3P': 'policyref="/w3c/p3p.xml",CP="OUR SAMa ADM UNI BUS ALL CUR DSP TAI COR IND STA"',
  'Access-Control-Allow-Origin': '*',
  'X-Content-Type-Options': 'nosniff',
  'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 'Date': 'Sat, 28 Jan 2023 01:22:48 GMT',
  'Content-Length': '34600'
)>

Error fetching data from Alarm.com.
Trying to refresh auth tokens by logging in again.

# Second error (account credentials are correct btw, the error is misleading):

Error fetching Smartzone:{{MY USERNAME}} data: Error communicating with api.
Authentication failed while fetching Smartzone:{{ MY USERNAME}} data: Invalid account credentials.

# Third error:

Unexpected error fetching Smartzone:{{MY USERNAME}} data:
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 225, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 181, in _async_update_data
    return await self.update_method()
  File "/config/custom_components/alarmdotcom/alarmhub.py", line 214, in async_update
    await self.system.async_update()
  File "/usr/local/lib/python3.10/site-packages/pyalarmdotcomajax/__init__.py", line 348, in async_update
    await self._async_get_and_build_devices(device_types)
  File "/usr/local/lib/python3.10/site-packages/pyalarmdotcomajax/__init__.py", line 616, in _async_get_and_build_devices
    ] = await self._async_get_items_and_subordinates(url=url)
  File "/usr/local/lib/python3.10/site-packages/pyalarmdotcomajax/__init__.py", line 977, in _async_get_items_and_subordinates
    return await self._async_get_items_and_subordinates(
  File "/usr/local/lib/python3.10/site-packages/pyalarmdotcomajax/__init__.py", line 900, in _async_get_items_and_subordinates
    raise ValueError
ValueError

I'm using a system user which has 2FA enabled, and when I configure the integration, I get a success message saying the authentication was successful (after asking for the 2FA code). I can also arm/disarm the alarm, and read the sensors, but everything seems to have a very high delay due to all these errors (arming takes a couple minutes to complete).

The HomeAssistant UI also emits a notification saying that the integration must be reconfigured, and duplicates the entry in the integrations page:

Screenshot_20230128_020203

I've already re-configured it as suggested multiple times, but it always just throws the same errors again and again after just a few minutes. How can I fix this?

dkedinger commented 1 year ago

I am having the same issue. No matter how often I authorize the connection I get the message to reconfigure it within a day.

elahd commented 1 year ago

Duplicate of #187. Let's move this to that thread.