-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
177 lines (140 loc) · 3.88 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/golang-jwt/jwt/v5"
"os"
"strings"
"time"
"github.com/roman-koshchei/email-switch/emails"
"github.com/roman-koshchei/email-switch/types"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
)
var RootApiKey string
var Providers []emails.EmailSender
var QStashCurrentSigningKey string
var QStashNextSigningKey string
var QStash bool = false
func init() {
envFromFile("./.env")
RootApiKey = os.Getenv("ROOT_API_KEY")
var providersData []byte
providersValue := os.Getenv("PROVIDERS_VALUE")
if len(providersValue) > 0 {
providersData = []byte(providersValue)
} else {
providersFile := os.Getenv("PROVIDERS_FILE")
if len(providersFile) == 0 {
providersFile = "./providers.json"
}
data, err := os.ReadFile(providersFile)
if err != nil {
fmt.Println("Providers file can't be accessed or doesn't exist")
panic(err)
}
providersData = data
}
Providers = emails.Parse(providersData)
qstashEnv := os.Getenv("QSTASH")
if "true" == qstashEnv || "1" == qstashEnv {
QStash = true
QStashCurrentSigningKey = os.Getenv("QSTASH_CURRENT_SIGNING_KEY")
QStashNextSigningKey = os.Getenv("QSTASH_NEXT_SIGNING_KEY")
}
}
func main() {
sender := emails.NewEmailSwitch(Providers)
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Email Switch")
})
api := app.Group("/api", Authorization)
api.Post("/emails", func(c *fiber.Ctx) error {
input := new(types.Email)
if err := c.BodyParser(input); err != nil || !input.IsValid() {
return c.SendStatus(fiber.StatusBadRequest)
}
if sender.Send(input) {
return c.SendStatus(fiber.StatusOK)
}
return c.SendStatus(fiber.StatusInternalServerError)
})
if QStash {
app.Post("/qstash", func(c *fiber.Ctx) error {
signature := c.Get("Upstash-Signature")
body := c.BodyRaw()
legit := verifyRequestWithKey(QStashCurrentSigningKey, signature, body)
if !legit {
legit = verifyRequestWithKey(QStashNextSigningKey, signature, body)
}
if !legit {
return c.Status(fiber.StatusBadRequest).SendString("Signature isn't legit")
}
input := new(types.Email)
if err := c.BodyParser(input); err != nil || !input.IsValid() {
return c.Status(fiber.StatusBadRequest).SendString("Body has wrong shape")
}
if sender.Send(input) {
return c.SendStatus(fiber.StatusOK)
}
return c.SendStatus(fiber.StatusInternalServerError)
})
}
log.Fatal(app.Listen(":8080"))
}
func verifyRequestWithKey(key string, token string, body []byte) bool {
signingKey := []byte(key)
claims := jwt.MapClaims{}
parsedToken, err := jwt.ParseWithClaims(token, claims,
func(_ *jwt.Token) (interface{}, error) {
return signingKey, nil
},
jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}),
jwt.WithIssuer("Upstash"),
jwt.WithLeeway(time.Second),
jwt.WithIssuedAt(),
)
if err != nil || !parsedToken.Valid {
return false
}
// TODO: verify url
jwtBodyHash, ok := claims["body"].(string)
if !ok {
return false
}
jwtBodyHash = strings.TrimRight(jwtBodyHash, "=")
hashedBody := sha256.Sum256(body)
base64Hash := strings.TrimRight(base64.URLEncoding.EncodeToString(hashedBody[:]), "=")
if jwtBodyHash != base64Hash {
return false
}
return true
}
func envFromFile(path string) {
data, err := os.ReadFile(path)
if err != nil {
log.Info("Env file isn't found")
return
}
log.Info("Reading env variables from " + path)
content := string(data)
lines := strings.Split(content, "\n")
for _, line := range lines {
key, value, ok := strings.Cut(line, "=")
if ok && len(key) > 0 {
os.Setenv(key, value)
}
}
}
func Authorization(c *fiber.Ctx) error {
authHeader := c.Get("Authorization")
parts := strings.Split(authHeader, " ")
if len(parts) == 2 &&
parts[0] == "Bearer" &&
parts[1] == RootApiKey {
return c.Next()
}
return c.SendStatus(fiber.StatusUnauthorized)
}