{-# 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-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- 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.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | A role mapping.
--
-- /See:/ 'newRoleMapping' smart constructor.
data RoleMapping = RoleMapping'
  { -- | 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,
    -- | 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 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
(RoleMapping -> RoleMapping -> Bool)
-> (RoleMapping -> RoleMapping -> Bool) -> Eq RoleMapping
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]
(Int -> ReadS RoleMapping)
-> ReadS [RoleMapping]
-> ReadPrec RoleMapping
-> ReadPrec [RoleMapping]
-> Read 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
(Int -> RoleMapping -> ShowS)
-> (RoleMapping -> String)
-> ([RoleMapping] -> ShowS)
-> Show RoleMapping
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. RoleMapping -> Rep RoleMapping x)
-> (forall x. Rep RoleMapping x -> RoleMapping)
-> Generic RoleMapping
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:
--
-- '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.
--
-- '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.
--
-- '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' :: Maybe RulesConfigurationType
-> Maybe AmbiguousRoleResolutionType
-> RoleMappingType
-> RoleMapping
RoleMapping'
    { $sel:rulesConfiguration:RoleMapping' :: Maybe RulesConfigurationType
rulesConfiguration = Maybe RulesConfigurationType
forall a. Maybe a
Prelude.Nothing,
      $sel:ambiguousRoleResolution:RoleMapping' :: Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution = Maybe AmbiguousRoleResolutionType
forall a. Maybe a
Prelude.Nothing,
      $sel:type':RoleMapping' :: RoleMappingType
type' = RoleMappingType
pType_
    }

-- | 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 :: (Maybe RulesConfigurationType -> f (Maybe RulesConfigurationType))
-> RoleMapping -> f RoleMapping
roleMapping_rulesConfiguration = (RoleMapping -> Maybe RulesConfigurationType)
-> (RoleMapping -> Maybe RulesConfigurationType -> RoleMapping)
-> Lens
     RoleMapping
     RoleMapping
     (Maybe RulesConfigurationType)
     (Maybe RulesConfigurationType)
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)

-- | 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 :: (Maybe AmbiguousRoleResolutionType
 -> f (Maybe AmbiguousRoleResolutionType))
-> RoleMapping -> f RoleMapping
roleMapping_ambiguousRoleResolution = (RoleMapping -> Maybe AmbiguousRoleResolutionType)
-> (RoleMapping
    -> Maybe AmbiguousRoleResolutionType -> RoleMapping)
-> Lens
     RoleMapping
     RoleMapping
     (Maybe AmbiguousRoleResolutionType)
     (Maybe AmbiguousRoleResolutionType)
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 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 :: (RoleMappingType -> f RoleMappingType)
-> RoleMapping -> f RoleMapping
roleMapping_type = (RoleMapping -> RoleMappingType)
-> (RoleMapping -> RoleMappingType -> RoleMapping)
-> Lens RoleMapping RoleMapping RoleMappingType RoleMappingType
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 Core.FromJSON RoleMapping where
  parseJSON :: Value -> Parser RoleMapping
parseJSON =
    String
-> (Object -> Parser RoleMapping) -> Value -> Parser RoleMapping
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"RoleMapping"
      ( \Object
x ->
          Maybe RulesConfigurationType
-> Maybe AmbiguousRoleResolutionType
-> RoleMappingType
-> RoleMapping
RoleMapping'
            (Maybe RulesConfigurationType
 -> Maybe AmbiguousRoleResolutionType
 -> RoleMappingType
 -> RoleMapping)
-> Parser (Maybe RulesConfigurationType)
-> Parser
     (Maybe AmbiguousRoleResolutionType
      -> RoleMappingType -> RoleMapping)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe RulesConfigurationType)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"RulesConfiguration")
            Parser
  (Maybe AmbiguousRoleResolutionType
   -> RoleMappingType -> RoleMapping)
-> Parser (Maybe AmbiguousRoleResolutionType)
-> Parser (RoleMappingType -> RoleMapping)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe AmbiguousRoleResolutionType)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"AmbiguousRoleResolution")
            Parser (RoleMappingType -> RoleMapping)
-> Parser RoleMappingType -> Parser RoleMapping
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser RoleMappingType
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"Type")
      )

instance Prelude.Hashable RoleMapping

instance Prelude.NFData RoleMapping

instance Core.ToJSON RoleMapping where
  toJSON :: RoleMapping -> Value
toJSON RoleMapping' {Maybe AmbiguousRoleResolutionType
Maybe RulesConfigurationType
RoleMappingType
type' :: RoleMappingType
ambiguousRoleResolution :: Maybe AmbiguousRoleResolutionType
rulesConfiguration :: Maybe RulesConfigurationType
$sel:type':RoleMapping' :: RoleMapping -> RoleMappingType
$sel:ambiguousRoleResolution:RoleMapping' :: RoleMapping -> Maybe AmbiguousRoleResolutionType
$sel:rulesConfiguration:RoleMapping' :: RoleMapping -> Maybe RulesConfigurationType
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"RulesConfiguration" Text -> RulesConfigurationType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (RulesConfigurationType -> Pair)
-> Maybe RulesConfigurationType -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe RulesConfigurationType
rulesConfiguration,
            (Text
"AmbiguousRoleResolution" Text -> AmbiguousRoleResolutionType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=)
              (AmbiguousRoleResolutionType -> Pair)
-> Maybe AmbiguousRoleResolutionType -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe AmbiguousRoleResolutionType
ambiguousRoleResolution,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"Type" Text -> RoleMappingType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= RoleMappingType
type')
          ]
      )