on develop help sign in
Tweaking naming and related coercions for pretty errors
commitc2b71624065ac63908b10fa0b7a059fd965fef34 authorBrendan Hay <brendan.g.hay@gmail.com> dateSat 31 Oct 2015, 15:34 (11 years ago) parent9f76906
6 files changed+87−75
gogol/src/Network/Google.hs+8 gogol/src/Network/Google/Auth.hs+48−48 gogol/src/Network/Google/Auth/InstalledApplication.hs+1 gogol/src/Network/Google/Auth/Scope.hs+28−14 gogol/src/Network/Google/Auth/ServiceAccount.hs+1 gogol/src/Network/Google/Internal/Auth.hs+1−13
gogol/src/Network/Google.hs+8
@@ -43,6 +43,14 @@ module Network.Google
4343 , newEnvWith
4444 , getApplicationDefault
4545
46+ -- ** Authorization
47+ , allow
48+ , forbid
49+ , (!)
50+
51+ , type Authorize
52+ , type HasScope
53+
4654 -- * Sending Requests
4755 , send
4856
gogol/src/Network/Google/Auth.hs+48−48
@@ -27,54 +27,54 @@
2727-- Explicitly specify your Google credentials, or retrieve them
2828-- from the underlying OS.
2929module Network.Google.Auth
30− (
31− -- * Credentials
32− Credentials (..)
33−
34− -- ** Application Default Credentials
35− , getApplicationDefault
36− , fromWellKnownPath
37− , fromFilePath
38−
39− -- ** Installed Application Credentials
40− , formURL
41−
42− -- ** Authorizing Requests
43− , authorize
44−
45− -- ** Thread-safe Storage
46− , Store
47− , initStore
48−
49− , Auth (..)
50− , exchange
51− , refresh
52−
53− -- ** Default Constants
54− , checkGCEVar
55− , cloudSDKConfigDir
56− , defaultCredentialsFile
57−
58− -- ** Handling Errors
59− , AsAuthError (..)
60− , AuthError (..)
61−
62− -- * OAuth Types
63− , OAuthClient (..)
64− , OAuthToken (..)
65− , OAuthCode (..)
66− , OAuthScope (..)
67−
68− -- * Re-exported Types
69− , AccessToken (..)
70− , RefreshToken (..)
71− , Secret (..)
72− , ServiceId (..)
73− , ClientId (..)
74−
75− -- * Re-exported Modules
76− , module Network.Google.Auth.Scope
77− ) where
30+ (
31+ -- * Credentials
32+ Credentials (..)
33+
34+ -- ** Application Default Credentials
35+ , getApplicationDefault
36+ , fromWellKnownPath
37+ , fromFilePath
38+
39+ -- ** Installed Application Credentials
40+ , formURL
41+
42+ -- ** Authorizing Requests
43+ , authorize
44+
45+ -- ** Thread-safe Storage
46+ , Store
47+ , initStore
48+
49+ , Auth (..)
50+ , exchange
51+ , refresh
52+
53+ -- ** Default Constants
54+ , checkGCEVar
55+ , cloudSDKConfigDir
56+ , defaultCredentialsFile
57+
58+ -- ** Handling Errors
59+ , AsAuthError (..)
60+ , AuthError (..)
61+
62+ -- * OAuth Types
63+ , OAuthClient (..)
64+ , OAuthToken (..)
65+ , OAuthCode (..)
66+ , OAuthScope (..)
67+
68+ -- * Re-exported Types
69+ , AccessToken (..)
70+ , RefreshToken (..)
71+ , Secret (..)
72+ , ServiceId (..)
73+ , ClientId (..)
74+
75+ -- * Re-exported Modules
76+ , module Network.Google.Auth.Scope
77+ ) where
7878
7979import Control.Concurrent
8080import Control.Monad.Catch (MonadCatch)
gogol/src/Network/Google/Auth/InstalledApplication.hs+1
@@ -13,6 +13,7 @@ module Network.Google.Auth.InstalledApplication where
1313import Control.Monad.Catch (MonadCatch)
1414import Control.Monad.IO.Class (MonadIO)
1515import qualified Data.Text.Encoding as Text
16+import Network.Google.Auth.Scope (queryEncodeScopes)
1617import Network.Google.Internal.Auth
1718import Network.Google.Internal.Logger (Logger)
1819import Network.Google.Prelude
gogol/src/Network/Google/Auth/Scope.hs+28−14
@@ -1,5 +1,6 @@
11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE ExplicitNamespaces #-}
3+{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE PolyKinds #-}
45{-# LANGUAGE ScopedTypeVariables #-}
56{-# LANGUAGE TypeFamilies #-}
@@ -18,15 +19,21 @@
1819-- from the underlying OS.
1920module Network.Google.Auth.Scope where
2021
22+import Data.ByteString (ByteString)
23+import qualified Data.ByteString.Char8 as BS8
24+import Data.Coerce (coerce)
25+import Data.Text (Text)
2126import qualified Data.Text as Text
22−import Data.Type.Bool (type (||))
27+import qualified Data.Text.Encoding as Text
28+import Data.Type.Bool (type (||), type If)
2329import Data.Type.Equality (type (==))
2430import Data.Typeable (Proxy (..))
2531import GHC.Exts (Constraint)
2632import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
2733import Network.Google.Internal.Auth (Credentials)
28−import Network.Google.Prelude (GoogleRequest (Ss),
34+import Network.Google.Prelude (GoogleRequest (..),
2935 OAuthScope (..))
36+import Network.HTTP.Types (urlEncode)
3037
3138-- | Type-restrict the supplied credentials/whatever to the specific scopes.
3239--
@@ -39,6 +46,9 @@ import Network.Google.Prelude (GoogleRequest (Ss),
3946allow :: proxy s -> k s -> k s
4047allow _ = id
4148
49+forbid :: k '[] -> k '[]
50+forbid = id
51+
4252-- | Append the type-level lists of two 'Proxy' values.
4353--
4454-- /See:/ 'allow'.
@@ -47,25 +57,20 @@ allow _ = id
4757
4858-- | Determine if _any_ of the scopes a request requires is a subset
4959-- of the scopes the credentials supports.
60+--
61+-- For error message/presentation purposes, this wraps the result of
62+-- the membership checks below.
5063type family Authorize (s :: [Symbol]) a :: Constraint where
51− Authorize s a = HasScope (Ss a ⊆ s) s
52−
53−-- | For error message/presentation purposes, this wraps the result of
54−-- the membership checks below. When 'False fails to match, it will show
55−-- the reduced error case for '⊆' compared against the full
56−-- set of scopes the credentials ('s') have.
57−type family HasScope p s :: Constraint where
58− HasScope 'True s = ()
64+ Authorize s a = HasScope (Scopes a) s ~ 'True
5965
60−type family (⊆) a s where
61− (⊆) a (x ': xs) = x ∈ a || a ⊆ xs
66+type family HasScope a s where
67+ HasScope '[] s = 'True
68+ HasScope a (x ': xs) = x ∈ a || HasScope a xs
6269
63−-- | Is 'a :: *' an element of 'b :: [*]'.
6470type family (∈) a b where
6571 (∈) x '[] = 'False
6672 (∈) x (y ': xs) = x == y || x ∈ xs
6773
68−-- | Type-level append.
6974type family (++) xs ys where
7075 (++) a '[] = a
7176 (++) '[] b = b
@@ -85,3 +90,12 @@ instance (KnownSymbol x, Allow xs) => Allow (x ': xs) where
8590
8691instance Allow s => Allow (Credentials s) where
8792 getScopes _ = getScopes (Proxy :: Proxy s)
93+
94+concatScopes :: [OAuthScope] -> Text
95+concatScopes = Text.intercalate " " . coerce
96+
97+queryEncodeScopes :: [OAuthScope] -> ByteString
98+queryEncodeScopes =
99+ BS8.intercalate "+"
100+ . map (urlEncode True . Text.encodeUtf8)
101+ . coerce
gogol/src/Network/Google/Auth/ServiceAccount.hs+1
@@ -20,6 +20,7 @@ import qualified Data.ByteString.Char8 as BS8
2020import qualified Data.Text as Text
2121import qualified Data.Text.Encoding as Text
2222import Data.Time.Clock.POSIX
23+import Network.Google.Auth.Scope (concatScopes)
2324import Network.Google.Compute.Metadata
2425import Network.Google.Internal.Auth
2526import Network.Google.Internal.Logger
gogol/src/Network/Google/Internal/Auth.hs+1−13
@@ -30,9 +30,7 @@ import Data.ByteArray (ByteArray)
3030import Data.ByteArray.Encoding
3131import Data.ByteString (ByteString)
3232import Data.ByteString.Builder ()
33−import qualified Data.ByteString.Char8 as BS8
3433import qualified Data.ByteString.Lazy as LBS
35−import Data.Coerce (coerce)
3634import Data.Default.Class (def)
3735import Data.String (IsString)
3836import qualified Data.Text as Text
@@ -45,8 +43,7 @@ import Network.Google.Internal.Logger
4543import Network.Google.Prelude
4644import Network.HTTP.Conduit (HttpException, Manager)
4745import qualified Network.HTTP.Conduit as Client
48−import Network.HTTP.Types (Status, hContentType,
49− urlEncode)
46+import Network.HTTP.Types (Status, hContentType)
5047
5148-- | The supported credential mechanisms.
5249data Credentials (s :: [Symbol])
@@ -320,14 +317,5 @@ base64Encode = base64 . LBS.toStrict . encode . object
320317base64 :: ByteArray a => a -> ByteString
321318base64 = convertToBase Base64URLUnpadded
322319
323−queryEncodeScopes :: [OAuthScope] -> ByteString
324−queryEncodeScopes =
325− BS8.intercalate "+"
326− . map (urlEncode True . Text.encodeUtf8)
327− . coerce
328−
329−concatScopes :: [OAuthScope] -> Text
330−concatScopes = Text.intercalate " " . coerce
331−
332320textBody :: Text -> RequestBody
333321textBody = Client.RequestBodyBS . Text.encodeUtf8