Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions postgrest.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ library
, stm-hamt >= 1.2 && < 2
, focus >= 1.0 && < 2
, some >= 1.0.4.1 && < 2
, oidc-client >= 0.8.0.0 && < 0.9
, http-client-tls >= 0.3.6.4 && < 0.4
Comment on lines +161 to +162
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http-client-tls is fine, but oidc-client depends on TemplateHaskell, so would block us for #3281 (comment). We should have very good reasons to introduce that without any sane alternatives, because it would effectively prevent us from ever doing cross compilation to platforms without emulator support.

-- -fno-spec-constr may help keep compile time memory use in check,
-- see https://gitlab.haskell.org/ghc/ghc/issues/16017#note_219304
-- -optP-Wno-nonportable-include-path
Expand Down
16 changes: 16 additions & 0 deletions src/PostgREST/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier (..),
toQi)

import Protolude hiding (Proxy, toList)
import Web.OIDC.Client (Provider(..), discover)
import Network.HTTP.Client.TLS (newTlsManager)
import Network.HTTP.Client (HttpException (InvalidUrlException))

audMatchesCfg :: AppConfig -> Text -> Bool
audMatchesCfg = maybe (const True) (==) . configJwtAudience
Expand Down Expand Up @@ -247,6 +250,7 @@ readAppConfig dbSettings optPath prevDbUri roleSettings roleIsolationLvl = do
decodeLoadFiles parsedConfig = try $
decodeJWKS =<<
decodeSecret =<<
oidcConnectDiscovery =<<
readSecretFile =<<
readDbUriFile prevDbUri parsedConfig

Expand Down Expand Up @@ -478,6 +482,18 @@ parser optPath env dbSettings roleSettings roleIsolationLvl =
defaultServerHost :: Maybe Text -> Text
defaultServerHost = fromMaybe "!4"

oidcConnectDiscovery :: AppConfig -> IO AppConfig
oidcConnectDiscovery conf = maybe (pure conf) (performDiscovery . decodeUtf8) (configJwtSecret conf)
where
performDiscovery uri = oidcDiscover uri `catches` [
Handler (\case
InvalidUrlException _ _ -> pure conf
_ -> fail "OIDC discovery failed")
]
oidcDiscover uri = do
manager <- newTlsManager
(Provider _ keys) <- discover uri manager
pure $ conf { configJWKS = Just $ JWT.JwkSet keys }
-- | Read the JWT secret from a file if configJwtSecret is actually a
-- filepath(has @ as its prefix). To check if the JWT secret is provided is
-- in fact a file path, it must be decoded as 'Text' to be processed.
Expand Down
Loading