-
Notifications
You must be signed in to change notification settings - Fork 332
Spar Polysemy: In-memory interpreters #1920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c3e1dbe
Now and AssIDStore interpreters
isovector 263f946
Add AReqIDStore.Mem
isovector a6235c5
add boolTTL helper
isovector db1e01f
Add BindCookieStore.Mem
isovector 2072759
Add SAMLUserStore.Mem
isovector b948402
Make format
isovector 96995d0
Add DefaultSsoCode.Mem
isovector 3db57a0
Add ScimExternalIdStore.Mem
isovector c4b51ef
Add ScimTokenStore.Mem
isovector 7f5dbb8
Add ScimUserTimesStore.Mem
isovector c8bd681
make format
isovector 2252951
Changelog
isovector ae3d3ea
Merge branch 'develop' into in-mem-interpreters
isovector b983824
Pull out VerdictFormatStore
isovector 39737af
Remove stale comment
isovector edfa1eb
Also emit internal state
isovector 201769f
Hi CI
isovector File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add in-memory interpreters for most Spar effects |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| {-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | ||
|
|
||
| module Spar.Sem.AReqIDStore.Mem where | ||
|
|
||
| import qualified Data.Map as M | ||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State | ||
| import qualified SAML2.WebSSO.Types as SAML | ||
| import Spar.Sem.AReqIDStore | ||
| import Spar.Sem.Now | ||
| import Wire.API.User.Saml (AReqId) | ||
|
|
||
| aReqIDStoreToMem :: | ||
| Member Now r => | ||
| Sem (AReqIDStore ': r) a -> | ||
| Sem r (Map AReqId SAML.Time, a) | ||
| aReqIDStoreToMem = (runState mempty .) $ | ||
| reinterpret $ \case | ||
| Store areqid ti -> modify $ M.insert areqid ti | ||
| UnStore areqid -> modify $ M.delete areqid | ||
| IsAlive areqid -> | ||
| gets (M.lookup areqid) >>= \case | ||
| Just time -> do | ||
| boolTTL False True time | ||
| Nothing -> pure False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| {-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | ||
|
|
||
| module Spar.Sem.AssIDStore.Mem where | ||
|
|
||
| import qualified Data.Map as M | ||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State | ||
| import qualified SAML2.WebSSO.Types as SAML | ||
| import Spar.Sem.AssIDStore | ||
| import Spar.Sem.Now | ||
| import Wire.API.User.Saml (AssId) | ||
|
|
||
| assIdStoreToMem :: | ||
| Member Now r => | ||
| Sem (AssIDStore ': r) a -> | ||
| Sem r (Map AssId SAML.Time, a) | ||
| assIdStoreToMem = (runState mempty .) $ | ||
| reinterpret $ \case | ||
| Store assid ti -> modify $ M.insert assid ti | ||
| UnStore assid -> modify $ M.delete assid | ||
| IsAlive assid -> | ||
| gets (M.lookup assid) >>= \case | ||
| Just time -> boolTTL False True time | ||
| Nothing -> pure False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| module Spar.Sem.BindCookieStore.Mem where | ||
|
|
||
| import Data.Id (UserId) | ||
| import qualified Data.Map as M | ||
| import Data.String.Conversions (cs) | ||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State | ||
| import SAML2.WebSSO | ||
| import qualified SAML2.WebSSO.Cookie as SAML | ||
| import qualified SAML2.WebSSO.Types as SAML | ||
| import Spar.Sem.BindCookieStore | ||
| import Spar.Sem.Now | ||
| import qualified Spar.Sem.Now as Now | ||
| import qualified Web.Cookie as Cky | ||
| import Wire.API.Cookie | ||
|
|
||
| bindCookieStoreToMem :: Member Now r => Sem (BindCookieStore ': r) a -> Sem r (Map BindCookie (SAML.Time, UserId), a) | ||
| bindCookieStoreToMem = (runState mempty .) $ | ||
| reinterpret $ \case | ||
| Insert sbc uid ndt -> do | ||
| let ckyval = BindCookie . cs . Cky.setCookieValue . SAML.fromSimpleSetCookie . getSimpleSetCookie $ sbc | ||
| now <- Now.get | ||
| modify $ M.insert ckyval (addTime ndt now, uid) | ||
| Lookup bc -> do | ||
| gets (M.lookup bc) >>= \case | ||
| Just (time, uid) -> boolTTL Nothing (Just uid) time | ||
| Nothing -> pure Nothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| {-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | ||
|
|
||
| module Spar.Sem.DefaultSsoCode.Mem where | ||
|
|
||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State (get, put, runState) | ||
| import qualified SAML2.WebSSO as SAML | ||
| import Spar.Sem.DefaultSsoCode (DefaultSsoCode (..)) | ||
|
|
||
| defaultSsoCodeToMem :: Sem (DefaultSsoCode ': r) a -> Sem r (Maybe SAML.IdPId, a) | ||
| defaultSsoCodeToMem = (runState Nothing .) $ | ||
| reinterpret $ \case | ||
| Get -> get | ||
| Store ipi -> put $ Just ipi | ||
| Delete -> put Nothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,23 @@ | ||
| module Spar.Sem.Now where | ||
|
|
||
| import Imports | ||
| import Polysemy | ||
| import qualified SAML2.WebSSO as SAML | ||
|
|
||
| data Now m a where | ||
| Get :: Now m SAML.Time | ||
|
|
||
| makeSem ''Now | ||
|
|
||
| -- | Check a time against 'Now', checking if it's still alive (hasn't occurred yet.) | ||
| boolTTL :: | ||
| Member Now r => | ||
| -- | The value to return if the TTL is expired | ||
| a -> | ||
| -- | The value to return if the TTL is alive | ||
| a -> | ||
| SAML.Time -> -- The time to check | ||
| Sem r a | ||
| boolTTL f t time = do | ||
| now <- get | ||
| pure $ bool f t $ now <= time |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| module Spar.Sem.Now.Input where | ||
|
|
||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.Input | ||
| import qualified SAML2.WebSSO as SAML | ||
| import Spar.Sem.Now | ||
|
|
||
| nowToInput :: | ||
| Member (Input SAML.Time) r => | ||
| Sem (Now ': r) a -> | ||
| Sem r a | ||
| nowToInput = interpret $ \case | ||
| Get -> input |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| {-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | ||
|
|
||
| module Spar.Sem.SAMLUserStore.Mem where | ||
|
|
||
| import Control.Lens (view) | ||
| import Data.Coerce (coerce) | ||
| import Data.Id | ||
| import qualified Data.Map as M | ||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State (gets, modify, runState) | ||
| import SAML2.WebSSO (uidTenant) | ||
| import qualified SAML2.WebSSO as SAML | ||
| import Spar.Sem.SAMLUserStore | ||
|
|
||
| newtype UserRefOrd = UserRefOrd {unUserRefOrd :: SAML.UserRef} | ||
| deriving (Eq) | ||
|
|
||
| instance Ord UserRefOrd where | ||
| compare (UserRefOrd (SAML.UserRef is ni)) (UserRefOrd (SAML.UserRef is' ni')) = | ||
| compare is is' <> compare ni ni' | ||
|
|
||
| samlUserStoreToMem :: Sem (SAMLUserStore ': r) a -> Sem r (Map UserRefOrd UserId, a) | ||
| samlUserStoreToMem = (runState @(Map UserRefOrd UserId) mempty .) $ | ||
| reinterpret $ \case | ||
| Insert ur uid -> modify $ M.insert (UserRefOrd ur) uid | ||
| Get ur -> gets $ M.lookup $ UserRefOrd ur | ||
| GetAnyByIssuer is -> gets $ fmap snd . find (eqIssuer is . fst) . M.toList | ||
| GetSomeByIssuer is -> gets $ coerce . filter (eqIssuer is . fst) . M.toList | ||
| DeleteByIssuer is -> modify $ M.filterWithKey (\ref _ -> not $ eqIssuer is ref) | ||
| Delete _uid ur -> modify $ M.delete $ UserRefOrd ur | ||
| where | ||
| eqIssuer :: SAML.Issuer -> UserRefOrd -> Bool | ||
| eqIssuer is = (== is) . view uidTenant . unUserRefOrd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| {-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | ||
|
|
||
| module Spar.Sem.ScimExternalIdStore.Mem where | ||
|
|
||
| import Data.Id (TeamId, UserId) | ||
| import qualified Data.Map as M | ||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State | ||
| import Spar.Sem.ScimExternalIdStore | ||
| import Wire.API.User.Identity (Email) | ||
|
|
||
| scimExternalIdStoreToMem :: | ||
| Sem (ScimExternalIdStore ': r) a -> | ||
| Sem r (Map (TeamId, Email) UserId, a) | ||
| scimExternalIdStoreToMem = (runState mempty .) $ | ||
| reinterpret $ \case | ||
| Insert tid em uid -> modify $ M.insert (tid, em) uid | ||
| Lookup tid em -> gets $ M.lookup (tid, em) | ||
| Delete tid em -> modify $ M.delete (tid, em) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| {-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | ||
|
|
||
| module Spar.Sem.ScimTokenStore.Mem where | ||
|
|
||
| import qualified Data.Map as M | ||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State | ||
| import Spar.Scim | ||
| import Spar.Sem.ScimTokenStore | ||
|
|
||
| scimTokenStoreToMem :: | ||
| Sem (ScimTokenStore ': r) a -> | ||
| Sem r (Map ScimToken ScimTokenInfo, a) | ||
| scimTokenStoreToMem = (runState mempty .) $ | ||
| reinterpret $ \case | ||
| Insert st sti -> modify $ M.insert st sti | ||
| Lookup st -> gets $ M.lookup st | ||
| GetByTeam tid -> gets $ filter ((== tid) . stiTeam) . M.elems | ||
| Delete tid stid -> modify $ M.filter $ \sti -> not $ stiTeam sti == tid && stiId sti == stid | ||
| DeleteByTeam tid -> modify $ M.filter (not . (== tid) . stiTeam) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| {-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | ||
|
|
||
| module Spar.Sem.ScimUserTimesStore.Mem where | ||
|
|
||
| import Data.Id (UserId) | ||
| import Data.Json.Util (UTCTimeMillis, toUTCTimeMillis) | ||
| import qualified Data.Map as M | ||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State | ||
| import Spar.Sem.ScimUserTimesStore | ||
| import Web.Scim.Schema.Common (WithId (WithId)) | ||
| import Web.Scim.Schema.Meta (WithMeta (WithMeta), created, lastModified) | ||
|
|
||
| scimUserTimesStoreToMem :: | ||
| Sem (ScimUserTimesStore ': r) a -> | ||
| Sem r (Map UserId (UTCTimeMillis, UTCTimeMillis), a) | ||
| scimUserTimesStoreToMem = (runState mempty .) $ | ||
| reinterpret $ \case | ||
| Write (WithMeta meta (WithId uid _)) -> modify $ M.insert uid (toUTCTimeMillis $ created meta, toUTCTimeMillis $ lastModified meta) | ||
| Read uid -> gets $ M.lookup uid | ||
| Delete uid -> modify $ M.delete uid |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| {-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-} | ||
|
|
||
| module Spar.Sem.VerdictFormatStore.Mem where | ||
|
|
||
| import qualified Data.Map as M | ||
| import Imports | ||
| import Polysemy | ||
| import Polysemy.State hiding (Get) | ||
| import SAML2.WebSSO (addTime) | ||
| import qualified SAML2.WebSSO.Types as SAML | ||
| import Spar.Sem.Now (Now, boolTTL) | ||
| import qualified Spar.Sem.Now as Now | ||
| import Spar.Sem.VerdictFormatStore | ||
| import Wire.API.User.Saml (AReqId, VerdictFormat) | ||
|
|
||
| verdictFormatStoreToMem :: | ||
| Member Now r => | ||
| Sem (VerdictFormatStore ': r) a -> | ||
| Sem r (Map AReqId (SAML.Time, VerdictFormat), a) | ||
| verdictFormatStoreToMem = | ||
| (runState mempty .) $ | ||
| reinterpret $ \case | ||
| Store ndt areqid vf -> do | ||
| now <- Now.get | ||
| modify $ M.insert areqid (addTime ndt now, vf) | ||
| Get areqid -> do | ||
| gets (M.lookup areqid) >>= \case | ||
| Just (time, vf) -> do | ||
| boolTTL Nothing (Just vf) time | ||
| Nothing -> pure Nothing |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Different PR ok?