Skip to content

Commit bb0eeef

Browse files
committed
add: Oidc Connect Discovery
1 parent 55cd5a2 commit bb0eeef

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

postgrest.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ library
158158
, stm-hamt >= 1.2 && < 2
159159
, focus >= 1.0 && < 2
160160
, some >= 1.0.4.1 && < 2
161+
, oidc-client >= 0.8.0.0 && < 0.9
162+
, http-client-tls >= 0.3.6.4 && < 0.4
161163
-- -fno-spec-constr may help keep compile time memory use in check,
162164
-- see https://gitlab.haskell.org/ghc/ghc/issues/16017#note_219304
163165
-- -optP-Wno-nonportable-include-path

src/PostgREST/Config.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier (..),
6767
toQi)
6868

6969
import Protolude hiding (Proxy, toList)
70+
import Web.OIDC.Client (Provider(..), discover)
71+
import Network.HTTP.Client.TLS (newTlsManager)
72+
import Network.HTTP.Client (HttpException (InvalidUrlException))
73+
import Control.Arrow ((***), (&&&))
7074

7175
audMatchesCfg :: AppConfig -> Text -> Bool
7276
audMatchesCfg = maybe (const True) (==) . configJwtAudience
@@ -247,6 +251,7 @@ readAppConfig dbSettings optPath prevDbUri roleSettings roleIsolationLvl = do
247251
decodeLoadFiles parsedConfig = try $
248252
decodeJWKS =<<
249253
decodeSecret =<<
254+
oidcConnectDiscovery =<<
250255
readSecretFile =<<
251256
readDbUriFile prevDbUri parsedConfig
252257

@@ -478,6 +483,18 @@ parser optPath env dbSettings roleSettings roleIsolationLvl =
478483
defaultServerHost :: Maybe Text -> Text
479484
defaultServerHost = fromMaybe "!4"
480485

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

0 commit comments

Comments
 (0)