module API.Spar where

import API.Common (defPassword)
import Data.String.Conversions.Monomorphic (fromLT)
import GHC.Stack
import qualified SAML2.WebSSO as SAML
import Testlib.Prelude

-- | https://staging-nginz-https.zinfra.io/v6/api/swagger-ui/#/default/get_scim_auth_tokens
getScimTokens :: (HasCallStack, MakesValue caller) => caller -> App Response
getScimTokens :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> App Response
getScimTokens caller
caller = do
  Request
req <- caller -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest caller
caller Service
Spar Versioned
Versioned String
"/scim/auth-tokens"
  String -> Request -> App Response
submit String
"GET" Request
req

-- | https://staging-nginz-https.zinfra.io/v5/api/swagger-ui/#/default/post_scim_auth_tokens
createScimToken :: (HasCallStack, MakesValue caller) => caller -> App Response
createScimToken :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> App Response
createScimToken caller
caller = do
  Request
req <- caller -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest caller
caller Service
Spar Versioned
Versioned String
"/scim/auth-tokens"
  String -> Request -> App Response
submit String
"POST" (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& [Pair] -> Request -> Request
addJSONObject [String
"password" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
defPassword, String
"description" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"integration test"]

-- | https://staging-nginz-https.zinfra.io/v5/api/swagger-ui/#/default/post_scim_auth_tokens
createScimTokenWithName :: (HasCallStack, MakesValue caller) => caller -> String -> App Response
createScimTokenWithName :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> App Response
createScimTokenWithName caller
caller String
name = do
  Request
req <- caller -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest caller
caller Service
Spar Versioned
Versioned String
"/scim/auth-tokens"
  String -> Request -> App Response
submit String
"POST" (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& [Pair] -> Request -> Request
addJSONObject [String
"password" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
defPassword, String
"description" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
"integration test", String
"name" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
name]

putScimTokenName :: (HasCallStack, MakesValue caller) => caller -> String -> String -> App Response
putScimTokenName :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> String -> App Response
putScimTokenName caller
caller String
token String
name = do
  Request
req <- caller -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest caller
caller Service
Spar Versioned
Versioned (String -> App Request) -> String -> App Request
forall a b. (a -> b) -> a -> b
$ [String] -> String
joinHttpPath [String
"scim", String
"auth-tokens", String
token]
  String -> Request -> App Response
submit String
"PUT" (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& [Pair] -> Request -> Request
addJSONObject [String
"name" String -> String -> Pair
forall a. ToJSON a => String -> a -> Pair
.= String
name]

createScimUser :: (HasCallStack, MakesValue domain, MakesValue scimUser) => domain -> String -> scimUser -> App Response
createScimUser :: forall domain scimUser.
(HasCallStack, MakesValue domain, MakesValue scimUser) =>
domain -> String -> scimUser -> App Response
createScimUser domain
domain String
token scimUser
scimUser = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest domain
domain Service
Spar Versioned
Versioned String
"/scim/v2/Users"
  Value
body <- scimUser -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make scimUser
scimUser
  String -> Request -> App Response
submit String
"POST" (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& Value -> Request -> Request
forall a. ToJSON a => a -> Request -> Request
addJSON Value
body (Request -> Request) -> (Request -> Request) -> Request -> Request
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> Request -> Request
addHeader String
"Authorization" (String
"Bearer " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
token)

findUsersByExternalId :: (HasCallStack, MakesValue domain) => domain -> String -> String -> App Response
findUsersByExternalId :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> String -> App Response
findUsersByExternalId domain
domain String
scimToken String
externalId = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest domain
domain Service
Spar Versioned
Versioned String
"/scim/v2/Users"
  String -> Request -> App Response
submit String
"GET" (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& [(String, String)] -> Request -> Request
addQueryParams [(String
"filter", String
"externalId eq \"" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
externalId String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"\"")]
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> String -> Request -> Request
addHeader String
"Authorization" (String
"Bearer " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
scimToken)
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> String -> Request -> Request
addHeader String
"Accept" String
"application/scim+json"

getScimUser :: (HasCallStack, MakesValue domain) => domain -> String -> String -> App Response
getScimUser :: forall caller.
(HasCallStack, MakesValue caller) =>
caller -> String -> String -> App Response
getScimUser domain
domain String
scimToken String
uid = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest domain
domain Service
Spar Versioned
Versioned (String -> App Request) -> String -> App Request
forall a b. (a -> b) -> a -> b
$ [String] -> String
joinHttpPath [String
"scim", String
"v2", String
"Users", String
uid]
  String -> Request -> App Response
submit String
"GET" (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> String -> Request -> Request
addHeader String
"Authorization" (String
"Bearer " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
scimToken)
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> String -> Request -> Request
addHeader String
"Accept" String
"application/scim+json"

updateScimUser :: (HasCallStack, MakesValue domain, MakesValue scimUser) => domain -> String -> String -> scimUser -> App Response
updateScimUser :: forall domain scimUser.
(HasCallStack, MakesValue domain, MakesValue scimUser) =>
domain -> String -> String -> scimUser -> App Response
updateScimUser domain
domain String
scimToken String
userId scimUser
scimUser = do
  Request
req <- domain -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest domain
domain Service
Spar Versioned
Versioned (String -> App Request) -> String -> App Request
forall a b. (a -> b) -> a -> b
$ [String] -> String
joinHttpPath [String
"scim", String
"v2", String
"Users", String
userId]
  Value
body <- scimUser -> App Value
forall a. (MakesValue a, HasCallStack) => a -> App Value
make scimUser
scimUser
  String -> Request -> App Response
submit String
"PUT" (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& Value -> Request -> Request
forall a. ToJSON a => a -> Request -> Request
addJSON Value
body (Request -> Request) -> (Request -> Request) -> Request -> Request
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String -> Request -> Request
addHeader String
"Authorization" (String
"Bearer " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
scimToken)
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> String -> Request -> Request
addHeader String
"Accept" String
"application/scim+json"

createIdp :: (HasCallStack, MakesValue user) => user -> SAML.IdPMetadata -> App Response
createIdp :: forall user.
(HasCallStack, MakesValue user) =>
user -> IdPMetadata -> App Response
createIdp user
user IdPMetadata
metadata = do
  Request
req <- user -> Service -> Versioned -> String -> App Request
forall user.
(HasCallStack, MakesValue user) =>
user -> Service -> Versioned -> String -> App Request
baseRequest user
user Service
Spar Versioned
Unversioned String
"/identity-providers"
  String -> Request -> App Response
submit String
"POST" (Request -> App Response) -> Request -> App Response
forall a b. (a -> b) -> a -> b
$ Request
req
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& [(String, String)] -> Request -> Request
addQueryParams [(String
"api_version", String
"v2")]
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& ByteString -> Request -> Request
addXML (LT -> ByteString
forall a. ConvertibleStrings LT a => LT -> a
fromLT (LT -> ByteString) -> LT -> ByteString
forall a b. (a -> b) -> a -> b
$ IdPMetadata -> LT
forall a. HasXMLRoot a => a -> LT
SAML.encode IdPMetadata
metadata)
    Request -> (Request -> Request) -> Request
forall a b. a -> (a -> b) -> b
& String -> String -> Request -> Request
addHeader String
"Content-Type" String
"application/xml"