module Hackage.Security.Util.Base64 (
    Base64 -- opaque
  , fromByteString
  , toByteString
  ) where

import MyPrelude
import Data.ByteString (ByteString)
import qualified Data.ByteString.Char8  as C8  -- only called on B64-enc strings
import qualified Data.ByteString.Base64 as B64

import Hackage.Security.Util.JSON

-- | Simple wrapper around bytestring with ToJSON and FromJSON instances that
-- use base64 encoding.
newtype Base64 = Base64 ByteString

fromByteString :: ByteString -> Base64
fromByteString :: ByteString -> Base64
fromByteString = ByteString -> Base64
Base64

toByteString :: Base64 -> ByteString
toByteString :: Base64 -> ByteString
toByteString (Base64 ByteString
bs) = ByteString
bs

instance Monad m => ToJSON m Base64 where
  toJSON :: Base64 -> m JSValue
toJSON (Base64 ByteString
bs) = forall (m :: * -> *) a. ToJSON m a => a -> m JSValue
toJSON (ByteString -> [Char]
C8.unpack (ByteString -> ByteString
B64.encode ByteString
bs))

instance ReportSchemaErrors m => FromJSON m Base64 where
  fromJSON :: JSValue -> m Base64
fromJSON JSValue
val = do
    [Char]
str <- forall (m :: * -> *) a. FromJSON m a => JSValue -> m a
fromJSON JSValue
val
    case ByteString -> Either [Char] ByteString
B64.decode ([Char] -> ByteString
C8.pack [Char]
str) of
      Left [Char]
_err -> forall (m :: * -> *) a.
ReportSchemaErrors m =>
[Char] -> Maybe [Char] -> m a
expected [Char]
"base-64 encoded string" forall a. Maybe a
Nothing
      Right ByteString
bs  -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ ByteString -> Base64
Base64 ByteString
bs