{-# LANGUAGE OverloadedStrings #-} module Data.SMTP.Parser.Seal ( parseURISeal ) where import Data.SMTP.Types.Seal -- import Data.SMTP.Types.Resource import qualified Data.Attoparsec.Text as A import qualified Data.ByteString.Base64.URL as B64 import Data.ByteString (ByteString) import Text.StringConvert {- | Parses a seal inside a URI parameter. In the format: cpId:code[:nonce] Where all the values are binary octet streams encoded in base64url. -} parseURISeal :: A.Parser Seal parseURISeal = do mycp <- base64 A.string ":" mycode <- base64 A.choice [ do A.string ":" mynonce <- base64 return . Seal mycp mycode $ Just mynonce, return $ Seal mycp mycode Nothing ] base64 :: A.Parser ByteString base64 = do e <- A.takeWhile isBase64Char case B64.decode . s $ e of Left r -> fail r Right r -> return r where isBase64Char c | c >= '0' && c <= '9' = True | c >= 'a' && c <= 'z' = True | c >= 'A' && c <= 'Z' = True | c == '+' || c == '-' || c == '=' = True | otherwise = False