packages feed

password-http-api-data-0.1.0.0: src/Data/Password/HttpApiData.hs

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

{-|
Module      : Data.Password.HttpApiData
Copyright   : (c) Dennis Gosnell, 2019; Felix Paulusma, 2020
License     : BSD-style (see LICENSE file)
Maintainer  : cdep.illabout@gmail.com
Stability   : experimental
Portability : POSIX

This module provides `http-api-data` typeclass instances
for 'Password'.

See the "Data.Password.Types" module for more information.
-}

module Data.Password.HttpApiData () where

import Data.Password.Types
import GHC.TypeLits (TypeError, ErrorMessage(..))
import Web.HttpApiData (FromHttpApiData(..), ToHttpApiData(..))

-- $setup
-- >>> :set -XOverloadedStrings
-- >>> :set -XDataKinds
--
-- Import needed functions.
--
-- >>> import Data.Password.Bcrypt (Salt(..), hashPasswordWithSalt, unsafeShowPassword)
-- >>> import Web.HttpApiData (parseUrlPiece)

type ErrMsg = 'Text "Warning! Tried to convert plain-text Password to HttpApiData!"
         :$$: 'Text "  This is likely a security leak. Please make sure whether this was intended."
         :$$: 'Text "  If this is intended, please use 'unsafeShowPassword' before converting to HttpApiData"
         :$$: 'Text ""

-- | This instance allows a 'Password' to be created with functions like
-- 'Web.HttpApiData.parseUrlPiece' or 'Web.HttpApiData.parseQueryParam'.
--
-- >>> let eitherPassword = parseUrlPiece "foobar"
-- >>> fmap unsafeShowPassword eitherPassword
-- Right "foobar"
instance FromHttpApiData Password where
  parseUrlPiece = fmap mkPassword . parseUrlPiece

-- | Type error! Do not transmit plain-text 'Password's over HTTP!
instance TypeError ErrMsg => ToHttpApiData Password where
  toUrlPiece = error "unreachable"