{-# 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.CognitoIdentity.Types.RoleMapping
-- 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.CognitoIdentity.Types.RoleMapping where

import Amazonka.CognitoIdentity.Types.AmbiguousRoleResolutionType
import Amazonka.CognitoIdentity.Types.RoleMappingType
import Amazonka.CognitoIdentity.Types.RulesConfigurationType
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 role mapping.
--
-- /See:/ 'newRoleMapping' smart constructor.
data RoleMapping = RoleMapping'
  { -- | If you specify Token or Rules as the @Type@, @AmbiguousRoleResolution@
    -- is required.
    --
    -- Specifies the action to be taken if either no rules match the claim
    -- value for the @Rules@ type, or there is no @cognito:preferred_role@
    -- claim and there are multiple @cognito:roles@ matches for the @Token@
    -- type.
    RoleMapping -> Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution :: Prelude.Maybe AmbiguousRoleResolutionType,
    -- | The rules to be used for mapping users to roles.
    --
    -- If you specify Rules as the role mapping type, @RulesConfiguration@ is
    -- required.
    RoleMapping -> Maybe RulesConfigurationType
rulesConfiguration :: Prelude.Maybe RulesConfigurationType,
    -- | The role mapping type. Token will use @cognito:roles@ and
    -- @cognito:preferred_role@ claims from the Cognito identity provider token
    -- to map groups to roles. Rules will attempt to match claims from the
    -- token to map to a role.
    RoleMapping -> RoleMappingType
type' :: RoleMappingType
  }
  deriving (RoleMapping -> RoleMapping -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RoleMapping -> RoleMapping -> Bool
$c/= :: RoleMapping -> RoleMapping -> Bool
== :: RoleMapping -> RoleMapping -> Bool
$c== :: RoleMapping -> RoleMapping -> Bool
Prelude.Eq, ReadPrec [RoleMapping]
ReadPrec RoleMapping
Int -> ReadS RoleMapping
ReadS [RoleMapping]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RoleMapping]
$creadListPrec :: ReadPrec [RoleMapping]
readPrec :: ReadPrec RoleMapping
$creadPrec :: ReadPrec RoleMapping
readList :: ReadS [RoleMapping]
$creadList :: ReadS [RoleMapping]
readsPrec :: Int -> ReadS RoleMapping
$creadsPrec :: Int -> ReadS RoleMapping
Prelude.Read, Int -> RoleMapping -> ShowS
[RoleMapping] -> ShowS
RoleMapping -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RoleMapping] -> ShowS
$cshowList :: [RoleMapping] -> ShowS
show :: RoleMapping -> String
$cshow :: RoleMapping -> String
showsPrec :: Int -> RoleMapping -> ShowS
$cshowsPrec :: Int -> RoleMapping -> ShowS
Prelude.Show, forall x. Rep RoleMapping x -> RoleMapping
forall x. RoleMapping -> Rep RoleMapping x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RoleMapping x -> RoleMapping
$cfrom :: forall x. RoleMapping -> Rep RoleMapping x
Prelude.Generic)

-- |
-- Create a value of 'RoleMapping' 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:
--
-- 'ambiguousRoleResolution', 'roleMapping_ambiguousRoleResolution' - If you specify Token or Rules as the @Type@, @AmbiguousRoleResolution@
-- is required.
--
-- Specifies the action to be taken if either no rules match the claim
-- value for the @Rules@ type, or there is no @cognito:preferred_role@
-- claim and there are multiple @cognito:roles@ matches for the @Token@
-- type.
--
-- 'rulesConfiguration', 'roleMapping_rulesConfiguration' - The rules to be used for mapping users to roles.
--
-- If you specify Rules as the role mapping type, @RulesConfiguration@ is
-- required.
--
-- 'type'', 'roleMapping_type' - The role mapping type. Token will use @cognito:roles@ and
-- @cognito:preferred_role@ claims from the Cognito identity provider token
-- to map groups to roles. Rules will attempt to match claims from the
-- token to map to a role.
newRoleMapping ::
  -- | 'type''
  RoleMappingType ->
  RoleMapping
newRoleMapping :: RoleMappingType -> RoleMapping
newRoleMapping RoleMappingType
pType_ =
  RoleMapping'
    { $sel:ambiguousRoleResolution:RoleMapping' :: Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution =
        forall a. Maybe a
Prelude.Nothing,
      $sel:rulesConfiguration:RoleMapping' :: Maybe RulesConfigurationType
rulesConfiguration = forall a. Maybe a
Prelude.Nothing,
      $sel:type':RoleMapping' :: RoleMappingType
type' = RoleMappingType
pType_
    }

-- | If you specify Token or Rules as the @Type@, @AmbiguousRoleResolution@
-- is required.
--
-- Specifies the action to be taken if either no rules match the claim
-- value for the @Rules@ type, or there is no @cognito:preferred_role@
-- claim and there are multiple @cognito:roles@ matches for the @Token@
-- type.
roleMapping_ambiguousRoleResolution :: Lens.Lens' RoleMapping (Prelude.Maybe AmbiguousRoleResolutionType)
roleMapping_ambiguousRoleResolution :: Lens' RoleMapping (Maybe AmbiguousRoleResolutionType)
roleMapping_ambiguousRoleResolution = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RoleMapping' {Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution :: Maybe AmbiguousRoleResolutionType
$sel:ambiguousRoleResolution:RoleMapping' :: RoleMapping -> Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution} -> Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution) (\s :: RoleMapping
s@RoleMapping' {} Maybe AmbiguousRoleResolutionType
a -> RoleMapping
s {$sel:ambiguousRoleResolution:RoleMapping' :: Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution = Maybe AmbiguousRoleResolutionType
a} :: RoleMapping)

