{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.AppSync.Types.LambdaAuthorizerConfig
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.AppSync.Types.LambdaAuthorizerConfig where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude

-- | A @LambdaAuthorizerConfig@ specifies how to authorize AppSync API access
-- when using the @AWS_LAMBDA@ authorizer mode. Be aware that an AppSync
-- API can have only one Lambda authorizer configured at a time.
--
-- /See:/ 'newLambdaAuthorizerConfig' smart constructor.
data LambdaAuthorizerConfig = LambdaAuthorizerConfig'
  { -- | The number of seconds a response should be cached for. The default is 5
    -- minutes (300 seconds). The Lambda function can override this by
    -- returning a @ttlOverride@ key in its response. A value of 0 disables
    -- caching of responses.
    LambdaAuthorizerConfig -> Maybe Natural
authorizerResultTtlInSeconds :: Prelude.Maybe Prelude.Natural,
    -- | A regular expression for validation of tokens before the Lambda function
    -- is called.
    LambdaAuthorizerConfig -> Maybe Text
identityValidationExpression :: Prelude.Maybe Prelude.Text,
    -- | The Amazon Resource Name (ARN) of the Lambda function to be called for
    -- authorization. This can be a standard Lambda ARN, a version ARN
    -- (@...\/v3@), or an alias ARN.
    --
    -- __Note__: This Lambda function must have the following resource-based
    -- policy assigned to it. When configuring Lambda authorizers in the
    -- console, this is done for you. To use the Command Line Interface (CLI),
    -- run the following:
    --
    -- @aws lambda add-permission --function-name \"arn:aws:lambda:us-east-2:111122223333:function:my-function\" --statement-id \"appsync\" --principal appsync.amazonaws.com --action lambda:InvokeFunction@
    LambdaAuthorizerConfig -> Text
authorizerUri :: Prelude.Text
  }
  deriving (LambdaAuthorizerConfig -> LambdaAuthorizerConfig -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LambdaAuthorizerConfig -> LambdaAuthorizerConfig -> Bool
$c/= :: LambdaAuthorizerConfig -> LambdaAuthorizerConfig -> Bool
== :: LambdaAuthorizerConfig -> LambdaAuthorizerConfig -> Bool
$c== :: LambdaAuthorizerConfig -> LambdaAuthorizerConfig -> Bool
Prelude.Eq, ReadPrec [LambdaAuthorizerConfig]
ReadPrec LambdaAuthorizerConfig
Int -> ReadS LambdaAuthorizerConfig
ReadS [LambdaAuthorizerConfig]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [LambdaAuthorizerConfig]
$creadListPrec :: ReadPrec [LambdaAuthorizerConfig]
readPrec :: ReadPrec LambdaAuthorizerConfig
$creadPrec :: ReadPrec LambdaAuthorizerConfig
readList :: ReadS [LambdaAuthorizerConfig]
$creadList :: ReadS [LambdaAuthorizerConfig]
readsPrec :: Int -> ReadS LambdaAuthorizerConfig
$creadsPrec :: Int -> ReadS LambdaAuthorizerConfig
Prelude.Read, Int -> LambdaAuthorizerConfig -> ShowS
[LambdaAuthorizerConfig] -> ShowS
LambdaAuthorizerConfig -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LambdaAuthorizerConfig] -> ShowS
$cshowList :: [LambdaAuthorizerConfig] -> ShowS
show :: LambdaAuthorizerConfig -> String
$cshow :: LambdaAuthorizerConfig -> String
showsPrec :: Int -> LambdaAuthorizerConfig -> ShowS
$cshowsPrec :: Int -> LambdaAuthorizerConfig -> ShowS
Prelude.Show, forall x. Rep LambdaAuthorizerConfig x -> LambdaAuthorizerConfig
forall x. LambdaAuthorizerConfig -> Rep LambdaAuthorizerConfig x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep LambdaAuthorizerConfig x -> LambdaAuthorizerConfig
$cfrom :: forall x. LambdaAuthorizerConfig -> Rep LambdaAuthorizerConfig x
Prelude.Generic)

