Seal.hs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Data.SMTP.Parser.Seal (
  3. parseURISeal
  4. ) where
  5. import Data.SMTP.Types.Seal
  6. -- import Data.SMTP.Types.Resource
  7. import qualified Data.Attoparsec.Text as A
  8. import qualified Data.ByteString.Base64.URL as B64
  9. import Data.ByteString (ByteString)
  10. import Text.StringConvert
  11. {- |
  12. Parses a seal inside a URI parameter.
  13. In the format: cpId:code[:nonce]
  14. Where all the values are binary octet streams
  15. encoded in base64url.
  16. -}
  17. parseURISeal :: A.Parser Seal
  18. parseURISeal = do
  19. mycp <- base64
  20. A.string ":"
  21. mycode <- base64
  22. A.choice [
  23. do
  24. A.string ":"
  25. mynonce <- base64
  26. return . Seal mycp mycode $ Just mynonce,
  27. return $ Seal mycp mycode Nothing
  28. ]
  29. base64 :: A.Parser ByteString
  30. base64 = do
  31. e <- A.takeWhile isBase64Char
  32. case B64.decode . s $ e of
  33. Left r -> fail r
  34. Right r -> return r
  35. where
  36. isBase64Char c
  37. | c >= '0' && c <= '9' = True
  38. | c >= 'a' && c <= 'z' = True
  39. | c >= 'A' && c <= 'Z' = True
  40. | c == '+' || c == '-' || c == '=' = True
  41. | otherwise = False