unkai-0.1.0.1: src/Unkai/Auth/ServiceAccount.hs
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
-- |
-- Module : Unkai.Auth.ServiceAccount
-- 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)
--
-- Credentials for Server to Server communication, such as between a deployed web
-- application and a Google service.
--
-- Typically, an application uses a service account when the application uses
-- Google services to work with its own data rather than a specific user's data.
--
-- /See:/ <https://developers.google.com/identity/protocols/OAuth2ServiceAccount Service Account Documentation>.
module Unkai.Auth.ServiceAccount
( ServiceAccount (..),
AuthorizedUser (..),
maxTokenLifetime,
metadataToken,
authorizedUserToken,
serviceAccountToken,
encodeBearerJWT,
)
where
import Control.Applicative ((<|>))
import Control.Monad.Catch
import Control.Monad.IO.Class
import Crypto.Hash.Algorithms (SHA256 (..))
import Crypto.PubKey.RSA.PKCS15 (signSafer)
import Data.Aeson
import Data.ByteString.Char8 qualified as BS8
import Data.Text qualified as Text
import Data.Text.Encoding qualified as Text
import Data.Time.Clock.POSIX
import Network.HTTP.Client (Manager, RequestBody (RequestBodyBS))
import Network.HTTP.Client qualified as Client
import Unkai.Auth.Scope (KnownScopes (..), concatScopes)
import Unkai.Compute.Metadata
import Unkai.Internal.Auth
import Unkai.Internal.Logger
import Unkai.Prelude hiding (buildText)
-- | The maximum lifetime of a @service_account@ token which is 1 hour, in seconds.
maxTokenLifetime :: Seconds
maxTokenLifetime = 3600
-- | Obtain an 'OAuthToken' from the local instace metadata
-- using the specific 'ServiceId'.
--
-- For example: @http:\/\/metadata.google.internal\/computeMetadata\/v1\/instance\/service-accounts\/default\/token@
-- will be retrieved if the given 'ServiceId' is @\"default\"@.
metadataToken ::
(MonadIO m) =>
ServiceId ->
Logger ->
Manager ->
m (OAuthToken s)
metadataToken s =
refreshRequest $
metadataRequest
{ Client.path =
"/computeMetadata/v1/instance/service-accounts/"
<> Text.encodeUtf8 (toQueryParam s)
<> "/token"
}
-- | Use the 'AuthorizedUser' to obtain a new 'OAuthToken'. If the supplied
-- 'RefreshToken' is 'Nothing', the original 'RefreshToken' from the user will
-- be used.
authorizedUserToken ::
(MonadIO m) =>
AuthorizedUser ->
Maybe RefreshToken ->
Logger ->
Manager ->
m (OAuthToken s)
authorizedUserToken u r logger manager = do
token <- refreshRequest request logger manager
pure token {_tokenRefresh = _tokenRefresh token <|> Just refresh}
where
refresh = fromMaybe (_userRefresh u) r
request =
Client.urlEncodedBody
[ ("grant_type", "refresh_token"),
("client_id", toHeader (_userId u)),
("client_secret", toHeader (_userSecret u)),
("refresh_token", toHeader refresh)
]
tokenRequest
-- | Obtain an 'OAuthToken' from 'tokenURL'
-- by signing and sending a JSON Web Token (JWT) using the supplied 'ServiceAccount'.
serviceAccountToken ::
(MonadIO m, KnownScopes s) =>
ServiceAccount ->
proxy s ->
Logger ->
Manager ->
m (OAuthToken s)
serviceAccountToken s p l m = do
b <- encodeBearerJWT s p
let rq =
tokenRequest
{ Client.requestBody =
RequestBodyBS $
"grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer"
<> "&assertion="
<> b
}
refreshRequest rq l m
-- | Encode the supplied 'ServiceAccount's key id, email, and scopes using the
-- private key in the JSON Web Token (JWT) format.
encodeBearerJWT ::
(MonadIO m, KnownScopes s) =>
ServiceAccount ->
proxy s ->
m ByteString
encodeBearerJWT s p = liftIO $ do
i <- input . truncate <$> getPOSIXTime
r <- signSafer (Just SHA256) (_servicePrivateKey s) i
either failure (pure . concat' i) r
where
concat' i x = i <> "." <> signature (base64 x)
failure e =
throwM $
TokenRefreshError (toEnum 400) (Text.pack (show e)) Nothing
signature bs =
case BS8.unsnoc bs of
Nothing -> mempty
Just (bs', x)
| x == '=' -> bs'
| otherwise -> bs
input n = header <> "." <> payload
where
header =
base64Encode
[ "alg" .= ("RS256" :: Text),
"typ" .= ("JWT" :: Text),
"kid" .= _serviceKeyId s
]
payload =
base64Encode $
[ "aud" .= tokenURL,
"scope" .= concatScopes (scopeVals p),
"iat" .= n,
"exp" .= (n + seconds maxTokenLifetime),
"iss" .= _serviceEmail s
]
<> maybe [] (\sub -> ["sub" .= sub]) (_serviceAccountUser s)