2018-09-27 14:22:49 +03:00
|
|
|
module Hasura.Server.Auth.JWT.Logging
|
|
|
|
( JwkRefreshLog (..),
|
2020-02-05 10:07:31 +03:00
|
|
|
JwkFetchError (..),
|
2018-09-27 14:22:49 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson
|
2021-06-15 18:05:41 +03:00
|
|
|
import Data.ByteString.Lazy qualified as BL
|
2018-12-13 10:26:15 +03:00
|
|
|
import Hasura.HTTP
|
2021-06-15 18:05:41 +03:00
|
|
|
import Hasura.Logging
|
|
|
|
( EngineLogType (..),
|
2021-09-24 01:56:37 +03:00
|
|
|
Hasura,
|
2021-06-15 18:05:41 +03:00
|
|
|
LogLevel (..),
|
|
|
|
ToEngineLog (..),
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
2021-06-15 18:05:41 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Network.HTTP.Types qualified as HTTP
|
|
|
|
import Network.URI (URI)
|
2018-09-27 14:22:49 +03:00
|
|
|
|
2020-02-05 10:07:31 +03:00
|
|
|
-- | Possible errors during fetching and parsing JWK
|
|
|
|
-- (the 'Text' type at the end is a friendly error message)
|
|
|
|
data JwkFetchError
|
|
|
|
= -- | Exception while making the HTTP request
|
|
|
|
JFEHttpException !HttpException !Text
|
|
|
|
| -- | Non-2xx HTTP errors from the upstream server
|
|
|
|
JFEHttpError !URI !HTTP.Status !BL.ByteString !Text
|
|
|
|
| -- | Error parsing the JWK response itself
|
|
|
|
JFEJwkParseError !Text !Text
|
|
|
|
| -- | Error parsing the expiry of the JWK
|
|
|
|
JFEExpiryParseError !(Maybe Text) Text
|
2019-08-01 13:51:59 +03:00
|
|
|
deriving (Show)
|
|
|
|
|
2020-02-05 10:07:31 +03:00
|
|
|
instance ToJSON JwkFetchError where
|
|
|
|
toJSON = \case
|
|
|
|
JFEHttpException e _ ->
|
|
|
|
object ["http_exception" .= e]
|
|
|
|
JFEHttpError url status body _ ->
|
|
|
|
object
|
|
|
|
[ "status_code" .= HTTP.statusCode status,
|
2021-06-15 18:05:41 +03:00
|
|
|
"url" .= tshow url,
|
|
|
|
"response" .= bsToTxt (BL.toStrict body)
|
2020-02-05 10:07:31 +03:00
|
|
|
]
|
|
|
|
JFEJwkParseError e msg ->
|
|
|
|
object ["error" .= e, "message" .= msg]
|
|
|
|
JFEExpiryParseError e msg ->
|
|
|
|
object ["error" .= e, "message" .= msg]
|
|
|
|
|
2018-09-27 14:22:49 +03:00
|
|
|
data JwkRefreshLog = JwkRefreshLog
|
2020-02-05 10:07:31 +03:00
|
|
|
{ jrlLogLevel :: !LogLevel,
|
|
|
|
jrlMessage :: !(Maybe Text),
|
|
|
|
jrlError :: !(Maybe JwkFetchError)
|
2018-09-27 14:22:49 +03:00
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
|
|
|
instance ToJSON JwkRefreshLog where
|
2020-02-05 10:07:31 +03:00
|
|
|
toJSON (JwkRefreshLog _ msg err) =
|
|
|
|
object
|
|
|
|
[ "message" .= msg,
|
|
|
|
"error" .= err
|
2019-08-01 13:51:59 +03:00
|
|
|
]
|
2018-09-27 14:22:49 +03:00
|
|
|
|
2019-11-26 15:14:21 +03:00
|
|
|
instance ToEngineLog JwkRefreshLog Hasura where
|
2018-09-27 14:22:49 +03:00
|
|
|
toEngineLog jwkRefreshLog =
|
2022-11-08 10:36:26 +03:00
|
|
|
(jrlLogLevel jwkRefreshLog, ELTJwkRefreshLog, toJSON jwkRefreshLog)
|