wg-portal/internal/server/routes.go

92 lines
2.7 KiB
Go
Raw Normal View History

2020-11-05 13:37:51 -05:00
package server
import (
"net/http"
"github.com/gin-gonic/gin"
wg_portal "github.com/h44z/wg-portal"
2020-11-05 13:37:51 -05:00
)
func SetupRoutes(s *Server) {
// Startpage
s.server.GET("/", s.GetIndex)
s.server.GET("/favicon.ico", func(c *gin.Context) {
file, _ := wg_portal.Statics.ReadFile("assets/img/favicon.ico")
c.Data(
http.StatusOK,
"image/x-icon",
file,
)
})
2020-11-05 13:37:51 -05:00
// Auth routes
auth := s.server.Group("/auth")
auth.GET("/login", s.GetLogin)
auth.POST("/login", s.PostLogin)
auth.GET("/logout", s.GetLogout)
// Admin routes
admin := s.server.Group("/admin")
admin.Use(s.RequireAuthentication("admin"))
2020-11-05 13:37:51 -05:00
admin.GET("/", s.GetAdminIndex)
2020-11-07 04:31:48 -05:00
admin.GET("/device/edit", s.GetAdminEditInterface)
admin.POST("/device/edit", s.PostAdminEditInterface)
2020-11-10 03:31:02 -05:00
admin.GET("/device/download", s.GetInterfaceConfig)
2020-11-10 16:23:05 -05:00
admin.GET("/device/applyglobals", s.GetApplyGlobalConfig)
2020-11-07 04:31:48 -05:00
admin.GET("/peer/edit", s.GetAdminEditPeer)
admin.POST("/peer/edit", s.PostAdminEditPeer)
admin.GET("/peer/create", s.GetAdminCreatePeer)
admin.POST("/peer/create", s.PostAdminCreatePeer)
2020-11-07 14:32:25 -05:00
admin.GET("/peer/createldap", s.GetAdminCreateLdapPeers)
2020-11-08 04:26:18 -05:00
admin.POST("/peer/createldap", s.PostAdminCreateLdapPeers)
2020-11-09 05:17:19 -05:00
admin.GET("/peer/delete", s.GetAdminDeletePeer)
2020-11-10 03:31:02 -05:00
admin.GET("/peer/download", s.GetPeerConfig)
admin.GET("/peer/email", s.GetPeerConfigMail)
2020-11-05 13:37:51 -05:00
admin.GET("/users/", s.GetAdminUsersIndex)
admin.GET("/users/create", s.GetAdminUsersCreate)
admin.POST("/users/create", s.PostAdminUsersCreate)
admin.GET("/users/edit", s.GetAdminUsersEdit)
admin.POST("/users/edit", s.PostAdminUsersEdit)
2020-11-05 13:37:51 -05:00
// User routes
user := s.server.Group("/user")
user.Use(s.RequireAuthentication("")) // empty scope = all logged in users
2020-11-10 03:31:02 -05:00
user.GET("/qrcode", s.GetPeerQRCode)
user.GET("/profile", s.GetUserIndex)
2020-11-10 03:31:02 -05:00
user.GET("/download", s.GetPeerConfig)
user.GET("/email", s.GetPeerConfigMail)
2020-11-10 16:23:05 -05:00
user.GET("/status", s.GetPeerStatus)
2020-11-05 13:37:51 -05:00
}
func (s *Server) RequireAuthentication(scope string) gin.HandlerFunc {
return func(c *gin.Context) {
session := GetSessionData(c)
2020-11-05 13:37:51 -05:00
if !session.LoggedIn {
// Abort the request with the appropriate error code
c.Abort()
2020-11-10 03:31:02 -05:00
c.Redirect(http.StatusSeeOther, "/auth/login?err=loginreq")
2020-11-05 13:37:51 -05:00
return
}
if scope == "admin" && !session.IsAdmin {
// Abort the request with the appropriate error code
c.Abort()
s.GetHandleError(c, http.StatusUnauthorized, "unauthorized", "not enough permissions")
return
}
// default case if some randome scope was set...
if scope != "" && !session.IsAdmin {
2020-11-05 13:37:51 -05:00
// Abort the request with the appropriate error code
c.Abort()
2020-11-10 03:31:02 -05:00
s.GetHandleError(c, http.StatusUnauthorized, "unauthorized", "not enough permissions")
2020-11-05 13:37:51 -05:00
return
}
// Continue down the chain to handler etc
c.Next()
}
}