@@ -20,20 +20,21 @@ module Network.Google.Auth.ApplicationDefault where
21import Control.Applicative
21import Control.Applicative
22import Control.Exception.Lens (catching, throwingM)
22import Control.Exception.Lens (catching, throwingM)
23−import Control.Monad (unless)
23+import Control.Monad (unless, when)
24import Control.Monad.Catch
24import Control.Monad.Catch
25import Control.Monad.IO.Class (MonadIO (..))
25import Control.Monad.IO.Class (MonadIO (..))
26import Data.Aeson
26import Data.Aeson
27import Data.Aeson.Types (parseEither)
27import Data.Aeson.Types (parseEither)
28import qualified Data.ByteString.Lazy as LBS
28import qualified Data.ByteString.Lazy as LBS
29+import Data.Maybe (maybe)
29import qualified Data.Text as Text
30import qualified Data.Text as Text
30import Network.Google.Compute.Metadata (isGCE)
31import Network.Google.Compute.Metadata (isGCE)
31import Network.Google.Internal.Auth
32import Network.Google.Internal.Auth
33import Network.HTTP.Conduit (Manager)
34import Network.HTTP.Conduit (Manager)
34−import System.Directory (doesFileExist, getHomeDirectory)
35+import System.Directory (doesFileExist, getHomeDirectory, createDirectoryIfMissing)
35import System.Environment (lookupEnv)
36import System.Environment (lookupEnv)
36−import System.FilePath ((</>))
37+import System.FilePath ((</>), takeDirectory)
37import System.Info (os)
38import System.Info (os)
39-- | The environment variable name which is used to specify the directory
40-- | The environment variable name which is used to specify the directory
@@ -126,6 +127,30 @@ fromFilePath f = do
126 either (throwM . InvalidFileError f . Text.pack) pure
127 either (throwM . InvalidFileError f . Text.pack) pure
127 (fromJSONCredentials bs)
128 (fromJSONCredentials bs)
130+-- | Save 'AuthorizedUser'
131+-- /See:/ 'cloudSDKConfigPath', 'defaultCredentialsPath'.
132+saveAuthorizedUserToWellKnownPath :: (MonadIO m, MonadCatch m)
133+ => Bool -- ^ Force to save if True
136+saveAuthorizedUserToWellKnownPath b a = do
137+ d <- defaultCredentialsPath
138+ f <- maybe cloudSDKConfigPath pure d
139+ liftIO $ createDirectoryIfMissing True $ takeDirectory f
140+ saveAuthorizedUser f b a
142+-- | Save 'AuthorizedUser'
143+saveAuthorizedUser :: (MonadIO m, MonadCatch m)
145+ -> Bool -- ^ Force to save if True
148+saveAuthorizedUser f b a = do
149+ p <- liftIO (doesFileExist f)
151+ throwM (FileExistError f)
152+ liftIO (LBS.writeFile f $ encode a)
129-- | Attempt to parse either a @service_account@ or @authorized_user@ formatted
154-- | Attempt to parse either a @service_account@ or @authorized_user@ formatted
130-- JSON value to obtain credentials.
155-- JSON value to obtain credentials.
131fromJSONCredentials :: LBS.ByteString -> Either String (Credentials s)
156fromJSONCredentials :: LBS.ByteString -> Either String (Credentials s)