unkai-0.1.0.1: src/Unkai/Auth.hs
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
-- |
-- Module : Unkai.Auth
-- Copyright : (c) 2015-2022 Brendan Hay
-- License : Mozilla Public License, v. 2.0.
-- Maintainer : Nadeem Bitar <nadeem@gmail.com>
-- Stability : provisional
-- Portability : non-portable (GHC extensions)
--
-- Explicitly specify your Google credentials, or retrieve them
-- from the underlying OS.
module Unkai.Auth
( -- * Credentials
Credentials (..),
AuthorizedUser (..),
ServiceAccount (..),
-- ** Application Default Credentials
getApplicationDefault,
fromWellKnownPath,
fromFilePath,
saveAuthorizedUserToWellKnownPath,
saveAuthorizedUser,
-- ** Service account user impersonation
serviceAccountUser,
-- *** Kubernetes
serviceAccountTokenFile,
-- ** Authorizing Requests
authorize,
-- ** Thread-safe Storage
Store,
initStore,
retrieveAuthFromStore,
retrieveTokenFromStore,
Auth (..),
exchange,
refresh,
-- ** Default Constants
checkGCEVar,
cloudSDKConfigDir,
defaultCredentialsFile,
-- ** Handling Errors
AsAuthError (..),
AuthError (..),
-- * OAuth Types
OAuthToken (..),
OAuthScope (..),
-- * Re-exported Types
AccessToken (..),
RefreshToken (..),
GSecret (..),
ServiceId (..),
ClientId (..),
-- * Re-exported Modules
module Unkai.Auth.Scope,
)
where
import Control.Concurrent
import Control.Monad.IO.Class (MonadIO (..))
import Data.Time (getCurrentTime)
import GHC.TypeLits (Symbol)
import Network.HTTP.Client (Manager)
import Network.HTTP.Client qualified as Client
import Network.HTTP.Types (hAuthorization)
import Unkai.Auth.ApplicationDefault
import Unkai.Auth.Scope
import Unkai.Auth.ServiceAccount
import Unkai.Auth.TokenFile
import Unkai.Compute.Metadata (checkGCEVar)
import Unkai.Internal.Auth
import Unkai.Internal.Logger (Logger)
import Unkai.Prelude
-- | An 'OAuthToken' that can potentially be expired, with the original
-- credentials that can be used to perform a refresh.
data Auth (s :: [Symbol]) = Auth
{ _credentials :: !(Credentials s),
_token :: !(OAuthToken s)
}
-- | Check if the given token is still valid, ie. younger than the projected
-- expiry time.
--
-- This deliberately makes no external calls due to the absolute construction of
-- the '_tokenExpiry' field, unlike the
-- <https://developers.google.com/accounts/docs/OAuth2Login#validatingtoken documented>
-- validation method.
validate :: (MonadIO m) => Auth s -> m Bool
validate a = (< _tokenExpiry (_token a)) <$> liftIO getCurrentTime
-- | Data store which ensures thread-safe access of credentials.
newtype Store (s :: [Symbol]) = Store (MVar (Auth s))
-- | Construct storage containing the credentials which have not yet been
-- exchanged or refreshed.
initStore ::
(MonadIO m, KnownScopes s) =>
Credentials s ->
Logger ->
Manager ->
m (Store s)
initStore c l m = exchange c l m >>= fmap Store . liftIO . newMVar
-- | Retrieve auth from storage
retrieveAuthFromStore ::
(MonadIO m) =>
Store s ->
m (Auth s)
retrieveAuthFromStore (Store s) =
liftIO (readMVar s)
-- | Concurrently read the current token, and if expired, then
-- safely perform a single serial refresh.
retrieveTokenFromStore ::
(MonadIO m, KnownScopes s) =>
Store s ->
Logger ->
Manager ->
m (OAuthToken s)
retrieveTokenFromStore (Store s) l m = do
x <- liftIO (readMVar s)
mx <- validate x
if mx
then pure (_token x)
else liftIO . modifyMVar s $ \y -> do
my <- validate y
if my
then pure (y, _token y)
else do
z <- refresh y l m
pure (z, _token z)
-- | Perform the initial credentials exchange to obtain a valid 'OAuthToken'
-- suitable for authorizing requests.
exchange ::
forall m s.
(MonadIO m, KnownScopes s) =>
Credentials s ->
Logger ->
Manager ->
m (Auth s)
exchange c l = fmap (Auth c) . action l
where
action = case c of
FromMetadata s -> metadataToken s
FromAccount a -> serviceAccountToken a (Proxy :: Proxy s)
FromUser u -> authorizedUserToken u Nothing
FromTokenFile f -> \_l _m -> readTokenFile f
-- | Refresh an existing 'OAuthToken'.
refresh ::
forall m s.
(MonadIO m, KnownScopes s) =>
Auth s ->
Logger ->
Manager ->
m (Auth s)
refresh (Auth c t) l = fmap (Auth c) . action l
where
action = case c of
FromMetadata s -> metadataToken s
FromAccount a -> serviceAccountToken a (Proxy :: Proxy s)
FromUser u -> authorizedUserToken u (_tokenRefresh t)
FromTokenFile f -> \_l _m -> readTokenFile f
-- | Apply the (by way of possible token refresh) a bearer token to the
-- authentication header of a request.
authorize ::
(MonadIO m, KnownScopes s) =>
Client.Request ->
Store s ->
Logger ->
Manager ->
m Client.Request
authorize rq s l m = bearer <$> retrieveTokenFromStore s l m
where
bearer t =
rq
{ Client.requestHeaders =
( hAuthorization,
"Bearer " <> toHeader (_tokenAccess t)
)
: Client.requestHeaders rq
}
-- | Set the user to be impersonated for a service account with domain
-- wide delegation. See
-- https://developers.google.com/identity/protocols/OAuth2ServiceAccount
serviceAccountUser ::
forall s.
Maybe Text ->
Credentials s ->
Credentials s
serviceAccountUser u (FromAccount s) = FromAccount $ s {_serviceAccountUser = u}
serviceAccountUser _ c = c