summaryrefslogtreecommitdiff
path: root/hosts/surtr/email/spm/provision/Spm/Provision.hs
blob: ff18baa08eba5639da63aee0fbf9149fdcbf2f93 (plain)
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
41
42
43
44
45
46
module Spm.Provision
  ( main
  ) where

import Prelude
import Options.Applicative
import Control.Monad

import qualified Data.Text.IO as Text

import qualified Data.UUID as UUID
import qualified Data.UUID.V4 as UUID

import Crypto.JOSE.JWK

import qualified Data.ByteString.Lazy.Char8 as CLBS
import qualified Data.Aeson as JSON

import Control.Lens


data Command
  = InstanceId
  | JwkSet
  deriving stock (Eq, Ord, Read, Show)

cmdInstanceId :: IO ()
cmdInstanceId = Text.putStrLn . UUID.toText =<< UUID.nextRandom

cmdJwkSet :: IO ()
cmdJwkSet = do
  k' <- genJWK (OKPGenParam Ed25519)
  kid <- UUID.nextRandom
  let k = k' & jwkKid ?~ UUID.toText kid
             & jwkUse ?~ Sig
             & jwkKeyOps ?~ [Sign, Verify]
  CLBS.putStrLn . JSON.encode . JWKSet $ pure k

opts :: Parser (IO ())
opts = subparser $
     command "instance-id" (info (pure cmdInstanceId) idm)
  <> command "jwk-set" (info (pure cmdJwkSet) idm)

  
main :: IO ()
main = join $ execParser (info opts idm)