@@ -16,14 +16,11 @@ module PostgREST.Auth.Jwt
1616 , parseClaims ) where
1717
1818import qualified Data.Aeson as JSON
19- import qualified Data.Aeson.Key as K
2019import qualified Data.Aeson.KeyMap as KM
2120import qualified Data.ByteString as BS
2221import qualified Data.ByteString.Internal as BS
2322import qualified Data.ByteString.Lazy.Char8 as LBS
2423import qualified Data.Scientific as Sci
25- import qualified Data.Text as T
26- import qualified Data.Vector as V
2724import qualified Jose.Jwk as JWT
2825import qualified Jose.Jwt as JWT
2926
@@ -33,12 +30,12 @@ import Data.Text ()
3330import Data.Time.Clock (UTCTime , nominalDiffTimeToSeconds )
3431import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds )
3532
36- import PostgREST.Auth.Types (AuthResult (.. ))
37- import PostgREST.Config (AppConfig (.. ), FilterExp ( .. ), JSPath ,
38- JSPathExp ( .. ), audMatchesCfg )
39- import PostgREST.Error (Error (.. ),
40- JwtClaimsError (AudClaimNotStringOrArray , ExpClaimNotNumber , IatClaimNotNumber , JWTExpired , JWTIssuedAtFuture , JWTNotInAudience , JWTNotYetValid , NbfClaimNotNumber , ParsingClaimsFailed ),
41- JwtDecodeError (.. ), JwtError (.. ))
33+ import PostgREST.Auth.Types (AuthResult (.. ))
34+ import PostgREST.Config (AppConfig (.. ), audMatchesCfg )
35+ import PostgREST.Config.JSPath ( walkJSPath )
36+ import PostgREST.Error (Error (.. ),
37+ JwtClaimsError (AudClaimNotStringOrArray , ExpClaimNotNumber , IatClaimNotNumber , JWTExpired , JWTIssuedAtFuture , JWTNotInAudience , JWTNotYetValid , NbfClaimNotNumber , ParsingClaimsFailed ),
38+ JwtDecodeError (.. ), JwtError (.. ))
4239
4340import Data.Aeson ((.:?) )
4441import Data.Aeson.Types (parseMaybe )
@@ -128,24 +125,6 @@ parseClaims cfg@AppConfig{configJwtRoleClaimKey, configDbAnonRole} time mclaims
128125 , authRole = role
129126 }
130127 where
131- walkJSPath :: Maybe JSON. Value -> JSPath -> Maybe JSON. Value
132- walkJSPath x [] = x
133- walkJSPath (Just (JSON. Object o)) (JSPKey key: rest) = walkJSPath (KM. lookup (K. fromText key) o) rest
134- walkJSPath (Just (JSON. Array ar)) (JSPIdx idx: rest) = walkJSPath (ar V. !? idx) rest
135- walkJSPath (Just (JSON. Array ar)) [JSPFilter (EqualsCond txt)] = findFirstMatch (==) txt ar
136- walkJSPath (Just (JSON. Array ar)) [JSPFilter (NotEqualsCond txt)] = findFirstMatch (/=) txt ar
137- walkJSPath (Just (JSON. Array ar)) [JSPFilter (StartsWithCond txt)] = findFirstMatch T. isPrefixOf txt ar
138- walkJSPath (Just (JSON. Array ar)) [JSPFilter (EndsWithCond txt)] = findFirstMatch T. isSuffixOf txt ar
139- walkJSPath (Just (JSON. Array ar)) [JSPFilter (ContainsCond txt)] = findFirstMatch T. isInfixOf txt ar
140- walkJSPath _ _ = Nothing
141-
142- findFirstMatch matchWith pattern = foldr checkMatch Nothing
143- where
144- checkMatch (JSON. String txt) acc
145- | pattern `matchWith` txt = Just $ JSON. String txt
146- | otherwise = acc
147- checkMatch _ acc = acc
148-
149128 unquoted :: JSON. Value -> BS. ByteString
150129 unquoted (JSON. String t) = encodeUtf8 t
151130 unquoted v = LBS. toStrict $ JSON. encode v
0 commit comments