add full data erasure + reformat the entire thing cause vsc sucks
This commit is contained in:
@@ -26,40 +26,13 @@ type DiscordUserResult struct {
|
||||
Id string `json:"id"`
|
||||
}
|
||||
|
||||
var rdb *redis.Client
|
||||
|
||||
func hash(s string) string {
|
||||
return fmt.Sprintf("%x", sha1.Sum([]byte(s)))
|
||||
}
|
||||
|
||||
func main() {
|
||||
// environment
|
||||
HOST := os.Getenv("HOST")
|
||||
PORT := os.Getenv("PORT")
|
||||
REDIS_URI := os.Getenv("REDIS_URI")
|
||||
ROOT_REDIRECT := os.Getenv("ROOT_REDIRECT")
|
||||
|
||||
DISCORD_CLIENT_ID := os.Getenv("DISCORD_CLIENT_ID")
|
||||
DISCORD_CLIENT_SECRET := os.Getenv("DISCORD_CLIENT_SECRET")
|
||||
DISCORD_REDIRECT_URI := os.Getenv("DISCORD_REDIRECT_URI")
|
||||
|
||||
PEPPER_SECRETS := os.Getenv("PEPPER_SECRETS")
|
||||
PEPPER_SETTINGS := os.Getenv("PEPPER_SETTINGS")
|
||||
|
||||
slRaw, _ := strconv.ParseInt(os.Getenv("SIZE_LIMIT"), 10, 0)
|
||||
SIZE_LIMIT := int(slRaw)
|
||||
|
||||
app := fiber.New()
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: REDIS_URI,
|
||||
})
|
||||
req := reqHttp.C()
|
||||
|
||||
app.Use(cors.New(cors.Config{
|
||||
ExposeHeaders: "ETag",
|
||||
}))
|
||||
app.Use(logger.New())
|
||||
|
||||
// #region settings
|
||||
app.All("/settings", func(c *fiber.Ctx) error {
|
||||
func requireAuth(c *fiber.Ctx) error {
|
||||
authToken := c.Get("Authorization")
|
||||
|
||||
if authToken == "" {
|
||||
@@ -91,7 +64,7 @@ func main() {
|
||||
userId := tokenSplit[0]
|
||||
secret := tokenSplit[1]
|
||||
|
||||
storedSecret, err := rdb.Get(c.Context(), "secrets:" + hash(PEPPER_SECRETS + userId)).Result()
|
||||
storedSecret, err := rdb.Get(c.Context(), "secrets:"+hash(os.Getenv("PEPPER_SECRETS")+userId)).Result()
|
||||
|
||||
if err == redis.Nil {
|
||||
return c.Status(401).JSON(&fiber.Map{
|
||||
@@ -110,12 +83,43 @@ func main() {
|
||||
c.Context().SetUserValue("userId", userId)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func main() {
|
||||
// environment
|
||||
HOST := os.Getenv("HOST")
|
||||
PORT := os.Getenv("PORT")
|
||||
REDIS_URI := os.Getenv("REDIS_URI")
|
||||
ROOT_REDIRECT := os.Getenv("ROOT_REDIRECT")
|
||||
|
||||
DISCORD_CLIENT_ID := os.Getenv("DISCORD_CLIENT_ID")
|
||||
DISCORD_CLIENT_SECRET := os.Getenv("DISCORD_CLIENT_SECRET")
|
||||
DISCORD_REDIRECT_URI := os.Getenv("DISCORD_REDIRECT_URI")
|
||||
|
||||
PEPPER_SECRETS := os.Getenv("PEPPER_SECRETS")
|
||||
PEPPER_SETTINGS := os.Getenv("PEPPER_SETTINGS")
|
||||
|
||||
slRaw, _ := strconv.ParseInt(os.Getenv("SIZE_LIMIT"), 10, 0)
|
||||
SIZE_LIMIT := int(slRaw)
|
||||
|
||||
app := fiber.New()
|
||||
rdb = redis.NewClient(&redis.Options{
|
||||
Addr: REDIS_URI,
|
||||
})
|
||||
req := reqHttp.C()
|
||||
|
||||
app.Use(cors.New(cors.Config{
|
||||
ExposeHeaders: "ETag",
|
||||
}))
|
||||
app.Use(logger.New())
|
||||
|
||||
// #region settings
|
||||
app.All("/settings", requireAuth)
|
||||
|
||||
app.Head("/settings", func(c *fiber.Ctx) error {
|
||||
userId := c.Context().UserValue("userId").(string)
|
||||
|
||||
written, err := rdb.HGet(c.Context(), "settings:" + hash(PEPPER_SETTINGS + userId), "written").Result()
|
||||
written, err := rdb.HGet(c.Context(), "settings:"+hash(PEPPER_SETTINGS+userId), "written").Result()
|
||||
|
||||
if err == redis.Nil {
|
||||
return c.Status(404).Send(nil)
|
||||
@@ -130,7 +134,7 @@ func main() {
|
||||
app.Get("/settings", func(c *fiber.Ctx) error {
|
||||
userId := c.Context().UserValue("userId").(string)
|
||||
|
||||
settings, err := rdb.HMGet(c.Context(), "settings:" + hash(PEPPER_SETTINGS + userId), "value", "written").Result()
|
||||
settings, err := rdb.HMGet(c.Context(), "settings:"+hash(PEPPER_SETTINGS+userId), "value", "written").Result()
|
||||
|
||||
// we shouldn't expect an error here, HMGet doesn't return one
|
||||
if err != nil {
|
||||
@@ -154,13 +158,13 @@ func main() {
|
||||
})
|
||||
|
||||
app.Put("/settings", func(c *fiber.Ctx) error {
|
||||
if (c.Get("Content-Type") != "application/octet-stream") {
|
||||
if c.Get("Content-Type") != "application/octet-stream" {
|
||||
return c.Status(415).JSON(&fiber.Map{
|
||||
"error": "Content type must be application/octet-stream",
|
||||
})
|
||||
}
|
||||
|
||||
if (len(c.Body()) > SIZE_LIMIT) {
|
||||
if len(c.Body()) > SIZE_LIMIT {
|
||||
return c.Status(413).JSON(&fiber.Map{
|
||||
"error": "Settings are too large",
|
||||
})
|
||||
@@ -170,7 +174,7 @@ func main() {
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
_, err := rdb.HSet(c.Context(), "settings:" + hash(PEPPER_SETTINGS + userId), map[string]interface{}{
|
||||
_, err := rdb.HSet(c.Context(), "settings:"+hash(PEPPER_SETTINGS+userId), map[string]interface{}{
|
||||
"value": c.Body(),
|
||||
"written": now,
|
||||
}).Result()
|
||||
@@ -187,7 +191,7 @@ func main() {
|
||||
app.Delete("/settings", func(c *fiber.Ctx) error {
|
||||
userId := c.Context().UserValue("userId").(string)
|
||||
|
||||
rdb.Del(c.Context(), "settings:" + hash(PEPPER_SETTINGS + userId))
|
||||
rdb.Del(c.Context(), "settings:"+hash(PEPPER_SETTINGS+userId))
|
||||
|
||||
return c.SendStatus(204)
|
||||
})
|
||||
@@ -248,7 +252,7 @@ func main() {
|
||||
|
||||
userId := userResult.Id
|
||||
|
||||
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 {
|
||||
key := make([]byte, 48)
|
||||
@@ -261,7 +265,7 @@ func main() {
|
||||
}
|
||||
|
||||
secret = hex.EncodeToString(key)
|
||||
rdb.Set(c.Context(), "secrets:" + hash(PEPPER_SECRETS + userId), secret, 0)
|
||||
rdb.Set(c.Context(), "secrets:"+hash(PEPPER_SECRETS+userId), secret, 0)
|
||||
}
|
||||
|
||||
return c.JSON(&fiber.Map{
|
||||
@@ -277,6 +281,17 @@ func main() {
|
||||
})
|
||||
// #endregion
|
||||
|
||||
// #region erase all
|
||||
app.Delete("/", requireAuth, func(c *fiber.Ctx) error {
|
||||
userId := c.Context().UserValue("userId").(string)
|
||||
|
||||
rdb.Del(c.Context(), "settings:"+hash(PEPPER_SETTINGS+userId))
|
||||
rdb.Del(c.Context(), "secret"+hash(PEPPER_SECRETS+userId))
|
||||
|
||||
return c.SendStatus(204)
|
||||
})
|
||||
// #endregion
|
||||
|
||||
app.Get("/", func(c *fiber.Ctx) error {
|
||||
return c.Redirect(ROOT_REDIRECT, 307)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user