-- | The rules to be used for mapping users to roles.
--
-- If you specify Rules as the role mapping type, @RulesConfiguration@ is
-- required.
roleMapping_rulesConfiguration :: Lens.Lens' RoleMapping (Prelude.Maybe RulesConfigurationType)
roleMapping_rulesConfiguration :: Lens' RoleMapping (Maybe RulesConfigurationType)
roleMapping_rulesConfiguration = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RoleMapping' {Maybe RulesConfigurationType
rulesConfiguration :: Maybe RulesConfigurationType
$sel:rulesConfiguration:RoleMapping' :: RoleMapping -> Maybe RulesConfigurationType
rulesConfiguration} -> Maybe RulesConfigurationType
rulesConfiguration) (\s :: RoleMapping
s@RoleMapping' {} Maybe RulesConfigurationType
a -> RoleMapping
s {$sel:rulesConfiguration:RoleMapping' :: Maybe RulesConfigurationType
rulesConfiguration = Maybe RulesConfigurationType
a} :: RoleMapping)

-- | The role mapping type. Token will use @cognito:roles@ and
-- @cognito:preferred_role@ claims from the Cognito identity provider token
-- to map groups to roles. Rules will attempt to match claims from the
-- token to map to a role.
roleMapping_type :: Lens.Lens' RoleMapping RoleMappingType
roleMapping_type :: Lens' RoleMapping RoleMappingType
roleMapping_type = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RoleMapping' {RoleMappingType
type' :: RoleMappingType
$sel:type':RoleMapping' :: RoleMapping -> RoleMappingType
type'} -> RoleMappingType
type') (\s :: RoleMapping
s@RoleMapping' {} RoleMappingType
a -> RoleMapping
s {$sel:type':RoleMapping' :: RoleMappingType
type' = RoleMappingType
a} :: RoleMapping)

instance Data.FromJSON RoleMapping where
  parseJSON :: Value -> Parser RoleMapping
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"RoleMapping"
      ( \Object
x ->
          Maybe AmbiguousRoleResolutionType
-> Maybe RulesConfigurationType -> RoleMappingType -> RoleMapping
RoleMapping'
            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
"AmbiguousRoleResolution")
            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
"RulesConfiguration")
            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
"Type")
      )

instance Prelude.Hashable RoleMapping where
  hashWithSalt :: Int -> RoleMapping -> Int
hashWithSalt Int
_salt RoleMapping' {Maybe AmbiguousRoleResolutionType
Maybe RulesConfigurationType
RoleMappingType
type' :: RoleMappingType
rulesConfiguration :: Maybe RulesConfigurationType
ambiguousRoleResolution :: Maybe AmbiguousRoleResolutionType
$sel:type':RoleMapping' :: RoleMapping -> RoleMappingType
$sel:rulesConfiguration:RoleMapping' :: RoleMapping -> Maybe RulesConfigurationType
$sel:ambiguousRoleResolution:RoleMapping' :: RoleMapping -> Maybe AmbiguousRoleResolutionType
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe RulesConfigurationType
rulesConfiguration
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` RoleMappingType
type'

instance Prelude.NFData RoleMapping where
  rnf :: RoleMapping -> ()
rnf RoleMapping' {Maybe AmbiguousRoleResolutionType
Maybe RulesConfigurationType
RoleMappingType
type' :: RoleMappingType
rulesConfiguration :: Maybe RulesConfigurationType
ambiguousRoleResolution :: Maybe AmbiguousRoleResolutionType
$sel:type':RoleMapping' :: RoleMapping -> RoleMappingType
$sel:rulesConfiguration:RoleMapping' :: RoleMapping -> Maybe RulesConfigurationType
$sel:ambiguousRoleResolution:RoleMapping' :: RoleMapping -> Maybe AmbiguousRoleResolutionType
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe RulesConfigurationType
rulesConfiguration
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf RoleMappingType
type'

instance Data.ToJSON RoleMapping where
  toJSON :: RoleMapping -> Value
toJSON RoleMapping' {Maybe AmbiguousRoleResolutionType
Maybe RulesConfigurationType
RoleMappingType
type' :: RoleMappingType
rulesConfiguration :: Maybe RulesConfigurationType
ambiguousRoleResolution :: Maybe AmbiguousRoleResolutionType
$sel:type':RoleMapping' :: RoleMapping -> RoleMappingType
$sel:rulesConfiguration:RoleMapping' :: RoleMapping -> Maybe RulesConfigurationType
$sel:ambiguousRoleResolution:RoleMapping' :: RoleMapping -> Maybe AmbiguousRoleResolutionType
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"AmbiguousRoleResolution" 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 AmbiguousRoleResolutionType
ambiguousRoleResolution,
            (Key
"RulesConfiguration" 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 RulesConfigurationType
rulesConfiguration,
            forall a. a -> Maybe a
Prelude.Just (Key
"Type" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= RoleMappingType
type')
          ]
      )