auth: Fix has_luci_context_local_auth.

Checking for LUCI_CONTEXT is not enough. To use local auth, the
stored config must be valid and have a default account id.

Bug: 1018069
Change-Id: I5bbe880cc217a06ae0e1ae8d7e00ef09502af6a9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/1888211
Reviewed-by: Vadim Shtayura <vadimsh@chromium.org>
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
diff --git a/auth.py b/auth.py
index 1123cf7..204cfb7 100644
--- a/auth.py
+++ b/auth.py
@@ -57,7 +57,15 @@
 
 def has_luci_context_local_auth():
   """Returns whether LUCI_CONTEXT should be used for ambient authentication."""
-  return bool(os.environ.get('LUCI_CONTEXT'))
+  ctx_path = os.environ.get('LUCI_CONTEXT')
+  if not ctx_path:
+    return False
+  try:
+    with open(ctx_path) as f:
+      loaded = json.load(f)
+  except (OSError, IOError, ValueError):
+    return False
+  return loaded.get('local_auth', {}).get('default_account_id') is not None
 
 
 class Authenticator(object):