From 55986bab3615b0caf05a1038ddd9e99077e8863d Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Mon, 6 Jan 2025 20:17:07 +0300 Subject: [PATCH] refactor(api): move authentication into a middleware --- web/api/handlers.go | 49 +++++++-------------------------------------- web/api/routes.go | 14 ++++++------- web/api/utils.go | 16 +++++++++++++++ 3 files changed, 30 insertions(+), 49 deletions(-) diff --git a/web/api/handlers.go b/web/api/handlers.go index bba8e8a..dc0ecaa 100644 --- a/web/api/handlers.go +++ b/web/api/handlers.go @@ -38,12 +38,7 @@ func userAuth(c *gin.Context) { } func userGet(c *gin.Context) { - session := sessions.Default(c) - user_id, ok := session.Get("user_id").(string) - if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) - return - } + user_id := c.GetString("user_id") user, err := db.UserGet(context.Background(), user_id) if err != nil { status, message := HandleDBError(err) @@ -54,12 +49,7 @@ func userGet(c *gin.Context) { } func userUpdate(c *gin.Context) { - session := sessions.Default(c) - user_id, ok := session.Get("user_id").(string) - if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) - return - } + user_id := c.GetString("user_id") var body map[string]string err := c.BindJSON(&body) if err != nil { @@ -130,12 +120,7 @@ func userLogout(c *gin.Context) { //#region Quotes func quotesGet(c *gin.Context) { - session := sessions.Default(c) - user_id, ok := session.Get("user_id").(string) - if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) - return - } + user_id := c.GetString("user_id") filter, ok := c.GetQuery("filter") if !ok { filter = "" @@ -178,12 +163,7 @@ func quotesGet(c *gin.Context) { } func quoteGet(c *gin.Context) { - session := sessions.Default(c) - user_id, ok := session.Get("user_id").(string) - if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) - return - } + user_id := c.GetString("user_id") quote_id := c.Param("id") quote, err := db.QuoteGet(context.Background(), user_id, quote_id) if err != nil { @@ -195,12 +175,7 @@ func quoteGet(c *gin.Context) { } func quoteAdd(c *gin.Context) { - session := sessions.Default(c) - user_id, ok := session.Get("user_id").(string) - if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) - return - } + user_id := c.GetString("user_id") var body map[string]string err := c.BindJSON(&body) if err != nil { @@ -238,12 +213,7 @@ func quoteAdd(c *gin.Context) { } func quoteUpdate(c *gin.Context) { - session := sessions.Default(c) - user_id, ok := session.Get("user_id").(string) - if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) - return - } + user_id := c.GetString("user_id") quote_id := c.Param("id") var body map[string]string err := c.BindJSON(&body) @@ -289,12 +259,7 @@ func quoteUpdate(c *gin.Context) { } func quoteDelete(c *gin.Context) { - session := sessions.Default(c) - user_id, ok := session.Get("user_id").(string) - if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) - return - } + user_id := c.GetString("user_id") quote_id := c.Param("id") err := db.QuoteDelete(context.Background(), user_id, quote_id) if err != nil { diff --git a/web/api/routes.go b/web/api/routes.go index 48fa817..19bac28 100644 --- a/web/api/routes.go +++ b/web/api/routes.go @@ -21,14 +21,14 @@ func RegisterRoutes(r *gin.Engine) { api := r.Group("/api") { api.POST("/auth", userAuth) - api.GET("/auth", userGet) - api.PATCH("/auth", userUpdate) + api.GET("/auth", MiddlewareAuth, userGet) + api.PATCH("/auth", MiddlewareAuth, userUpdate) api.DELETE("/auth", userLogout) - api.GET("/quotes", quotesGet) - api.POST("/quotes", quoteAdd) - api.GET("/quotes/:id", quoteGet) - api.PATCH("/quotes/:id", quoteUpdate) - api.DELETE("/quotes/:id", quoteDelete) + api.GET("/quotes", MiddlewareAuth, quotesGet) + api.POST("/quotes", MiddlewareAuth, quoteAdd) + api.GET("/quotes/:id", MiddlewareAuth, quoteGet) + api.PATCH("/quotes/:id", MiddlewareAuth, quoteUpdate) + api.DELETE("/quotes/:id", MiddlewareAuth, quoteDelete) } } diff --git a/web/api/utils.go b/web/api/utils.go index 48d3b57..3c731a2 100644 --- a/web/api/utils.go +++ b/web/api/utils.go @@ -1,8 +1,12 @@ package api import ( + "net/http" "strconv" + "strings" + "github.com/gin-contrib/sessions" + "github.com/gin-gonic/gin" "github.com/jackc/pgx/v5/pgconn" ) @@ -20,3 +24,15 @@ func HandleDBError(err error) (int, string) { return 400, pgErr.Message } } + +func MiddlewareAuth(c *gin.Context) { + session := sessions.Default(c) + user_id, ok := session.Get("user_id").(string) + if !ok && strings.HasPrefix(c.Request.URL.Path, "/api") { + c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Ты это, залогинься сначала что ли, а то чё как крыса"}) + return + } + c.Set("authorized", ok) + c.Set("user_id", user_id) + c.Next() +}