-- |
-- Create a value of 'LambdaAuthorizerConfig' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'authorizerResultTtlInSeconds', 'lambdaAuthorizerConfig_authorizerResultTtlInSeconds' - The number of seconds a response should be cached for. The default is 5
-- minutes (300 seconds). The Lambda function can override this by
-- returning a @ttlOverride@ key in its response. A value of 0 disables
-- caching of responses.
--
-- 'identityValidationExpression', 'lambdaAuthorizerConfig_identityValidationExpression' - A regular expression for validation of tokens before the Lambda function
-- is called.
--
-- 'authorizerUri', 'lambdaAuthorizerConfig_authorizerUri' - The Amazon Resource Name (ARN) of the Lambda function to be called for
-- authorization. This can be a standard Lambda ARN, a version ARN
-- (@...\/v3@), or an alias ARN.
--
-- __Note__: This Lambda function must have the following resource-based
-- policy assigned to it. When configuring Lambda authorizers in the
-- console, this is done for you. To use the Command Line Interface (CLI),
-- run the following:
--
-- @aws lambda add-permission --function-name \"arn:aws:lambda:us-east-2:111122223333:function:my-function\" --statement-id \"appsync\" --principal appsync.amazonaws.com --action lambda:InvokeFunction@
newLambdaAuthorizerConfig ::
  -- | 'authorizerUri'
  Prelude.Text ->
  LambdaAuthorizerConfig
newLambdaAuthorizerConfig :: Text -> LambdaAuthorizerConfig
newLambdaAuthorizerConfig Text
pAuthorizerUri_ =
  LambdaAuthorizerConfig'
    { $sel:authorizerResultTtlInSeconds:LambdaAuthorizerConfig' :: Maybe Natural
authorizerResultTtlInSeconds =
        forall a. Maybe a
Prelude.Nothing,
      $sel:identityValidationExpression:LambdaAuthorizerConfig' :: Maybe Text
identityValidationExpression = forall a. Maybe a
Prelude.Nothing,
      $sel:authorizerUri:LambdaAuthorizerConfig' :: Text
authorizerUri = Text
pAuthorizerUri_
    }

-- | The number of seconds a response should be cached for. The default is 5
-- minutes (300 seconds). The Lambda function can override this by
-- returning a @ttlOverride@ key in its response. A value of 0 disables
-- caching of responses.
lambdaAuthorizerConfig_authorizerResultTtlInSeconds :: Lens.Lens' LambdaAuthorizerConfig (Prelude.Maybe Prelude.Natural)
lambdaAuthorizerConfig_authorizerResultTtlInSeconds :: Lens' LambdaAuthorizerConfig (Maybe Natural)
lambdaAuthorizerConfig_authorizerResultTtlInSeconds = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\LambdaAuthorizerConfig' {Maybe Natural
authorizerResultTtlInSeconds :: Maybe Natural
$sel:authorizerResultTtlInSeconds:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Maybe Natural
authorizerResultTtlInSeconds} -> Maybe Natural
authorizerResultTtlInSeconds) (\s :: LambdaAuthorizerConfig
s@LambdaAuthorizerConfig' {} Maybe Natural
a -> LambdaAuthorizerConfig
s {$sel:authorizerResultTtlInSeconds:LambdaAuthorizerConfig' :: Maybe Natural
authorizerResultTtlInSeconds = Maybe Natural
a} :: LambdaAuthorizerConfig)

-- | A regular expression for validation of tokens before the Lambda function
-- is called.
lambdaAuthorizerConfig_identityValidationExpression :: Lens.Lens' LambdaAuthorizerConfig (Prelude.Maybe Prelude.Text)
lambdaAuthorizerConfig_identityValidationExpression :: Lens' LambdaAuthorizerConfig (Maybe Text)
lambdaAuthorizerConfig_identityValidationExpression = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\LambdaAuthorizerConfig' {Maybe Text
identityValidationExpression :: Maybe Text
$sel:identityValidationExpression:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Maybe Text
identityValidationExpression} -> Maybe Text
identityValidationExpression) (\s :: LambdaAuthorizerConfig
s@LambdaAuthorizerConfig' {} Maybe Text
a -> LambdaAuthorizerConfig
s {$sel:identityValidationExpression:LambdaAuthorizerConfig' :: Maybe Text
identityValidationExpression = Maybe Text
a} :: LambdaAuthorizerConfig)

