Skip to content

Commit

Permalink
Implemented authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
websterzh committed May 31, 2023
1 parent 8aef6a8 commit cdebfe3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
31 changes: 31 additions & 0 deletions gin/handlers/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package handlers

import (
"github.com/gin-gonic/gin"
"net/http"
"vatprc-queue/gin/errors"
"vatprc-queue/gin/services"
)

func CreateToken(c *gin.Context) (interface{}, error) {
newToken := services.CreateToken("*")
return newToken, nil
}

type DeleteTokenRequest struct {
Token string `json:"token"`
}

func DeleteToken(c *gin.Context) (interface{}, error) {
token, ok := c.Params.Get("token")
if token == "" || !ok {
return nil, errors.ApiError{
Status: http.StatusBadRequest,
Code: http.StatusBadRequest,
ShowInProduction: false,
Message: "Token is required",
}
}
services.DeleteToken(token)
return nil, nil
}
36 changes: 35 additions & 1 deletion gin/middlewares/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package middlewares
import (
"github.com/gin-gonic/gin"
"net/http"
"vatprc-queue/config"
"vatprc-queue/gin/services"
)

func TokenAuth() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.Request.Header.Get("token")
token := c.Request.Header.Get("Authorization")
if token == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
Expand All @@ -16,5 +18,37 @@ func TokenAuth() gin.HandlerFunc {
c.Abort()
return
}
if !services.HasToken(token, "*") {
c.JSON(http.StatusForbidden, gin.H{
"code": http.StatusForbidden,
"message": "Forbidden",
})
c.Abort()
return
}
c.Next()
}
}

func AtcCenterAuth() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.Request.Header.Get("Authorization")
if token == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"message": "Unauthorized",
})
c.Abort()
return
}
if token != config.File.Section("app").Key("token_generate_key").String() {
c.JSON(http.StatusForbidden, gin.H{
"code": http.StatusForbidden,
"message": "Forbidden",
})
c.Abort()
return
}
c.Next()
}
}
14 changes: 10 additions & 4 deletions gin/router/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gin-gonic/gin"
"vatprc-queue/gin/errors"
"vatprc-queue/gin/handlers"
"vatprc-queue/gin/middlewares"
)

func InitRouter() *gin.Engine {
Expand All @@ -18,13 +19,18 @@ func InitRouter() *gin.Engine {
v1.GET("/queue", errors.ErrorWrapper(handlers.GetMultipleQueuesHandler))
token := v1.Group("/token")
{
token.POST("", errors.ErrorWrapper(handlers.HelloWorld))
token.DELETE("/:token", errors.ErrorWrapper(handlers.HelloWorld))
token.Use(middlewares.AtcCenterAuth())
token.POST("", errors.ErrorWrapper(handlers.CreateToken))
token.DELETE("/:token", errors.ErrorWrapper(handlers.DeleteToken))
}
airport := v1.Group("/:airport")
{
airport.PATCH("/status", errors.ErrorWrapper(handlers.UpdateStatus))
airport.PATCH("/order", errors.ErrorWrapper(handlers.UpdateOrderHandler))
protected := airport.Group("")
protected.Use(middlewares.TokenAuth())
{
protected.PATCH("/status", errors.ErrorWrapper(handlers.UpdateStatus))
protected.PATCH("/order", errors.ErrorWrapper(handlers.UpdateOrderHandler))
}
airport.GET("/queue", errors.ErrorWrapper(handlers.GetQueueHandler))
airport.GET("/ws", handlers.NewWSConnection)
}
Expand Down

0 comments on commit cdebfe3

Please sign in to comment.