-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
155 lines (133 loc) · 4.69 KB
/
handlers.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package views
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/fatih/structs"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/horkruxes/hkxserver/client"
"github.com/horkruxes/hkxserver/model"
"github.com/horkruxes/hkxserver/query"
"github.com/horkruxes/hkxserver/service"
)
func GetKeys(c *fiber.Ctx) error {
outputData := client.GenKeys()
return c.Render("templates/keys/root", structs.Map(outputData))
}
func PostKeys(c *fiber.Ctx) error {
outputData := client.GenKeys()
// Get form data and reinject into output data
outputData.Sig = strings.TrimSpace(c.FormValue("signature"))
outputData.Sec = strings.TrimSpace(c.FormValue("secret-key"))
outputData.Pub = strings.TrimSpace(c.FormValue("public-key"))
outputData.DisplayedName = strings.TrimSpace(c.FormValue("displayed-name"))
outputData.Content = strings.TrimSpace(c.FormValue("message"))
outputData.MessageID = strings.TrimSpace(c.FormValue("answer-to"))
outputData.Verif = true
if outputData.Sig == "" {
// Answers to the signature GENERATION form
outputData.Sig = client.SignStrings(outputData.Sec, outputData.Pub, outputData.DisplayedName, outputData.Content, outputData.MessageID)
outputData.Valid = client.VerifyFromString(outputData.Pub, outputData.Sig, outputData.DisplayedName, outputData.Content, outputData.MessageID)
outputData.Sec = ""
} else {
// Answers to the signature VERIFICATION form
outputData.Valid = client.VerifyFromString(outputData.Pub, outputData.Sig, outputData.DisplayedName, outputData.Content, outputData.MessageID)
outputData.Sig = ""
}
return c.Render("templates/keys/root", structs.Map(outputData))
}
func GetFaq(c *fiber.Ctx) error {
return c.Render("templates/faq/root", fiber.Map{})
}
func GetMainNoFront(c *fiber.Ctx) error {
return c.SendString(`The pod admin chose to only use the 'data' part of Horkruxes.
Sorry, you'll have to use another client to see the messages.`)
}
func GetMain(s service.Service) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
s.ClientConfig = parseFormsToService(c, s)
localData := GetMessagesAndMainPageInfo(s)
return c.Render("templates/main/root", structs.Map(localData))
}
}
func GetAuthor(s service.Service) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
s.ClientConfig = parseFormsToService(c, s)
pubKey := c.Params("pubKey")
fmt.Println("pods list", s.GeneralConfig.TrustedPods)
localData := GetAuthorMessagesAndMainPageInfo(s, pubKey)
return c.Render("templates/main/root", structs.Map(localData))
}
}
func GetComments(s service.Service) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
s.ClientConfig = parseFormsToService(c, s)
id := c.Params("uuid")
_, err := uuid.Parse(id)
if err != nil {
return c.Status(400).SendString("Invalid UUID" + err.Error())
}
localData := GetCommentsAndMainPageInfo(s, id)
return c.Render("templates/main/root", structs.Map(localData))
}
}
func NewMessage(s service.Service) func(*fiber.Ctx) error {
return func(c *fiber.Ctx) error {
payload := FormToBasicMessage(c)
secretKey := strings.TrimSpace(c.FormValue("secret-key"))
if secretKey != "" && payload.SignatureBase64 == "" {
fmt.Println("signing server-side")
payload.SignatureBase64 = client.SignMessage(payload, secretKey)
if payload.SignatureBase64 == "" {
return c.Status(400).SendString("can't sign message")
}
}
newMessage := model.Message{}
fmt.Println("try to post to:", payload.Pod)
// Check if can do the db operations right now or if it should transfer the payload to another API
if payload.Pod == "" {
if err := payload.VerifyConstraints(); err != nil {
fmt.Println("error:", err)
return c.Status(400).SendString(err.Error())
}
fmt.Println("new msg", payload)
var err error
newMessage, err = query.NewMessage(s, payload)
if err != nil {
fmt.Println("error:", err)
return c.Status(422).SendString(err.Error())
}
} else {
reader, err := json.Marshal(payload)
if err != nil {
fmt.Println("err:", err)
}
resp, err := http.Post("https://"+payload.Pod+"/api/message", "application/json", bytes.NewBuffer(reader))
if err != nil {
fmt.Println("err:", err)
}
if resp.StatusCode == 409 {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("err:", err)
}
fmt.Println(string(body))
err = json.Unmarshal(body, &newMessage)
if err != nil {
fmt.Println("err:", err)
}
return c.SendString("error sending the message " + string(body))
}
}
if newMessage.MessageID == "" {
return c.Redirect("/comments/" + newMessage.ID)
} else {
return c.Redirect("/comments/" + newMessage.MessageID)
}
}
}