RESTful API for WireGuard Portal (#11)

This commit is contained in:
Christoph Haas
2021-04-26 22:00:50 +02:00
parent 35513ae994
commit 87964f8ec4
12 changed files with 1724 additions and 82 deletions

View File

@@ -10,9 +10,18 @@ import (
_ "github.com/h44z/wg-portal/internal/server/docs" // docs is generated by Swag CLI, you have to import it.
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
csrf "github.com/utrack/gin-csrf"
)
func SetupRoutes(s *Server) {
csrfMiddleware := csrf.Middleware(csrf.Options{
Secret: s.config.Core.SessionSecret,
ErrorFunc: func(c *gin.Context) {
c.String(400, "CSRF token mismatch")
c.Abort()
},
})
// Startpage
s.server.GET("/", s.GetIndex)
s.server.GET("/favicon.ico", func(c *gin.Context) {
@@ -26,12 +35,14 @@ func SetupRoutes(s *Server) {
// Auth routes
auth := s.server.Group("/auth")
auth.Use(csrfMiddleware)
auth.GET("/login", s.GetLogin)
auth.POST("/login", s.PostLogin)
auth.GET("/logout", s.GetLogout)
// Admin routes
admin := s.server.Group("/admin")
admin.Use(csrfMiddleware)
admin.Use(s.RequireAuthentication("admin"))
admin.GET("/", s.GetAdminIndex)
admin.GET("/device/edit", s.GetAdminEditInterface)
@@ -57,6 +68,7 @@ func SetupRoutes(s *Server) {
// User routes
user := s.server.Group("/user")
user.Use(csrfMiddleware)
user.Use(s.RequireAuthentication("")) // empty scope = all logged in users
user.GET("/qrcode", s.GetPeerQRCode)
user.GET("/profile", s.GetUserIndex)
@@ -68,15 +80,35 @@ func SetupRoutes(s *Server) {
func SetupApiRoutes(s *Server) {
api := ApiServer{s: s}
// Auth routes
apiV1 := s.server.Group("/api/v1")
apiV1.Use(s.RequireApiAuthentication("admin"))
apiV1.GET("/users", api.GetUsers)
apiV1.POST("/users", api.PostUser)
apiV1.GET("/user/:email", api.GetUser)
apiV1.PUT("/user/:email", api.PutUser)
apiV1.PATCH("/user/:email", api.PatchUser)
apiV1.DELETE("/user/:email", api.DeleteUser)
// Admin authenticated routes
apiV1Backend := s.server.Group("/api/v1/backend")
apiV1Backend.Use(s.RequireApiAuthentication("admin"))
apiV1Backend.GET("/users", api.GetUsers)
apiV1Backend.POST("/users", api.PostUser)
apiV1Backend.GET("/user/:email", api.GetUser)
apiV1Backend.PUT("/user/:email", api.PutUser)
apiV1Backend.PATCH("/user/:email", api.PatchUser)
apiV1Backend.DELETE("/user/:email", api.DeleteUser)
apiV1Backend.GET("/peers/:device", api.GetPeers)
apiV1Backend.POST("/peers/:device", api.PostPeer)
apiV1Backend.GET("/peer/:pkey", api.GetPeer)
apiV1Backend.PUT("/peer/:pkey", api.PutPeer)
apiV1Backend.PATCH("/peer/:pkey", api.PatchPeer)
apiV1Backend.DELETE("/peer/:pkey", api.DeletePeer)
apiV1Backend.GET("/devices", api.GetDevices)
apiV1Backend.GET("/device/:device", api.GetDevice)
apiV1Backend.PUT("/device/:device", api.PutDevice)
apiV1Backend.PATCH("/device/:device", api.PatchDevice)
// Simple authenticated routes
apiV1Deployment := s.server.Group("/api/v1/provisioning")
apiV1Deployment.Use(s.RequireApiAuthentication(""))
apiV1Deployment.GET("/peer/:pkey", api.GetPeerDeploymentConfig)
apiV1Deployment.POST("/peer", api.PostPeerDeploymentConfig)
// Swagger doc/ui
s.server.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))