/
add.go
216 lines (191 loc) · 5.35 KB
/
add.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package style
import (
"fmt"
"io"
"log"
"mime/multipart"
"os"
"strconv"
"strings"
"time"
"github.com/form3tech-oss/jwt-go"
"github.com/gofiber/fiber/v2"
"github.com/vednoc/go-usercss-parser"
jwtware "userstyles.world/handlers/jwt"
"userstyles.world/models"
"userstyles.world/modules/config"
"userstyles.world/modules/database"
"userstyles.world/modules/images"
"userstyles.world/search"
"userstyles.world/utils"
)
func CreateGet(c *fiber.Ctx) error {
u, _ := jwtware.User(c)
return c.Render("style/create", fiber.Map{
"Title": "Add userstyle",
"User": u,
"Method": "add",
})
}
func CreatePost(c *fiber.Ctx) error {
u, _ := jwtware.User(c)
secureToken, OAuthID := c.Query("token"), c.Query("oauthID")
// Check if userstyle name is empty.
if strings.TrimSpace(c.FormValue("name")) == "" {
return c.Render("err", fiber.Map{
"Title": "Style name can't be empty",
"User": u,
})
}
s := &models.Style{
Name: c.FormValue("name"),
Description: c.FormValue("description"),
Notes: c.FormValue("notes"),
Homepage: c.FormValue("homepage"),
Preview: c.FormValue("previewUrl"),
Code: c.FormValue("code"),
License: strings.TrimSpace(c.FormValue("license", "No License")),
Category: strings.TrimSpace(c.FormValue("category", "unset")),
UserID: u.ID,
}
code := usercss.ParseFromString(c.FormValue("code"))
if errs := usercss.BasicMetadataValidation(code); errs != nil {
arguments := fiber.Map{
"Title": "Add userstyle",
"User": u,
"Styles": s,
"Method": "add",
"Errors": errs,
}
if OAuthID != "" {
arguments["Method"] = "add_api"
arguments["OAuthID"] = OAuthID
arguments["SecureToken"] = secureToken
}
return c.Render("style/create", arguments)
}
// Prevent adding multiples of the same style.
err := models.CheckDuplicateStyle(s)
if err != nil {
return c.Render("err", fiber.Map{
"Title": err,
"User": u,
})
}
var image multipart.File
if s.Preview == "" {
if ff, _ := c.FormFile("preview"); ff != nil {
image, err = ff.Open()
if err != nil {
log.Println("Opening image , err:", err)
return c.Render("err", fiber.Map{
"Title": "Internal server error.",
"User": u,
})
}
}
}
s, err = models.CreateStyle(s)
if err != nil {
log.Println("Style creation failed, err:", err)
return c.Render("err", fiber.Map{
"Title": "Internal server error.",
"User": u,
})
}
styleID := strconv.FormatUint(uint64(s.ID), 10)
if image != nil {
data, _ := io.ReadAll(image)
err = os.WriteFile(images.CacheFolder+styleID+".original", data, 0o600)
if err != nil {
log.Println("Style creation failed, err:", err)
return c.Render("err", fiber.Map{
"Title": "Internal server error.",
"User": u,
})
}
if s.Preview == "" {
s.Preview = "https://userstyles.world/api/preview/" + styleID + ".jpeg"
database.Conn.
Model(new(models.Style)).
Where("id", styleID).
Updates(s)
}
}
if err = search.IndexStyle(s.ID); err != nil {
log.Printf("Re-indexing style %d failed, err: %s", s.ID, err.Error())
}
if OAuthID != "" {
return handleAPIStyle(c, secureToken, OAuthID, styleID, s)
}
return c.Redirect(fmt.Sprintf("/style/%d", int(s.ID)), fiber.StatusSeeOther)
}
func handleAPIStyle(c *fiber.Ctx, secureToken, oauthID, styleID string, style *models.Style) error {
u, _ := jwtware.User(c)
OAuth, err := models.GetOAuthByID(oauthID)
if err != nil || OAuth.ID == 0 {
return c.Status(400).
JSON(fiber.Map{
"data": "Incorrect oauthID specified",
})
}
unsealedText, err := utils.DecodePreparedText(secureToken, utils.AEAD_OAUTHP, config.ScrambleConfig)
if err != nil {
log.Println("Error: Couldn't unseal JWT Token:", err.Error())
return c.Status(500).
JSON(fiber.Map{
"data": "JWT Token error, please notify the admins.",
})
}
token, err := jwt.Parse(unsealedText, utils.OAuthPJwtKeyFunction)
if err != nil || !token.Valid {
log.Println("Error: Couldn't unseal JWT Token:", err.Error())
return c.Status(500).
JSON(fiber.Map{
"data": "JWT Token error, please notify the admins.",
})
}
claims := token.Claims.(jwt.MapClaims)
userID, ok := claims["userID"].(float64)
if !ok || userID != float64(u.ID) {
log.Println("WARNING!: Invalid userID")
return c.Status(500).
JSON(fiber.Map{
"data": "JWT Token error, please notify the admins.",
})
}
state, ok := claims["state"].(string)
if !ok {
log.Println("WARNING!: Invalid state")
return c.Status(500).
JSON(fiber.Map{
"data": "JWT Token error, please notify the admins.",
})
}
if style.UserID != u.ID {
log.Println("WARNING!: Invalid style's user ID")
return c.Status(500).
JSON(fiber.Map{
"data": "JWT Token error, please notify the admins.",
})
}
jwt, err := utils.NewJWTToken().
SetClaim("state", state).
SetClaim("userID", u.ID).
SetClaim("styleID", style.ID).
SetExpiration(time.Now().Add(time.Minute * 10)).
GetSignedString(utils.OAuthPSigningKey)
if err != nil {
log.Println("Error: Couldn't create JWT Token:", err.Error())
return c.Status(500).
JSON(fiber.Map{
"data": "JWT Token error, please notify the admins.",
})
}
returnCode := "?code=" + utils.PrepareText(jwt, utils.AEAD_OAUTHP, config.ScrambleConfig)
returnCode += "&style_id=" + styleID
if state != "" {
returnCode += "&state=" + state
}
return c.Redirect(OAuth.RedirectURI + "/" + returnCode)
}