feat(admin): authorize batch purchase starts
This commit is contained in:
+142
-13
@@ -2,11 +2,16 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"cmbuyer/admin/internal/auth"
|
||||
"cmbuyer/admin/internal/tasks"
|
||||
@@ -17,6 +22,7 @@ import (
|
||||
)
|
||||
|
||||
const maxFormBytes = 8 << 10
|
||||
const maxJSONBytes = 64 << 10
|
||||
|
||||
// Options 是路由层需要的安全依赖。凭据由启动配置注入,不能在路由中设置默认值。
|
||||
type Options struct {
|
||||
@@ -42,10 +48,84 @@ func NewRouter(options Options) (*gin.Engine, error) {
|
||||
router.GET("/tasks", tasksPage(options))
|
||||
router.GET("/tasks/new", newTaskPage(options))
|
||||
router.POST("/tasks", createTask(options))
|
||||
router.POST("/tasks/start-purchases", startPurchases(options))
|
||||
router.GET("/static/tasks.js", func(context *gin.Context) {
|
||||
context.Data(http.StatusOK, "application/javascript; charset=utf-8", webui.TasksScript())
|
||||
})
|
||||
|
||||
return router, nil
|
||||
}
|
||||
|
||||
func startPurchases(options Options) gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
if !options.Sessions.IsAuthenticated(context.Request) {
|
||||
context.Status(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
authenticated, csrfOK := options.Sessions.VerifyCSRF(context.Request, context.GetHeader("X-CSRF-Token"))
|
||||
if !authenticated || !csrfOK {
|
||||
context.Status(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
if !isJSONContentType(context.GetHeader("Content-Type")) {
|
||||
context.Status(http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
context.Request.Body = http.MaxBytesReader(context.Writer, context.Request.Body, maxJSONBytes)
|
||||
raw, err := io.ReadAll(context.Request.Body)
|
||||
if err != nil {
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
context.Status(http.StatusRequestEntityTooLarge)
|
||||
} else {
|
||||
context.Status(http.StatusBadRequest)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !utf8.Valid(raw) {
|
||||
context.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(raw))
|
||||
decoder.DisallowUnknownFields()
|
||||
var command tasks.StartCommand
|
||||
if err := decoder.Decode(&command); err != nil {
|
||||
context.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
var extra any
|
||||
if err := decoder.Decode(&extra); err != io.EOF {
|
||||
context.Status(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
result, err := options.Tasks.StartPurchases(context.Request.Context(), command, options.AdminUsername)
|
||||
if err != nil {
|
||||
if errors.Is(err, tasks.ErrInvalidStart) {
|
||||
context.Status(http.StatusBadRequest)
|
||||
} else if errors.Is(err, tasks.ErrStartConflict) {
|
||||
context.Status(http.StatusConflict)
|
||||
} else {
|
||||
context.Status(http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
context.JSON(http.StatusOK, result)
|
||||
}
|
||||
}
|
||||
|
||||
func isJSONContentType(value string) bool {
|
||||
mediaType, parameters, err := mime.ParseMediaType(value)
|
||||
if err != nil || mediaType != "application/json" {
|
||||
return false
|
||||
}
|
||||
for name, value := range parameters {
|
||||
if name != "charset" || !strings.EqualFold(value, "utf-8") {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func healthz(context *gin.Context) {
|
||||
context.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||||
}
|
||||
@@ -55,7 +135,7 @@ func securityHeaders() gin.HandlerFunc {
|
||||
context.Header("Cache-Control", "no-store")
|
||||
context.Header("X-Content-Type-Options", "nosniff")
|
||||
context.Header("Referrer-Policy", "no-referrer")
|
||||
context.Header("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'none'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'")
|
||||
context.Header("Content-Security-Policy", "default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'")
|
||||
context.Next()
|
||||
}
|
||||
}
|
||||
@@ -126,14 +206,23 @@ func tasksPage(options Options) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
drafts, err := options.Tasks.ListDrafts(context.Request.Context())
|
||||
filter := tasks.TaskFilter{Keyword: context.Query("keyword"), Status: context.Query("status"), CreatedFrom: context.Query("created_from"), CreatedTo: context.Query("created_to")}
|
||||
if validation := tasks.ValidateTaskFilter(filter); !validation.Valid() {
|
||||
startKey, err := tasks.NewCreateKey()
|
||||
if err != nil {
|
||||
context.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
renderTasks(context, http.StatusBadRequest, webui.TasksData{CSRFToken: csrfToken, Filter: filter, FilterErrors: validation, HasFilter: true, StartKey: startKey})
|
||||
return
|
||||
}
|
||||
data, err := taskListData(context, options, csrfToken, filter)
|
||||
if err != nil {
|
||||
context.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
data := webui.TasksData{CSRFToken: csrfToken, Drafts: drafts}
|
||||
for _, draft := range drafts {
|
||||
if draft.ID == context.Query("created") {
|
||||
for _, row := range data.Tasks {
|
||||
if row.ID == context.Query("created") {
|
||||
data.Success = true
|
||||
break
|
||||
}
|
||||
@@ -185,24 +274,32 @@ func createTask(options Options) gin.HandlerFunc {
|
||||
}
|
||||
fullPage := requestForm.Get("form_mode") == "full"
|
||||
if !validation.Valid() {
|
||||
drafts, err := options.Tasks.ListDrafts(context.Request.Context())
|
||||
if err != nil {
|
||||
context.Status(http.StatusInternalServerError)
|
||||
data, ok := createErrorData(context, options, fullPage)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
renderTasks(context, http.StatusBadRequest, webui.TasksData{CSRFToken: csrfFor(context, options), Drafts: drafts, Form: form, Errors: validation, OpenForm: !fullPage, FullPage: fullPage, FocusField: firstError(validation)})
|
||||
data.Form = form
|
||||
data.Errors = validation
|
||||
data.OpenForm = !fullPage
|
||||
data.FullPage = fullPage
|
||||
data.FocusField = firstError(validation)
|
||||
renderTasks(context, http.StatusBadRequest, data)
|
||||
return
|
||||
}
|
||||
created, err := options.Tasks.CreateDraft(context.Request.Context(), draft)
|
||||
if err != nil {
|
||||
if errors.Is(err, tasks.ErrCreateKeyConflict) {
|
||||
validation["create_key"] = "该创建请求已用于另一条任务,请重新打开表单。"
|
||||
drafts, listErr := options.Tasks.ListDrafts(context.Request.Context())
|
||||
if listErr != nil {
|
||||
context.Status(http.StatusInternalServerError)
|
||||
data, ok := createErrorData(context, options, fullPage)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
renderTasks(context, http.StatusConflict, webui.TasksData{CSRFToken: csrfFor(context, options), Drafts: drafts, Form: form, Errors: validation, OpenForm: !fullPage, FullPage: fullPage, FocusField: firstError(validation)})
|
||||
data.Form = form
|
||||
data.Errors = validation
|
||||
data.OpenForm = !fullPage
|
||||
data.FullPage = fullPage
|
||||
data.FocusField = firstError(validation)
|
||||
renderTasks(context, http.StatusConflict, data)
|
||||
return
|
||||
}
|
||||
context.Status(http.StatusInternalServerError)
|
||||
@@ -226,6 +323,38 @@ func csrfFor(context *gin.Context, options Options) string {
|
||||
csrf, _ := options.Sessions.Ensure(context.Writer, context.Request)
|
||||
return csrf
|
||||
}
|
||||
|
||||
func taskListData(context *gin.Context, options Options, csrfToken string, filter tasks.TaskFilter) (webui.TasksData, error) {
|
||||
rows, err := options.Tasks.ListTasks(context.Request.Context(), filter)
|
||||
if err != nil {
|
||||
return webui.TasksData{}, err
|
||||
}
|
||||
startKey, err := tasks.NewCreateKey()
|
||||
if err != nil {
|
||||
return webui.TasksData{}, err
|
||||
}
|
||||
return webui.TasksData{
|
||||
CSRFToken: csrfToken,
|
||||
Tasks: rows,
|
||||
Filter: filter,
|
||||
HasFilter: filter.Keyword != "" || filter.Status != "" || filter.CreatedFrom != "" || filter.CreatedTo != "",
|
||||
StartKey: startKey,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func createErrorData(context *gin.Context, options Options, fullPage bool) (webui.TasksData, bool) {
|
||||
csrfToken := csrfFor(context, options)
|
||||
if fullPage {
|
||||
return webui.TasksData{CSRFToken: csrfToken}, true
|
||||
}
|
||||
data, err := taskListData(context, options, csrfToken, tasks.TaskFilter{})
|
||||
if err != nil {
|
||||
context.Status(http.StatusInternalServerError)
|
||||
return webui.TasksData{}, false
|
||||
}
|
||||
return data, true
|
||||
}
|
||||
|
||||
func renderTasks(context *gin.Context, status int, data webui.TasksData) {
|
||||
context.Header("Content-Type", "text/html; charset=utf-8")
|
||||
context.Status(status)
|
||||
|
||||
Reference in New Issue
Block a user