-- | The Amazon Resource Name (ARN) of the Lambda function to be called for
-- authorization. This can be a standard Lambda ARN, a version ARN
-- (@...\/v3@), or an alias ARN.
--
-- __Note__: This Lambda function must have the following resource-based
-- policy assigned to it. When configuring Lambda authorizers in the
-- console, this is done for you. To use the Command Line Interface (CLI),
-- run the following:
--
-- @aws lambda add-permission --function-name \"arn:aws:lambda:us-east-2:111122223333:function:my-function\" --statement-id \"appsync\" --principal appsync.amazonaws.com --action lambda:InvokeFunction@
lambdaAuthorizerConfig_authorizerUri :: Lens.Lens' LambdaAuthorizerConfig Prelude.Text
lambdaAuthorizerConfig_authorizerUri :: Lens' LambdaAuthorizerConfig Text
lambdaAuthorizerConfig_authorizerUri = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\LambdaAuthorizerConfig' {Text
authorizerUri :: Text
$sel:authorizerUri:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Text
authorizerUri} -> Text
authorizerUri) (\s :: LambdaAuthorizerConfig
s@LambdaAuthorizerConfig' {} Text
a -> LambdaAuthorizerConfig
s {$sel:authorizerUri:LambdaAuthorizerConfig' :: Text
authorizerUri = Text
a} :: LambdaAuthorizerConfig)

instance Data.FromJSON LambdaAuthorizerConfig where
  parseJSON :: Value -> Parser LambdaAuthorizerConfig
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"LambdaAuthorizerConfig"
      ( \Object
x ->
          Maybe Natural -> Maybe Text -> Text -> LambdaAuthorizerConfig
LambdaAuthorizerConfig'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"authorizerResultTtlInSeconds")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"identityValidationExpression")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"authorizerUri")
      )

instance Prelude.Hashable LambdaAuthorizerConfig where
  hashWithSalt :: Int -> LambdaAuthorizerConfig -> Int
hashWithSalt Int
_salt LambdaAuthorizerConfig' {Maybe Natural
Maybe Text
Text
authorizerUri :: Text
identityValidationExpression :: Maybe Text
authorizerResultTtlInSeconds :: Maybe Natural
$sel:authorizerUri:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Text
$sel:identityValidationExpression:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Maybe Text
$sel:authorizerResultTtlInSeconds:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Maybe Natural
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
authorizerResultTtlInSeconds
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
identityValidationExpression
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
authorizerUri

instance Prelude.NFData LambdaAuthorizerConfig where
  rnf :: LambdaAuthorizerConfig -> ()
rnf LambdaAuthorizerConfig' {Maybe Natural
Maybe Text
Text
authorizerUri :: Text
identityValidationExpression :: Maybe Text
authorizerResultTtlInSeconds :: Maybe Natural
$sel:authorizerUri:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Text
$sel:identityValidationExpression:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Maybe Text
$sel:authorizerResultTtlInSeconds:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Maybe Natural
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
authorizerResultTtlInSeconds
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
identityValidationExpression
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
authorizerUri

instance Data.ToJSON LambdaAuthorizerConfig where
  toJSON :: LambdaAuthorizerConfig -> Value
toJSON LambdaAuthorizerConfig' {Maybe Natural
Maybe Text
Text
authorizerUri :: Text
identityValidationExpression :: Maybe Text
authorizerResultTtlInSeconds :: Maybe Natural
$sel:authorizerUri:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Text
$sel:identityValidationExpression:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Maybe Text
$sel:authorizerResultTtlInSeconds:LambdaAuthorizerConfig' :: LambdaAuthorizerConfig -> Maybe Natural
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"authorizerResultTtlInSeconds" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
authorizerResultTtlInSeconds,
            (Key
"identityValidationExpression" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
identityValidationExpression,
            forall a. a -> Maybe a
Prelude.Just
              (Key
"authorizerUri" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
authorizerUri)
          ]
      )