@@ -236,10 +236,25 @@ fromFile l m = do
236 x <- cloudSDKConfigPath
236 x <- cloudSDKConfigPath
237 fromFilePath x l m
237 fromFilePath x l m
239+-- | An internal type inhabited by the supported JSON formats
240+-- for Application Default Credentials.
242+ = ParsedService !ServiceAccount
239−data Parse = SA !ServiceAccount | AU !AuthorisedUser
243+ | ParsedUser !AuthorisedUser
245+-- The following instance accumulates all error messages if all parsers fail.
246+-- If there is a success, the entire parse succeeds with the single
241−instance FromJSON Parse where
248+instance FromJSON AccountJSON where
242− parseJSON o = SA <$> parseJSON o <|> AU <$> parseJSON o
249+ parseJSON o =
250+ let x = ParsedService <$> parseEither parseJSON o
251+ y = ParsedUser <$> parseEither parseJSON o
252+ in either fail pure $
254+ (Left e, Left e') -> Left $
255+ "Failed parsing service_account: " ++ e ++
256+ ", Failed parsing authorized_user: " ++ e'
244-- | Attempt to load either a @service_account@ or @authorized_user@ formatted
259-- | Attempt to load either a @service_account@ or @authorized_user@ formatted
245-- file to obtain the credentials neccessary to perform a token refresh from
260-- file to obtain the credentials neccessary to perform a token refresh from
@@ -250,14 +265,15 @@ fromFilePath :: (MonadIO m, MonadCatch m)
250 -> Manager
265 -> Manager
251 -> m Auth
266 -> m Auth
252fromFilePath f l m = do
267fromFilePath f l m = do
268+ logDebug l $ "Trying to read credentials from " <> build f
253 p <- liftIO (doesFileExist f)
269 p <- liftIO (doesFileExist f)
254 unless p $
270 unless p $
255 throwM (MissingFileError f)
271 throwM (MissingFileError f)
256 e <- liftIO (LBS.readFile f) >>=
272 e <- liftIO (LBS.readFile f) >>=
257 either (throwM . InvalidFileError f) pure . parseLBS
273 either (throwM . InvalidFileError f) pure . parseLBS
258 case e of
274 case e of
259− SA a -> refresh a l m >>= fmap AuthSign . liftIO . newMVar
275+ ParsedService a -> refresh a l m >>= fmap AuthSign . liftIO . newMVar
260− AU u -> refresh u l m >>= fmap AuthUser . liftIO . newMVar
276+ ParsedUser u -> refresh u l m >>= fmap AuthUser . liftIO . newMVar
262-- | Lookup the @GOOGLE_APPLICATION_CREDENTIALS@ environment variable for the
278-- | Lookup the @GOOGLE_APPLICATION_CREDENTIALS@ environment variable for the
263-- default application credentials filepath.
279-- default application credentials filepath.
@@ -389,18 +405,15 @@ data ServiceAccount = ServiceAccount
390instance FromJSON ServiceAccount where
406instance FromJSON ServiceAccount where
391 parseJSON = withObject "service_account" $ \o -> do
407 parseJSON = withObject "service_account" $ \o -> do
392− bs <- Text.encodeUtf8 <$> o .: "private_key_id"
408+ bs <- Text.encodeUtf8 <$> o .: "private_key"
393 ServiceAccount
409 ServiceAccount
394 <$> o .: "client_id"
410 <$> o .: "client_id"
395 <*> o .: "client_email"
411 <*> o .: "client_email"
396− <*> o .: "private_key"
412+ <*> o .: "private_key_id"
400− case listToMaybe (readKeyFileFromMemory bs) of
413+ <*> case listToMaybe (readKeyFileFromMemory bs) of
401 Just (PrivKeyRSA k) -> pure k
414 Just (PrivKeyRSA k) -> pure k
403− fail "Unable to parse key contents from 'private_key_id'"
416+ fail "Unable to parse key contents from \"private_key\""
406-- "client_id": "32555940559.apps.googleusercontent.com",
419-- "client_id": "32555940559.apps.googleusercontent.com",