optimize slightly

(cherry picked from commit 53d0653846c9d64ce86938bb04179a5564c62424)
This commit is contained in:
Lewis Crichton
2023-04-12 14:11:36 +01:00
parent 88d3cdc3fd
commit 589974339e
+22
View File
@@ -42,6 +42,8 @@ type DiscordUserResult struct {
Id string `json:"id"` Id string `json:"id"`
} }
var ALLOWED_USERS map[string]bool
var rdb *redis.Client var rdb *redis.Client
func hash(s string) string { func hash(s string) string {
@@ -80,6 +82,12 @@ func requireAuth(c *fiber.Ctx) error {
secret := tokenSplit[0] secret := tokenSplit[0]
userId := tokenSplit[1] userId := tokenSplit[1]
if ALLOWED_USERS != nil && c.Path() != "/v1" && c.Method() != "DELETE" && !ALLOWED_USERS[userId] {
return c.Status(403).JSON(&fiber.Map{
"error": "User is not whitelisted",
})
}
storedSecret, err := rdb.Get(c.Context(), "secrets:"+hash(os.Getenv("PEPPER_SECRETS")+userId)).Result() storedSecret, err := rdb.Get(c.Context(), "secrets:"+hash(os.Getenv("PEPPER_SECRETS")+userId)).Result()
if err == redis.Nil { if err == redis.Nil {
@@ -118,6 +126,14 @@ func main() {
slRaw, _ := strconv.ParseInt(os.Getenv("SIZE_LIMIT"), 10, 0) slRaw, _ := strconv.ParseInt(os.Getenv("SIZE_LIMIT"), 10, 0)
SIZE_LIMIT := int(slRaw) SIZE_LIMIT := int(slRaw)
auRaw := os.Getenv("ALLOWED_USERS")
if auRaw != "" {
ALLOWED_USERS = make(map[string]bool)
for _, userId := range strings.Split(auRaw, ",") {
ALLOWED_USERS[userId] = true
}
}
app := fiber.New() app := fiber.New()
rdb = redis.NewClient(&redis.Options{ rdb = redis.NewClient(&redis.Options{
Addr: REDIS_URI, Addr: REDIS_URI,
@@ -268,6 +284,12 @@ func main() {
userId := userResult.Id userId := userResult.Id
if ALLOWED_USERS != nil && !ALLOWED_USERS[userId] {
return c.Status(403).JSON(&fiber.Map{
"error": "User is not whitelisted",
})
}
secret, err := rdb.Get(c.Context(), "secrets:"+hash(PEPPER_SECRETS+userId)).Result() secret, err := rdb.Get(c.Context(), "secrets:"+hash(PEPPER_SECRETS+userId)).Result()
if err == redis.Nil { if err == redis.Nil {