~jonn/do-auth

ref: e9acaf7c2d98b8aea72680b08c70b0f86572d687 do-auth/src/DoAuth/Crypto/Server.hs -rw-r--r-- 1.1 KiB
e9acaf7cJonn Problem: Need B64 to send signed challenge back 1 year, 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

module DoAuth.Crypto.Server (mkChallenge, Challenge (..)) where

import Crypto.Nonce (generate)
import Data.Aeson
  ( FromJSON (parseJSON),
    KeyValue ((.=)),
    ToJSON (toJSON),
    object,
    withObject,
    (.:),
  )
import Data.ByteArray (ByteArray)
import Data.ByteArray.Sized (SizedByteArray, unSizedByteArray)
import Data.ByteString (ByteString (..))
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.UTF8 as Bsu
import Data.Either (fromRight)
import Debug.Trace (trace)
import GHC.Generics (Generic)
import DoAuth.Json (b64decode, b64encode)

newtype Challenge = Challenge {getChallenge :: ByteString}
  deriving (Show, Eq)

instance ToJSON Challenge where
  toJSON (Challenge x) = b64encode "DoAuth.Crypto.Server.Challenge" x

instance FromJSON Challenge where
  parseJSON = b64decode "DoAuth.Crypto.Server.Challenge" Challenge

type SmallChallenge = SizedByteArray 8 ByteString

mkChallenge :: IO Challenge
mkChallenge = Challenge <$> (unSizedByteArray <$> generate')
  where
    generate' = generate :: IO SmallChallenge