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
|
2020-02-05 10:07:31 +03:00
|
|
|
import Network.URI (URI)
|
2018-09-27 14:22:49 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
import Hasura.HTTP
|
2019-12-13 00:46:33 +03:00
|
|
|
import Hasura.Logging (EngineLogType (..), Hasura, InternalLogTypes (..),
|
|
|
|
LogLevel (..), ToEngineLog (..))
|
2018-09-27 14:22:49 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.Server.Logging ()
|
|
|
|
|
2020-02-05 10:07:31 +03:00
|
|
|
import qualified Data.ByteString.Lazy as BL
|
2018-09-27 14:22:49 +03:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Network.HTTP.Types as HTTP
|
|
|
|
|
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
|
|
|
|
= JFEHttpException !HttpException !Text
|
|
|
|
-- ^ Exception while making the HTTP request
|
|
|
|
| JFEHttpError !URI !HTTP.Status !BL.ByteString !Text
|
|
|
|
-- ^ Non-2xx HTTP errors from the upstream server
|
|
|
|
| JFEJwkParseError !Text !Text
|
|
|
|
-- ^ Error parsing the JWK response itself
|
|
|
|
| JFEExpiryParseError !(Maybe Text) Text
|
|
|
|
-- ^ Error parsing the expiry of the JWK
|
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
|
|
|
|
, "url" .= T.pack (show url)
|
|
|
|
, "response" .= bsToTxt (BL.toStrict body)
|
|
|
|
]
|
|
|
|
|
|
|
|
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 =
|
2019-11-26 15:14:21 +03:00
|
|
|
(jrlLogLevel jwkRefreshLog, ELTInternal ILTJwkRefreshLog, toJSON jwkRefreshLog)
|