{-# LANGUAGE OverloadedStrings, FlexibleContexts #-}

module Admin.Server.Middlewares
  ( middlewares
  ) where

import Data.ByteString.Char8 (ByteString)
import Network.Wai (Middleware)
import Network.Wai.Middleware.BearerTokenAuth
import Network.Wai.Middleware.Cors

-- | Middlewares used in Haskell Admin
middlewares :: [ByteString] -> Middleware
middlewares :: [ByteString] -> Middleware
middlewares [ByteString]
tokens = Middleware
allowCors Middleware -> Middleware -> Middleware
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> Middleware
tokenListAuth [ByteString]
tokens

allowCors :: Middleware
allowCors :: Middleware
allowCors = (Request -> Maybe CorsResourcePolicy) -> Middleware
cors (Maybe CorsResourcePolicy -> Request -> Maybe CorsResourcePolicy
forall a b. a -> b -> a
const (Maybe CorsResourcePolicy -> Request -> Maybe CorsResourcePolicy)
-> Maybe CorsResourcePolicy -> Request -> Maybe CorsResourcePolicy
forall a b. (a -> b) -> a -> b
$ CorsResourcePolicy -> Maybe CorsResourcePolicy
forall a. a -> Maybe a
Just CorsResourcePolicy
policy)

policy :: CorsResourcePolicy
policy :: CorsResourcePolicy
policy =
  CorsResourcePolicy
simpleCorsResourcePolicy
    {corsRequestHeaders :: [HeaderName]
corsRequestHeaders = [HeaderName
"Authorization", HeaderName
"Content-Type"]}