Refactor auth code

This commit is contained in:
Viacheslav Evseev
2024-02-22 01:50:03 +03:00
parent af14dbbeb8
commit c3f89042f9
11 changed files with 68 additions and 98 deletions

View File

@@ -21,7 +21,6 @@ import (
//
// @Param hash path string true "Torrent hash"
// @Param id path string true "File index in torrent"
// @Param not_auth query bool false "Not authenticated"
//
// @Produce application/octet-stream
// @Success 200 "Torrent data"
@@ -29,7 +28,7 @@ import (
func play(c *gin.Context) {
hash := c.Param("hash")
indexStr := c.Param("id")
notAuth := c.GetBool("not_auth")
notAuth := c.GetBool("auth_required") && c.GetString(gin.AuthUserKey) == ""
if hash == "" || indexStr == "" {
c.AbortWithError(http.StatusNotFound, errors.New("link should not be empty"))

View File

@@ -1,6 +1,8 @@
package api
import (
"server/web/auth"
"github.com/gin-gonic/gin"
)
@@ -8,15 +10,17 @@ type requestI struct {
Action string `json:"action,omitempty"`
}
func SetupRoute(route *gin.RouterGroup) {
route.GET("/shutdown", shutdown)
func SetupRoute(route gin.IRouter) {
authorized := route.Group("/", auth.CheckAuth())
route.POST("/settings", settings)
authorized.GET("/shutdown", shutdown)
route.POST("/torrents", torrents)
route.POST("/torrent/upload", torrentUpload)
authorized.POST("/settings", settings)
route.POST("/cache", cache)
authorized.POST("/torrents", torrents)
authorized.POST("/torrent/upload", torrentUpload)
authorized.POST("/cache", cache)
route.HEAD("/stream", stream)
route.HEAD("/stream/*fname", stream)
@@ -27,15 +31,15 @@ func SetupRoute(route *gin.RouterGroup) {
route.HEAD("/play/:hash/:id", play)
route.GET("/play/:hash/:id", play)
route.POST("/viewed", viewed)
authorized.POST("/viewed", viewed)
route.GET("/playlistall/all.m3u", allPlayList)
authorized.GET("/playlistall/all.m3u", allPlayList)
route.GET("/playlist", playList)
route.GET("/playlist/*fname", playList) // Is this endpoint still needed ? `fname` is never used in handler
route.GET("/download/:size", download)
authorized.GET("/download/:size", download)
route.GET("/search/*query", rutorSearch)
authorized.GET("/search/*query", rutorSearch)
route.GET("/ffp/:hash/:id", ffp)
authorized.GET("/ffp/:hash/:id", ffp)
}

View File

@@ -45,7 +45,6 @@ import (
// @Param play query string false "Start stream torrent"
// @Param title query string true "Set title of torrent"
// @Param poster query string true "Set poster link of torrent"
// @Param not_auth query string true "Stream / playlist without authentication"
//
// @Produce application/octet-stream
// @Success 200 "Data returned according to query"
@@ -62,7 +61,7 @@ func stream(c *gin.Context) {
title := c.Query("title")
poster := c.Query("poster")
data := ""
notAuth := c.GetBool("not_auth")
notAuth := c.GetBool("auth_required") && c.GetString(gin.AuthUserKey) == ""
if notAuth && (play || m3u) {
streamNoAuth(c)