-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2026 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 Test.TOTP.Enrol where

import qualified API.Brig as Brig
import qualified Data.ByteString.Char8 as BSChar8
import qualified Network.HTTP.Types.URI as HttpUri
import qualified Network.URI as NetUri
import SetupHelpers
import Test.TOTP.Helpers (responseHeader)
import Test.TOTP.MockBodies
import Testlib.Prelude

testTotpEnrol :: (HasCallStack) => App ()
testTotpEnrol :: HasCallStack => App ()
testTotpEnrol = do
  alice <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
OwnDomain CreateUser
forall a. Default a => a
def
  bindResponse (Brig.enrolTotp alice mockEnrolBody) $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
201

    -- the one response in the set that carries a secret must not be cached
    let cacheControl :: Maybe String
cacheControl = (Char -> Char) -> String -> String
forall a b. (a -> b) -> [a] -> [b]
map Char -> Char
toLower (String -> String)
-> (ByteString -> String) -> ByteString -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
BSChar8.unpack (ByteString -> String) -> Maybe ByteString -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> Response -> Maybe ByteString
responseHeader String
"Cache-Control" Response
resp
    HasCallStack => String -> Bool -> App ()
String -> Bool -> App ()
assertBool
      (String
"expected Cache-Control: no-store, got " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Maybe String -> String
forall a. Show a => a -> String
show Maybe String
cacheControl)
      (Maybe String
cacheControl Maybe String -> Maybe String -> Bool
forall a. Eq a => a -> a -> Bool
== String -> Maybe String
forall a. a -> Maybe a
Just String
"no-store")

    App String -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (App String -> App ()) -> App String -> App ()
forall a b. (a -> b) -> a -> b
$ App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString (Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"credential_id")
    App String -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (App String -> App ()) -> App String -> App ()
forall a b. (a -> b) -> a -> b
$ App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString (Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"secret")
    App String -> App ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (App String -> App ()) -> App String -> App ()
forall a b. (a -> b) -> a -> b
$ App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString (Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"uri")
    alg <- App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString (Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"algorithm")
    digits <- asInt (resp.json %. "digits")
    period <- asInt (resp.json %. "period")
    void $ asString (resp.json %. "server_time")
    void $ asString (resp.json %. "expires_at")

    -- the mock pins these rather than drawing them at random, so that a client
    -- that hardcodes the defaults does not look broken against it
    alg `shouldMatch` "sha1"
    digits `shouldMatchInt` 6
    period `shouldMatchInt` 30

-- | secret and uri field in response match.
testTotpEnrolUriMatchesSecret :: (HasCallStack) => App ()
testTotpEnrolUriMatchesSecret :: HasCallStack => App ()
testTotpEnrolUriMatchesSecret = do
  alice <- Domain -> CreateUser -> App Value
forall domain.
(HasCallStack, MakesValue domain) =>
domain -> CreateUser -> App Value
randomUser Domain
OwnDomain CreateUser
forall a. Default a => a
def
  bindResponse (Brig.enrolTotp alice mockEnrolBody) $ \Response
resp -> do
    Response
resp.status Int -> Int -> App ()
forall a. (MakesValue a, HasCallStack) => a -> Int -> App ()
`shouldMatchInt` Int
201
    secret <- App Value -> App String
forall a. (HasCallStack, MakesValue a) => a -> App String
asString (Response
resp.json Maybe Value -> String -> App Value
forall a. (HasCallStack, MakesValue a) => a -> String -> App Value
%. String
"secret")
    uriText <- asString (resp.json %. "uri")

    uri <- case NetUri.parseURI uriText of
      Maybe URI
Nothing -> String -> App URI
forall a. HasCallStack => String -> App a
assertFailure (String
"uri does not parse as a URI: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
uriText)
      Just URI
u -> URI -> App URI
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure URI
u
    NetUri.uriScheme uri `shouldMatch` "otpauth:"
    case NetUri.uriAuthority uri of
      Just URIAuth
auth -> URIAuth -> String
NetUri.uriRegName URIAuth
auth String -> String -> App ()
forall a b.
(MakesValue a, MakesValue b, HasCallStack) =>
a -> b -> App ()
`shouldMatch` String
"totp"
      Maybe URIAuth
Nothing -> String -> App ()
forall a. HasCallStack => String -> App a
assertFailure (String
"uri has no authority: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
uriText)

    let params = ByteString -> Query
HttpUri.parseQuery (String -> ByteString
BSChar8.pack (URI -> String
NetUri.uriQuery URI
uri))
        param String
k =
          App String
-> (ByteString -> App String) -> Maybe ByteString -> App String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
            (String -> App String
forall a. HasCallStack => String -> App a
assertFailure (String
"uri has no " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
k String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" parameter: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
uriText))
            (String -> App String
forall a. a -> App a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> App String)
-> (ByteString -> String) -> ByteString -> App String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
BSChar8.unpack)
            (Maybe (Maybe ByteString) -> Maybe ByteString
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (ByteString -> Query -> Maybe (Maybe ByteString)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (String -> ByteString
BSChar8.pack String
k) Query
params))
    -- this is the assertion the whole test exists for
    param "secret" `shouldMatch` secret
    param "issuer" `shouldMatch` "Wire"
    param "algorithm" `shouldMatch` "SHA1"
    param "digits" `shouldMatch` "6"
    param "period" `shouldMatch` "30"