Skip to content

Commit

Permalink
Add code for token auth
Browse files Browse the repository at this point in the history
  • Loading branch information
websterzh committed May 30, 2023
1 parent 354cf85 commit 8aef6a8
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/config.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ remove_fail_fsd_check = 2
fsd_fetch_interval_second = 30
fsd_fetch_timeout_second = 10
update_interval_second = 5
token_generate_key = sample_key
token_ttl_minute = 1440

[redis]
address = localhost
Expand Down
32 changes: 32 additions & 0 deletions gin/middlewares/recover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package middlewares

import (
"github.com/gin-gonic/gin"
"log"
"net/http"
"runtime/debug"
)

func Recover(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
log.Printf("panic: %v\n", r)
debug.PrintStack()
c.JSON(http.StatusInternalServerError, gin.H{
"msg": errorToString(r),
"data": nil,
})
c.Abort()
}
}()
c.Next()
}

func errorToString(r interface{}) string {
switch v := r.(type) {
case error:
return v.Error()
default:
return r.(string)
}
}
20 changes: 20 additions & 0 deletions gin/middlewares/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package middlewares

import (
"github.com/gin-gonic/gin"
"net/http"
)

func TokenAuth() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.Request.Header.Get("token")
if token == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"message": "Unauthorized",
})
c.Abort()
return
}
}
}
5 changes: 5 additions & 0 deletions gin/router/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func InitRouter() *gin.Engine {
{
v1.GET("/hello", errors.ErrorWrapper(handlers.HelloWorld))
v1.GET("/queue", errors.ErrorWrapper(handlers.GetMultipleQueuesHandler))
token := v1.Group("/token")
{
token.POST("", errors.ErrorWrapper(handlers.HelloWorld))
token.DELETE("/:token", errors.ErrorWrapper(handlers.HelloWorld))
}
airport := v1.Group("/:airport")
{
airport.PATCH("/status", errors.ErrorWrapper(handlers.UpdateStatus))
Expand Down
35 changes: 35 additions & 0 deletions gin/services/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package services

import (
"context"
"fmt"
"github.com/google/uuid"
"strings"
"time"
"vatprc-queue/config"
"vatprc-queue/redis"
)

func CreateToken(scope string) string {
newToken := strings.ReplaceAll(uuid.New().String(), "-", "")
exp := config.File.Section("app").Key("token_ttl_minute").MustInt(60)
redis.Client.Set(context.Background(), getTokenKey(newToken), scope, time.Duration(exp)*time.Minute)
return newToken
}

func DeleteToken(token string) {
redis.Client.Del(context.Background(), getTokenKey(token))
}

func HasToken(token string, airport string) bool {
// TODO: Check airport
exist, err := redis.Client.Exists(context.Background(), getTokenKey(token)).Result()
if err != nil {
return false
}
return exist == 1
}

func getTokenKey(token string) string {
return fmt.Sprint("token_", token)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.10.3 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
Expand Down

0 comments on commit 8aef6a8

Please sign in to comment.