-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzenflows-auth.go
68 lines (60 loc) · 1.64 KB
/
zenflows-auth.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bytes"
_ "embed"
"encoding/json"
"errors"
zenroom "github.com/dyne/Zenroom/bindings/golang/zenroom"
"io"
"net/http"
)
const GQL_PERSON_PUBKEY string = "query($email: String!) {personPubkey(email: $email)}"
// Input and output of sign_graphql.zen
type ZenroomData struct {
Gql string `json:"gql"`
EdDSASignature string `json:"eddsa_signature"`
EdDSAPublicKey string `json:"eddsa_public_key"`
}
type ZenroomResult struct {
Output []string `json:"output"`
}
// Fills ZenroomData with the public key requested to zenflows (from the email)
func (data *ZenroomData) requestPublicKey(email string) error {
query, err := json.Marshal(map[string]interface{}{
"query": GQL_PERSON_PUBKEY,
"variables": map[string]string{
"email": email,
},
})
resp, err := http.Post("http://fcos.interfacer.dyne.org:9000/api", "application/json", bytes.NewReader(query))
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var result map[string]map[string]string
json.Unmarshal(body, &result)
data.EdDSAPublicKey = result["data"]["personPubkey"]
return nil
}
// Used to verify the signature with `zenflows-crypto`
func (data *ZenroomData) isAuth() error {
var err error
jsonData, _ := json.Marshal(data)
// Verify the signature
result, success := zenroom.ZencodeExec(VERIFY, "", string(jsonData), "")
if !success {
return errors.New(result.Logs)
}
var zenroomResult ZenroomResult
err = json.Unmarshal([]byte(result.Output), &zenroomResult)
if err != nil {
return err
}
if zenroomResult.Output[0] != "1" {
return errors.New("Signature is not authentic")
}
return nil
}