-
Notifications
You must be signed in to change notification settings - Fork 0
/
public_middleware.go
70 lines (61 loc) · 2.04 KB
/
public_middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package middleware
import (
"fmt"
"net/http"
"strings"
"github.com/RBucket-Org/RB-Utils/utils/crypto_utils"
"github.com/RBucket-Org/RB-Utils/utils/rest_errors"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
"go.uber.org/zap"
)
type PublicClaim struct {
Key string
jwt.RegisteredClaims
}
type PublicValidate func(token string, secret string) (*PublicClaim, rest_errors.RestError)
func PublicMiddleWare(extractionKey string, restKey string, validateToken PublicValidate, sugarLogger *zap.SugaredLogger) gin.HandlerFunc {
return func(c *gin.Context) {
//get the auth token from the header of the URI
clientToken := c.Request.Header.Get("Authorization")
if clientToken == "" {
sugarLogger.Errorf("no authorization token")
emptyAuth := rest_errors.NewError("no authorization token", "empty_token", http.StatusForbidden)
c.JSON(int(emptyAuth.Status()), emptyAuth)
c.Abort()
return
}
//split the extracted token
extractedKey := strings.Split(clientToken, extractionKey)
if len(extractedKey) == 2 {
clientToken = strings.TrimSpace(extractedKey[len(extractedKey)-1])
} else {
sugarLogger.Errorf("invalid public auth token")
emptyAuth := rest_errors.NewError("invalid public auth token", "empty_token", http.StatusBadRequest)
c.JSON(int(emptyAuth.Status()), emptyAuth)
c.Abort()
return
}
//validate the token
claims, err := validateToken(clientToken, restKey)
if err != nil {
sugarLogger.Errorf(err.Message())
c.JSON(int(err.Status()), err)
c.Abort()
return
}
saltByte := crypto_utils.IdentityHash.GenerateSalt(extractionKey)
if !crypto_utils.IdentityHash.DoMatch(claims.Key, fmt.Sprintf("%s%s", extractionKey, restKey), saltByte) {
sugarLogger.Errorf("invalid public auth key")
emptyAuth := rest_errors.NewError("invalid public auth key", "invalid_key", http.StatusBadRequest)
c.JSON(int(emptyAuth.Status()), emptyAuth)
c.Abort()
return
}
c.Set("middleware_type", Public)
c.Set("user_id", int64(0))
c.Set("device_id", "")
c.Set("identity_key", "")
c.Next()
}
}