-- |
-- Module      : Amazonka.Presign
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : provisional
-- Portability : non-portable (GHC extensions)
--
-- This module contains functions for presigning requests using 'MonadIO' and
-- not one of the AWS specific transformers.
--
-- It is intended for use directly with "Amazonka.Auth" when only presigning
-- and no other AWS actions are required.
module Amazonka.Presign where

import Amazonka.Data
import Amazonka.Lens ((%~))
import Amazonka.Prelude
import Amazonka.Request (clientRequestURL)
import Amazonka.Types
import qualified Network.HTTP.Types as HTTP

-- | Presign an URL that is valid from the specified time until the
-- number of seconds expiry has elapsed.
--
-- /See:/ 'presign', 'presignWith'
presignURL ::
  (MonadIO m, AWSRequest a) =>
  Auth ->
  Region ->
  -- | Signing time.
  UTCTime ->
  -- | Expiry time.
  Seconds ->
  -- | Request to presign.
  a ->
  m ByteString
presignURL :: Auth -> Region -> UTCTime -> Seconds -> a -> m ByteString
presignURL Auth
a Region
r UTCTime
e Seconds
ts =
  (ClientRequest -> ByteString) -> m ClientRequest -> m ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ClientRequest -> ByteString
clientRequestURL
    (m ClientRequest -> m ByteString)
-> (a -> m ClientRequest) -> a -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auth -> Region -> UTCTime -> Seconds -> a -> m ClientRequest
forall (m :: * -> *) a.
(MonadIO m, AWSRequest a) =>
Auth -> Region -> UTCTime -> Seconds -> a -> m ClientRequest
presign Auth
a Region
r UTCTime
e Seconds
ts

-- | Presign an HTTP request that is valid from the specified time until the
-- number of seconds expiry has elapsed.
--
-- /See:/ 'presignWith', 'presignWithHeaders'
presign ::
  (MonadIO m, AWSRequest a) =>
  Auth ->
  Region ->
  -- | Signing time.
  UTCTime ->
  -- | Expiry time.
  Seconds ->
  -- | Request to presign.
  a ->
  m ClientRequest
presign :: Auth -> Region -> UTCTime -> Seconds -> a -> m ClientRequest
presign =
  (Service -> Service)
-> Auth -> Region -> UTCTime -> Seconds -> a -> m ClientRequest
forall (m :: * -> *) a.
(MonadIO m, AWSRequest a) =>
(Service -> Service)
-> Auth -> Region -> UTCTime -> Seconds -> a -> m ClientRequest
presignWith Service -> Service
forall a. a -> a
id

-- | A variant of 'presign' that allows modifying the default 'Service'
-- definition used to configure the request.
--
-- /See:/ 'presignWithHeaders'
presignWith ::
  (MonadIO m, AWSRequest a) =>
  -- | Modify the default service configuration.
  (Service -> Service) ->
  Auth ->
  Region ->
  -- | Signing time.
  UTCTime ->
  -- | Expiry time.
  Seconds ->
  -- | Request to presign.
  a ->
  m ClientRequest
presignWith :: (Service -> Service)
-> Auth -> Region -> UTCTime -> Seconds -> a -> m ClientRequest
presignWith = ([Header] -> [Header])
-> (Service -> Service)
-> Auth
-> Region
-> UTCTime
-> Seconds
-> a
-> m ClientRequest
forall (m :: * -> *) a.
(MonadIO m, AWSRequest a) =>
([Header] -> [Header])
-> (Service -> Service)
-> Auth
-> Region
-> UTCTime
-> Seconds
-> a
-> m ClientRequest
presignWithHeaders [Header] -> [Header]
defaultHeaders

-- | Modification to the headers that is applied by default (in 'presignWith');
-- removes the "Expect" header which is added to every 'PutObject'.
defaultHeaders :: [HTTP.Header] -> [HTTP.Header]
defaultHeaders :: [Header] -> [Header]
defaultHeaders = (Header -> Bool) -> [Header] -> [Header]
forall a. (a -> Bool) -> [a] -> [a]
filter ((HeaderName -> HeaderName -> Bool
forall a. Eq a => a -> a -> Bool
/= HeaderName
hExpect) (HeaderName -> Bool) -> (Header -> HeaderName) -> Header -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Header -> HeaderName
forall a b. (a, b) -> a
fst)

-- | A variant of 'presign' that allows modifying the default 'Headers'
-- and the default 'Service' definition used to configure the request.
presignWithHeaders ::
  (MonadIO m, AWSRequest a) =>
  -- | Modify the default headers.
  ([Header] -> [Header]) ->
  -- | Modify the default service configuration.
  (Service -> Service) ->
  Auth ->
  Region ->
  -- | Signing time.
  UTCTime ->
  -- | Expiry time.
  Seconds ->
  -- | Request to presign.
  a ->
  m ClientRequest
presignWithHeaders :: ([Header] -> [Header])
-> (Service -> Service)
-> Auth
-> Region
-> UTCTime
-> Seconds
-> a
-> m ClientRequest
presignWithHeaders [Header] -> [Header]
f Service -> Service
g Auth
a Region
r UTCTime
ts Seconds
ex a
x =
  Auth -> (AuthEnv -> m ClientRequest) -> m ClientRequest
forall (m :: * -> *) a.
MonadIO m =>
Auth -> (AuthEnv -> m a) -> m a
withAuth Auth
a ((AuthEnv -> m ClientRequest) -> m ClientRequest)
-> (AuthEnv -> m ClientRequest) -> m ClientRequest
forall a b. (a -> b) -> a -> b
$ \AuthEnv
ae ->
    ClientRequest -> m ClientRequest
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ClientRequest -> m ClientRequest)
-> ClientRequest -> m ClientRequest
forall a b. (a -> b) -> a -> b
$! Signed a -> ClientRequest
forall a. Signed a -> ClientRequest
signedRequest (Signed a -> ClientRequest) -> Signed a -> ClientRequest
forall a b. (a -> b) -> a -> b
$
      Seconds -> Algorithm a
forall a. Seconds -> Algorithm a
requestPresign Seconds
ex (a -> Request a
forall a. AWSRequest a => a -> Request a
request a
x Request a -> (Request a -> Request a) -> Request a
forall a b. a -> (a -> b) -> b
& ([Header] -> Identity [Header])
-> Request a -> Identity (Request a)
forall a. Lens' (Request a) [Header]
requestHeaders (([Header] -> Identity [Header])
 -> Request a -> Identity (Request a))
-> ([Header] -> [Header]) -> Request a -> Request a
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ [Header] -> [Header]
f Request a -> (Request a -> Request a) -> Request a
forall a b. a -> (a -> b) -> b
& (Service -> Identity Service) -> Request a -> Identity (Request a)
forall a. Lens' (Request a) Service
requestService ((Service -> Identity Service)
 -> Request a -> Identity (Request a))
-> (Service -> Service) -> Request a -> Request a
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Service -> Service
g) AuthEnv
ae Region
r UTCTime
ts