-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbearer_token.go
69 lines (57 loc) · 1.63 KB
/
bearer_token.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
package bearertoken
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
type TokenVerificationFunc func(string, *gin.Context) bool
type Handler func(*gin.Context)
type Options struct {
OnAuthorizationHeaderMissing Handler
OnAuthorizationHeaderInvalid Handler
OnTokenInvalid Handler
}
func Middleware(tokenVerificationFunc TokenVerificationFunc, opt ...Options) gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
// Authorization header is missing in HTTP request
if authHeader == "" {
if len(opt) > 0 && opt[0].OnAuthorizationHeaderMissing != nil {
opt[0].OnAuthorizationHeaderMissing(c)
} else {
c.AbortWithStatus(http.StatusUnauthorized)
}
return
}
authTokens := strings.Split(authHeader, " ")
// The value of authorization header is invalid
// It should start with "Bearer ", then the token value
if len(authTokens) != 2 || authTokens[0] != "Bearer" {
if len(opt) > 0 && opt[0].OnAuthorizationHeaderInvalid != nil {
opt[0].OnAuthorizationHeaderInvalid(c)
} else {
c.AbortWithStatus(http.StatusUnauthorized)
}
return
}
// Check token value is valid or not
if !tokenVerificationFunc(authTokens[1], c) {
return
}
// Everything looks fine, process next action
c.Next()
}
}
func MiddlewareWithStaticToken(token string, opt ...Options) gin.HandlerFunc {
return Middleware(func(s string, c *gin.Context) bool {
if s != token {
if len(opt) > 0 && opt[0].OnTokenInvalid != nil {
opt[0].OnTokenInvalid(c)
} else {
c.AbortWithStatus(http.StatusUnauthorized)
}
return false
}
return true
}, opt...)
}