PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 2ms app.codeStats 0ms

/src/Models/User.hs

http://github.com/biilmann/eventsource-broker
Haskell | 28 lines | 22 code | 5 blank | 1 comment | 1 complexity | fdd917fdb7620df6477a8098663fdc31 MD5 | raw file
 1{-# LANGUAGE OverloadedStrings #-}
 2module Models.User where
 3
 4import           Prelude hiding (lookup)
 5import           Data.UString (UString)
 6import qualified Data.UString as US
 7import           Data.ByteString (ByteString)
 8import qualified Data.ByteString.Char8 as BS
 9import qualified Data.ByteString.Lazy as LBS
10import           Data.Digest.Pure.SHA (sha1)
11import           DB
12
13data User = User { apiKey :: UString, apiSecret :: UString }
14
15get :: DB -> UString -> IO (Either Failure (Maybe User))
16get db key = do
17    result <- run db $ findOne (select ["key" =: key] "users")
18    return $ returnModel constructor result
19
20authenticate :: User -> ByteString -> ByteString -> Bool 
21authenticate user token timestamp =
22    let key    = US.toByteString $ apiKey user
23        secret = US.toByteString $ apiSecret user
24        digest = sha1 $ LBS.fromChunks [key, ":", secret, ":", timestamp] in
25    show digest == BS.unpack token
26
27constructor :: Document -> User
28constructor doc = User (at "key" doc) (at "secret" doc)