1
0

feat(api tokens): add crud routes to manage api tokens

This commit is contained in:
kolaente
2023-08-31 20:37:25 +02:00
parent 3faf48706a
commit e6b25bd57b
5 changed files with 154 additions and 9 deletions

View File

@ -22,6 +22,8 @@ import (
"net/http"
"code.vikunja.io/api/pkg/log"
_ "code.vikunja.io/api/pkg/swagger" // To make sure the swag files are properly registered
"github.com/labstack/echo/v4"
"github.com/swaggo/swag"
)

View File

@ -17,9 +17,10 @@
package routes
import (
"github.com/labstack/echo/v4"
"net/http"
"strings"
"github.com/labstack/echo/v4"
)
var apiTokenRoutes = map[string]*APITokenRoute{}
@ -56,7 +57,7 @@ func getRouteGroupName(route echo.Route) string {
}
}
// gets called for every added APITokenRoute and builds a list of all routes we can use for the api tokens.
// collectRoutesForAPITokenUsage gets called for every added APITokenRoute and builds a list of all routes we can use for the api tokens.
func collectRoutesForAPITokenUsage(route echo.Route) {
if !strings.Contains(route.Name, "(*WebHandler)") {
@ -65,7 +66,10 @@ func collectRoutesForAPITokenUsage(route echo.Route) {
routeGroupName := getRouteGroupName(route)
if routeGroupName == "subscriptions" || routeGroupName == "notifications" || strings.HasSuffix(routeGroupName, "_bulk") {
if routeGroupName == "subscriptions" ||
routeGroupName == "notifications" ||
routeGroupName == "tokens" ||
strings.HasSuffix(routeGroupName, "_bulk") {
return
}
@ -94,7 +98,7 @@ func collectRoutesForAPITokenUsage(route echo.Route) {
// GetAvailableAPIRoutesForToken returns a list of all API routes which are available for token usage.
// @Summary Get a list of all token api routes
// @Description Returns a list of all API routes which are available to use with an api token, not a user login.
// @tags opi
// @tags api
// @Produce json
// @Security JWTKeyAuth
// @Success 200 {array} routes.APITokenRoute "The list of all routes."

View File

@ -563,6 +563,16 @@ func registerAPIRoutes(a *echo.Group) {
a.GET("/backgrounds/unsplash/images/:image", unsplash.ProxyUnsplashImage)
}
}
// API Tokens
apiTokenProvider := &handler.WebHandler{
EmptyStruct: func() handler.CObject {
return &models.APIToken{}
},
}
a.GET("/tokens", apiTokenProvider.ReadAllWeb)
a.PUT("/tokens", apiTokenProvider.CreateWeb)
a.DELETE("/tokens/:token", apiTokenProvider.DeleteWeb)
}
func registerMigrations(m *echo.Group) {