-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Wire.API.User.Auth.Sso where

import Data.Aeson (FromJSON (..), ToJSON (..))
import Data.Aeson qualified as A
import Data.Id
import Data.OpenApi qualified as S
import Data.Schema
import Imports
import Wire.API.User.Auth

-- | A special kind of login that is only used for an internal endpoint.
data SsoLogin = SsoLogin
  { SsoLogin -> UserId
ssoUserId :: !UserId,
    SsoLogin -> Maybe CookieLabel
ssoLabel :: !(Maybe CookieLabel)
  }
  deriving (Value -> Parser [SsoLogin]
Value -> Parser SsoLogin
(Value -> Parser SsoLogin)
-> (Value -> Parser [SsoLogin]) -> FromJSON SsoLogin
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
$cparseJSON :: Value -> Parser SsoLogin
parseJSON :: Value -> Parser SsoLogin
$cparseJSONList :: Value -> Parser [SsoLogin]
parseJSONList :: Value -> Parser [SsoLogin]
FromJSON, [SsoLogin] -> Value
[SsoLogin] -> Encoding
SsoLogin -> Value
SsoLogin -> Encoding
(SsoLogin -> Value)
-> (SsoLogin -> Encoding)
-> ([SsoLogin] -> Value)
-> ([SsoLogin] -> Encoding)
-> ToJSON SsoLogin
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: SsoLogin -> Value
toJSON :: SsoLogin -> Value
$ctoEncoding :: SsoLogin -> Encoding
toEncoding :: SsoLogin -> Encoding
$ctoJSONList :: [SsoLogin] -> Value
toJSONList :: [SsoLogin] -> Value
$ctoEncodingList :: [SsoLogin] -> Encoding
toEncodingList :: [SsoLogin] -> Encoding
ToJSON, Typeable SsoLogin
Typeable SsoLogin =>
(Proxy SsoLogin -> Declare (Definitions Schema) NamedSchema)
-> ToSchema SsoLogin
Proxy SsoLogin -> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a =>
(Proxy a -> Declare (Definitions Schema) NamedSchema) -> ToSchema a
$cdeclareNamedSchema :: Proxy SsoLogin -> Declare (Definitions Schema) NamedSchema
declareNamedSchema :: Proxy SsoLogin -> Declare (Definitions Schema) NamedSchema
S.ToSchema) via Schema SsoLogin

instance ToSchema SsoLogin where
  schema :: ValueSchema NamedSwaggerDoc SsoLogin
schema =
    Text
-> SchemaP SwaggerDoc Object [Pair] SsoLogin SsoLogin
-> ValueSchema NamedSwaggerDoc SsoLogin
forall doc doc' a b.
HasObject doc doc' =>
Text
-> SchemaP doc Object [Pair] a b -> SchemaP doc' Value Value a b
object Text
"SsoLogin" (SchemaP SwaggerDoc Object [Pair] SsoLogin SsoLogin
 -> ValueSchema NamedSwaggerDoc SsoLogin)
-> SchemaP SwaggerDoc Object [Pair] SsoLogin SsoLogin
-> ValueSchema NamedSwaggerDoc SsoLogin
forall a b. (a -> b) -> a -> b
$
      UserId -> Maybe CookieLabel -> SsoLogin
SsoLogin
        (UserId -> Maybe CookieLabel -> SsoLogin)
-> SchemaP SwaggerDoc Object [Pair] SsoLogin UserId
-> SchemaP
     SwaggerDoc Object [Pair] SsoLogin (Maybe CookieLabel -> SsoLogin)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SsoLogin -> UserId
ssoUserId (SsoLogin -> UserId)
-> SchemaP SwaggerDoc Object [Pair] UserId UserId
-> SchemaP SwaggerDoc Object [Pair] SsoLogin UserId
forall (p :: * -> * -> *) a a' b.
Profunctor p =>
(a -> a') -> p a' b -> p a b
.= Text
-> SchemaP NamedSwaggerDoc Value Value UserId UserId
-> SchemaP SwaggerDoc Object [Pair] UserId UserId
forall doc' doc a b.
HasField doc' doc =>
Text
-> SchemaP doc' Value Value a b -> SchemaP doc Object [Pair] a b
field Text
"user" SchemaP NamedSwaggerDoc Value Value UserId UserId
forall a. ToSchema a => ValueSchema NamedSwaggerDoc a
schema
        SchemaP
  SwaggerDoc Object [Pair] SsoLogin (Maybe CookieLabel -> SsoLogin)
-> SchemaP SwaggerDoc Object [Pair] SsoLogin (Maybe CookieLabel)
-> SchemaP SwaggerDoc Object [Pair] SsoLogin SsoLogin
forall a b.
SchemaP SwaggerDoc Object [Pair] SsoLogin (a -> b)
-> SchemaP SwaggerDoc Object [Pair] SsoLogin a
-> SchemaP SwaggerDoc Object [Pair] SsoLogin b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SsoLogin -> Maybe CookieLabel
ssoLabel (SsoLogin -> Maybe CookieLabel)
-> SchemaP
     SwaggerDoc Object [Pair] (Maybe CookieLabel) (Maybe CookieLabel)
-> SchemaP SwaggerDoc Object [Pair] SsoLogin (Maybe CookieLabel)
forall (p :: * -> * -> *) a a' b.
Profunctor p =>
(a -> a') -> p a' b -> p a b
.= Text
-> SchemaP
     NamedSwaggerDoc Value Value (Maybe CookieLabel) CookieLabel
-> SchemaP
     SwaggerDoc Object [Pair] (Maybe CookieLabel) (Maybe CookieLabel)
forall doc doc' a b.
(HasOpt doc, HasField doc' doc) =>
Text
-> SchemaP doc' Value Value a b
-> SchemaP doc Object [Pair] a (Maybe b)
optField Text
"label" (Value
-> SchemaP NamedSwaggerDoc Value Value CookieLabel CookieLabel
-> SchemaP
     NamedSwaggerDoc Value Value (Maybe CookieLabel) CookieLabel
forall w d v a b.
w -> SchemaP d v w a b -> SchemaP d v w (Maybe a) b
maybeWithDefault Value
A.Null SchemaP NamedSwaggerDoc Value Value CookieLabel CookieLabel
forall a. ToSchema a => ValueSchema NamedSwaggerDoc a
schema)