~jonn/do-auth

ref: c5ec5c21709066bc9d788eefde6419b597c95225 do-auth/src/DoAuth/Crypto/Server.hs -rw-r--r-- 1.1 KiB
c5ec5c21Jonn Problem: can't send detached signatures around 1 year, 13 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) = object $ 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