on develop help sign in
Tweaking naming
commit14897f845088d02a98133582c6379b9e97ca650c authorBrendan Hay <brendan.g.hay@gmail.com> dateWed 28 Oct 2015, 14:27 (11 years ago) parentc50d087
4 files changed+87−82
gogol/src/Network/Google.hs+1 gogol/src/Network/Google/Auth.hs+83−28 gogol/src/Network/Google/Auth/ApplicationDefaultCredentials.hs−51 gogol/src/Network/Google/Auth/ServiceAccount.hs+3−3
gogol/src/Network/Google.hs+1
@@ -105,6 +105,7 @@ import qualified Control.Monad.Writer.Lazy as LW
105105import qualified Control.Monad.Writer.Strict as W
106106import Network.Google.Auth
107107import Network.Google.Env
108+import Network.Google.Internal.Auth
108109import Network.Google.Internal.Body
109110import Network.Google.Internal.HTTP
110111import Network.Google.Internal.Logger
gogol/src/Network/Google/Auth.hs+83−28
@@ -133,34 +133,10 @@ import Network.HTTP.Conduit hiding
133133 (Request)
134134import qualified Network.HTTP.Conduit as Client
135135import Network.HTTP.Types
136−
137−-- | Attempt credentials discovery via the following steps:
138−--
139−-- * Read the default credentials from a file specified by
140−-- the environment variable @GOOGLE_APPLICATION_CREDENTIALS@ if it exists.
141−--
142−-- * Read the platform equivalent of @~\/.config\/gcloud\/application_default_credentials.json@ if it exists.
143−-- The @~/.config@ component of the path can be overriden by the environment
144−-- variable @CLOUDSDK_CONFIG@ if it exists.
145−--
146−-- * Retrieve the default service account application credentials if
147−-- running on GCE. The environment variable @NO_GCE_CHECK@ can be used to
148−-- skip this check if set to a truthy value such as @1@ or @true@.
149−--
150−-- The specified 'Scope's are used to authorize any @service_account@ that is
151−-- found with the appropriate scopes, otherwise they are not used. See the
152−-- top-level module of each individual @gogol-*@ library for a list of available
153−-- scopes, such as @Network.Google.Compute.computeScope@.
154−-- discover :: (MonadIO m, MonadCatch m)
155−-- => [OAuthScope]
156−-- -> Manager
157−-- -> m Credentials
158−-- discover ss m =
159−-- catching _MissingFileError (fromFile ss) $ \f -> do
160−-- p <- isGCE m
161−-- unless p $
162−-- throwingM _MissingFileError f
163−-- pure $! FromMetadata "default"
136+import System.Directory (doesFileExist, getHomeDirectory)
137+import System.Environment (lookupEnv)
138+import System.FilePath ((</>))
139+import System.Info (os)
164140
165141-- | The supported credential mechanisms.
166142data Credentials
@@ -189,6 +165,85 @@ data Credentials
189165 -- An 'AuthorizedUser' is typically created by the @gcloud init@ command
190166 -- of the Google CloudSDK Tools.
191167
168+-- | Attempt credentials discovery via the following steps:
169+--
170+-- * Read the default credentials from a file specified by
171+-- the environment variable @GOOGLE_APPLICATION_CREDENTIALS@ if it exists.
172+--
173+-- * Read the platform equivalent of @~\/.config\/gcloud\/application_default_credentials.json@ if it exists.
174+-- The @~/.config@ component of the path can be overriden by the environment
175+-- variable @CLOUDSDK_CONFIG@ if it exists.
176+--
177+-- * Retrieve the default service account application credentials if
178+-- running on GCE. The environment variable @NO_GCE_CHECK@ can be used to
179+-- skip this check if set to a truthy value such as @1@ or @true@.
180+--
181+-- The specified 'Scope's are used to authorize any @service_account@ that is
182+-- found with the appropriate scopes, otherwise they are not used. See the
183+-- top-level module of each individual @gogol-*@ library for a list of available
184+-- scopes, such as @Network.Google.Compute.computeScope@.
185+discover :: (MonadIO m, MonadCatch m)
186+ => [OAuthScope]
187+ -> Manager
188+ -> m Credentials
189+discover ss m =
190+ catching _MissingFileError (fromFile ss) $ \f -> do
191+ p <- isGCE m
192+ unless p $
193+ throwingM _MissingFileError f
194+ pure $! FromMetadata "default"
195+
196+-- | Attempt to load either a @service_account@ or @authorized_user@ formatted
197+-- file to obtain the credentials neccessary to perform a token refresh.
198+--
199+-- The specified 'Scope's are used to authorize any @service_account@ that is
200+-- found with the appropriate scopes, otherwise they are not used. See the
201+-- top-level module of each individual @gogol-*@ library for a list of available
202+-- scopes, such as @Network.Google.Compute.computeScope@.
203+--
204+-- /See:/ 'cloudSDKConfigPath', 'defaultCredentialsPath'.
205+fromFile :: (MonadIO m, MonadCatch m) => [OAuthScope] -> m Credentials
206+fromFile ss = do
207+ f <- defaultCredentialsPath
208+ case f of
209+ Just x -> fromFilePath ss x
210+ Nothing -> do
211+ x <- cloudSDKConfigPath
212+ fromFilePath ss x
213+
214+-- | Attempt to load either a @service_account@ or @authorized_user@ formatted
215+-- file to obtain the credentials neccessary to perform a token refresh from
216+-- the specified file.
217+--
218+-- The specified 'Scope's are used to authorize any @service_account@ that is
219+-- found with the appropriate scopes, otherwise they are not used. See the
220+-- top-level module of each individual @gogol-*@ library for a list of available
221+-- scopes, such as @Network.Google.Compute.computeScope@.
222+fromFilePath :: (MonadIO m, MonadCatch m)
223+ => [OAuthScope]
224+ -> FilePath
225+ -> m Credentials
226+fromFilePath ss f = do
227+ p <- liftIO (doesFileExist f)
228+ unless p $
229+ throwM (MissingFileError f)
230+ bs <- liftIO (LBS.readFile f)
231+ either (throwM . InvalidFileError f . Text.pack) pure
232+ (fromJSONCredentials ss bs)
233+
234+fromJSONCredentials :: [OAuthScope]
235+ -> LBS.ByteString
236+ -> Either String Credentials
237+fromJSONCredentials ss bs = do
238+ v <- eitherDecode' bs
239+ let x = (`FromAccount` ss) <$> parseEither parseJSON v
240+ y = FromUser <$> parseEither parseJSON v
241+ case (x, y) of
242+ (Left xe, Left ye) -> Left $
243+ "Failed parsing service_account: " ++ xe ++
244+ ", Failed parsing authorized_user: " ++ ye
245+ _ -> x <|> y
246+
192247exchange :: (MonadIO m, MonadCatch m)
193248 => Credentials
194249 -> Logger
gogol/src/Network/Google/Auth/ApplicationDefaultCredentials.hs−51
@@ -109,57 +109,6 @@ authorizedUserToken u r = refreshRequest $
109109 <> "&refresh_token=" <> toText (fromMaybe (_userRefresh u) r)
110110 }
111111
112−-- -- | Attempt to load either a @service_account@ or @authorized_user@ formatted
113−-- -- file to obtain the credentials neccessary to perform a token refresh.
114−-- --
115−-- -- The specified 'Scope's are used to authorize any @service_account@ that is
116−-- -- found with the appropriate scopes, otherwise they are not used. See the
117−-- -- top-level module of each individual @gogol-*@ library for a list of available
118−-- -- scopes, such as @Network.Google.Compute.computeScope@.
119−-- --
120−-- -- /See:/ 'cloudSDKConfigPath', 'defaultCredentialsPath'.
121−-- fromFile :: (MonadIO m, MonadCatch m) => [OAuthScope] -> m Credentials
122−-- fromFile ss = do
123−-- f <- defaultCredentialsPath
124−-- case f of
125−-- Just x -> fromFilePath ss x
126−-- Nothing -> do
127−-- x <- cloudSDKConfigPath
128−-- fromFilePath ss x
129−
130−-- -- | Attempt to load either a @service_account@ or @authorized_user@ formatted
131−-- -- file to obtain the credentials neccessary to perform a token refresh from
132−-- -- the specified file.
133−-- --
134−-- -- The specified 'Scope's are used to authorize any @service_account@ that is
135−-- -- found with the appropriate scopes, otherwise they are not used. See the
136−-- -- top-level module of each individual @gogol-*@ library for a list of available
137−-- -- scopes, such as @Network.Google.Compute.computeScope@.
138−-- fromFilePath :: (MonadIO m, MonadCatch m)
139−-- => [OAuthScope]
140−-- -> FilePath
141−-- -> m Credentials
142−-- fromFilePath ss f = do
143−-- p <- liftIO (doesFileExist f)
144−-- unless p $
145−-- throwM (MissingFileError f)
146−-- bs <- liftIO (LBS.readFile f)
147−-- either (throwM . InvalidFileError f . Text.pack) pure
148−-- (fromJSONCredentials ss bs)
149−
150−-- fromJSONCredentials :: [OAuthScope]
151−-- -> LBS.ByteString
152−-- -> Either String Credentials
153−-- fromJSONCredentials ss bs = do
154−-- v <- eitherDecode' bs
155−-- let x = (`FromAccount` ss) <$> parseEither parseJSON v
156−-- y = FromUser <$> parseEither parseJSON v
157−-- case (x, y) of
158−-- (Left xe, Left ye) -> Left $
159−-- "Failed parsing service_account: " ++ xe ++
160−-- ", Failed parsing authorized_user: " ++ ye
161−-- _ -> x <|> y
162−
163112-- | Lookup the @GOOGLE_APPLICATION_CREDENTIALS@ environment variable for the
164113-- default application credentials filepath.
165114defaultCredentialsPath :: MonadIO m => m (Maybe FilePath)
gogol/src/Network/Google/Auth/ServiceAccount.hs+3−3
@@ -105,7 +105,7 @@ serviceAccountToken :: (MonadIO m, MonadCatch m)
105105 -> Manager
106106 -> m OAuthToken
107107serviceAccountToken s ss l m = do
108− b <- encodeJWTBearer s ss
108+ b <- encodeBearerJWT s ss
109109 let rq = accountsRequest
110110 { Client.requestBody = RequestBodyBS $
111111 "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer"
@@ -126,11 +126,11 @@ metadataToken s = refreshRequest $
126126 <> "/token"
127127 }
128128
129−encodeJWTBearer :: (MonadIO m, MonadThrow m)
129+encodeBearerJWT :: (MonadIO m, MonadThrow m)
130130 => ServiceAccount
131131 -> [OAuthScope]
132132 -> m ByteString
133−encodeJWTBearer s ss = liftIO $ do
133+encodeBearerJWT s ss = liftIO $ do
134134 i <- input . truncate <$> getPOSIXTime
135135 r <- signSafer (Just SHA256) (_servicePrivateKey s) i
136136 either failure (pure . concat' i) r