From b651a4573424bf5ee41044ec60a8fb385e4d3810 Mon Sep 17 00:00:00 2001 From: calebmer Date: Sat, 28 Nov 2015 16:40:44 -0500 Subject: [PATCH 1/4] Fix time fetch on initialization --- src/PostgREST/Main.hs | 4 +++- src/PostgREST/Middleware.hs | 9 +++------ test/SpecHelper.hs | 4 +++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/PostgREST/Main.hs b/src/PostgREST/Main.hs index fed0c41df..8faec1be8 100644 --- a/src/PostgREST/Main.hs +++ b/src/PostgREST/Main.hs @@ -17,6 +17,7 @@ import Data.Functor.Identity import Data.Monoid ((<>)) import Data.String.Conversions (cs) import Data.Text (Text) +import Data.Time.Clock.POSIX (getPOSIXTime) import qualified Hasql as H import qualified Hasql.Postgres as P import Network.Wai @@ -73,7 +74,8 @@ main = do dbStructure <- either hasqlError return dbOrError runSettings appSettings $ middle $ \ req respond -> do + time <- getPOSIXTime body <- strictRequestBody req resOrError <- liftIO $ H.session pool $ H.tx txSettings $ - runWithClaims conf (app dbStructure conf body) req + runWithClaims conf time (app dbStructure conf body) req either (respond . pgErrResponse) respond resOrError diff --git a/src/PostgREST/Middleware.hs b/src/PostgREST/Middleware.hs index 0a3885e19..1a80fef7a 100644 --- a/src/PostgREST/Middleware.hs +++ b/src/PostgREST/Middleware.hs @@ -6,7 +6,7 @@ module PostgREST.Middleware where import Data.Maybe (fromMaybe) import Data.Text import Data.String.Conversions (cs) -import Data.Time.Clock.POSIX (getPOSIXTime) +import Data.Time.Clock (NominalDiffTime) import qualified Hasql as H import qualified Hasql.Postgres as P @@ -23,20 +23,17 @@ import PostgREST.Auth (setRole, jwtClaims, claimsToSQL) import PostgREST.Config (AppConfig (..), corsPolicy) import PostgREST.Error (errResponse) -import System.IO.Unsafe (unsafePerformIO) - import Prelude hiding(concat) import qualified Data.Vector as V import qualified Hasql.Backend as B import qualified Data.Map.Lazy as M -runWithClaims :: forall s. AppConfig -> +runWithClaims :: forall s. AppConfig -> NominalDiffTime -> (Request -> H.Tx P.Postgres s Response) -> Request -> H.Tx P.Postgres s Response -runWithClaims conf app req = do +runWithClaims conf time app req = do _ <- H.unitEx $ stmt setAnon - let time = unsafePerformIO getPOSIXTime case split (== ' ') (cs auth) of ("Bearer" : tokenStr : _) -> case jwtClaims jwtSecret tokenStr time of diff --git a/test/SpecHelper.hs b/test/SpecHelper.hs index f7c429cca..867abca38 100644 --- a/test/SpecHelper.hs +++ b/test/SpecHelper.hs @@ -12,6 +12,7 @@ import Data.String.Conversions (cs) import Data.Monoid import Data.Text hiding (map) import qualified Data.Vector as V +import Data.Time.Clock.POSIX (getPOSIXTime) import Control.Monad (void) import Control.Applicative @@ -59,9 +60,10 @@ withApp perform = do db <- either (fail . show) return dbOrError perform $ middle $ \req resp -> do + time <- getPOSIXTime body <- strictRequestBody req result <- liftIO $ H.session pool $ H.tx txSettings - $ runWithClaims cfg (app db cfg body) req + $ runWithClaims cfg time (app db cfg body) req either (resp . pgErrResponse) resp result where middle = defaultMiddle From b04e2ec6636805f1caa3460c8c11b9b08a471056 Mon Sep 17 00:00:00 2001 From: calebmer Date: Sat, 28 Nov 2015 17:41:54 -0500 Subject: [PATCH 2/4] Change updated package error to warning --- circle.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/circle.yml b/circle.yml index 74c1982c8..fe35c157e 100644 --- a/circle.yml +++ b/circle.yml @@ -13,4 +13,4 @@ dependencies: test: post: - cabal exec hlint -- -X QuasiQuotes src/**/*.hs test/**/*.hs - - cabal exec packdeps postgrest.cabal + - cabal exec packdeps postgrest.cabal || true From e7a47215f09809af7ec4e22381f080f76d4f5315 Mon Sep 17 00:00:00 2001 From: Caleb Meredith Date: Sat, 28 Nov 2015 17:56:37 -0500 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43ca9a02a..1d9dc2c6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +### Fixed +- Miscalculation of time used for expiring tokens - @calebmer + ## [0.3.0.1] - 2015-11-27 ### Fixed From ed515023879ce6c4a82728856cf6dedbc23acc32 Mon Sep 17 00:00:00 2001 From: Caleb Meredith Date: Sat, 28 Nov 2015 18:03:27 -0500 Subject: [PATCH 4/4] Add unreleased line to top of changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d9dc2c6b..d808b192e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + ### Fixed - Miscalculation of time used for expiring tokens - @calebmer