mirror of
https://github.com/miniflux/v2.git
synced 2025-08-16 18:01:37 +00:00
feat(api): add new endpoints to manage API keys
This commit is contained in:
parent
ebd65da3b6
commit
1cfeb52b0c
13 changed files with 316 additions and 66 deletions
|
@ -11,10 +11,9 @@ import (
|
|||
"miniflux.app/v2/internal/http/route"
|
||||
)
|
||||
|
||||
func (h *handler) removeAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *handler) deleteAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
keyID := request.RouteInt64Param(r, "keyID")
|
||||
err := h.store.RemoveAPIKey(request.UserID(r), keyID)
|
||||
if err != nil {
|
||||
if err := h.store.DeleteAPIKey(request.UserID(r), keyID); err != nil {
|
||||
html.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -9,44 +9,39 @@ import (
|
|||
"miniflux.app/v2/internal/http/request"
|
||||
"miniflux.app/v2/internal/http/response/html"
|
||||
"miniflux.app/v2/internal/http/route"
|
||||
"miniflux.app/v2/internal/locale"
|
||||
"miniflux.app/v2/internal/model"
|
||||
"miniflux.app/v2/internal/ui/form"
|
||||
"miniflux.app/v2/internal/ui/session"
|
||||
"miniflux.app/v2/internal/ui/view"
|
||||
"miniflux.app/v2/internal/validator"
|
||||
)
|
||||
|
||||
func (h *handler) saveAPIKey(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := h.store.UserByID(request.UserID(r))
|
||||
loggedUser, err := h.store.UserByID(request.UserID(r))
|
||||
if err != nil {
|
||||
html.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
apiKeyForm := form.NewAPIKeyForm(r)
|
||||
apiKeyCreationRequest := &model.APIKeyCreationRequest{
|
||||
Description: apiKeyForm.Description,
|
||||
}
|
||||
|
||||
sess := session.New(h.store, request.SessionID(r))
|
||||
view := view.New(h.tpl, r, sess)
|
||||
view.Set("form", apiKeyForm)
|
||||
view.Set("menu", "settings")
|
||||
view.Set("user", user)
|
||||
view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
|
||||
view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
|
||||
|
||||
if validationErr := apiKeyForm.Validate(); validationErr != nil {
|
||||
view.Set("errorMessage", validationErr.Translate(user.Language))
|
||||
if validationErr := validator.ValidateAPIKeyCreation(h.store, loggedUser.ID, apiKeyCreationRequest); validationErr != nil {
|
||||
sess := session.New(h.store, request.SessionID(r))
|
||||
view := view.New(h.tpl, r, sess)
|
||||
view.Set("form", apiKeyForm)
|
||||
view.Set("menu", "settings")
|
||||
view.Set("user", loggedUser)
|
||||
view.Set("countUnread", h.store.CountUnreadEntries(loggedUser.ID))
|
||||
view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(loggedUser.ID))
|
||||
view.Set("errorMessage", validationErr.Translate(loggedUser.Language))
|
||||
html.OK(w, r, view.Render("create_api_key"))
|
||||
return
|
||||
}
|
||||
|
||||
if h.store.APIKeyExists(user.ID, apiKeyForm.Description) {
|
||||
view.Set("errorMessage", locale.NewLocalizedError("error.api_key_already_exists").Translate(user.Language))
|
||||
html.OK(w, r, view.Render("create_api_key"))
|
||||
return
|
||||
}
|
||||
|
||||
apiKey := model.NewAPIKey(user.ID, apiKeyForm.Description)
|
||||
if err = h.store.CreateAPIKey(apiKey); err != nil {
|
||||
if _, err = h.store.CreateAPIKey(loggedUser.ID, apiKeyCreationRequest.Description); err != nil {
|
||||
html.ServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -5,8 +5,7 @@ package form // import "miniflux.app/v2/internal/ui/form"
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"miniflux.app/v2/internal/locale"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// APIKeyForm represents the API Key form.
|
||||
|
@ -14,18 +13,9 @@ type APIKeyForm struct {
|
|||
Description string
|
||||
}
|
||||
|
||||
// Validate makes sure the form values are valid.
|
||||
func (a APIKeyForm) Validate() *locale.LocalizedError {
|
||||
if a.Description == "" {
|
||||
return locale.NewLocalizedError("error.fields_mandatory")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewAPIKeyForm returns a new APIKeyForm.
|
||||
func NewAPIKeyForm(r *http.Request) *APIKeyForm {
|
||||
return &APIKeyForm{
|
||||
Description: r.FormValue("description"),
|
||||
Description: strings.TrimSpace(r.FormValue("description")),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -136,7 +136,7 @@ func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
|
|||
|
||||
// API Keys pages.
|
||||
uiRouter.HandleFunc("/keys", handler.showAPIKeysPage).Name("apiKeys").Methods(http.MethodGet)
|
||||
uiRouter.HandleFunc("/keys/{keyID}/remove", handler.removeAPIKey).Name("removeAPIKey").Methods(http.MethodPost)
|
||||
uiRouter.HandleFunc("/keys/{keyID}/delete", handler.deleteAPIKey).Name("deleteAPIKey").Methods(http.MethodPost)
|
||||
uiRouter.HandleFunc("/keys/create", handler.showCreateAPIKeyPage).Name("createAPIKey").Methods(http.MethodGet)
|
||||
uiRouter.HandleFunc("/keys/save", handler.saveAPIKey).Name("saveAPIKey").Methods(http.MethodPost)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue