
module Zero.KeyFetchToken.Internal
  (
    KeyFetchToken
  , KeyFetchState(..)

  , defaultKeyFetchState
  ) where

import           Data.Aeson
import           Data.Text (Text)
import           Zero.Swagger
import           Servant.API.Experimental.Auth (AuthProtect)

------------------------------------------------------------------------------
-- Types
------------------------------------------------------------------------------

-- | Type used which requires an auth token with hawk
type KeyFetchToken = AuthProtect "KeyFetchToken"

------------------------------------------------------------------------------

data KeyFetchState = KeyFetchState {
    ks_kA :: Text
  , ks_wrap_kB :: Text
  } deriving (Show, Generic)

instance ToJSON KeyFetchState where
  toJSON (KeyFetchState kA wrap_kB) =
    object [
      "kA" .= kA
    , "wrap_kB" .= wrap_kB
    ]

instance FromJSON KeyFetchState where
  parseJSON = withObject "KeyFetchState" $ \v -> KeyFetchState
    <$> v .: "kA"
    <*> v .: "wrap_kB"

instance ToSchema KeyFetchState where
  declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
    & mapped.schema.description ?~ "The public / private master key of this account. These keys are the base of future client-side key derivations. "
    & mapped.schema.example ?~ toJSON defaultKeyFetchState

------------------------------------------------------------------------------
-- Constructors
------------------------------------------------------------------------------

defaultKeyFetchState =
  KeyFetchState "<bytestring>" "<bytestring>"

