Skip to content

Commit 974e047

Browse files
authored
feat: Support change default head name (#60)
default value is Bearer
1 parent 9ce6825 commit 974e047

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ func main() {
104104
TokenLookup: "header:Authorization",
105105
// TokenLookup: "query:token",
106106
// TokenLookup: "cookie:token",
107+
108+
// TokenHeadName is a string in the header. Default value is "Bearer"
109+
TokenHeadName: "Bearer",
107110
}
108111

109112
r.POST("/login", authMiddleware.LoginHandler)

auth_jwt.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// is returned. On success, the wrapped middleware is called, and the userID is made available as
1515
// c.Get("userID").(string).
1616
// Users can get a token by posting a json request to LoginHandler. The token then needs to be passed in
17-
// the Authentication header. Example: Authorization:Bearer XXX_TOKEN_XXX#!/usr/bin/env
17+
// the Authentication header. Example: Authorization:Bearer XXX_TOKEN_XXX
1818
type GinJWTMiddleware struct {
1919
// Realm name to display to the user. Required.
2020
Realm string
@@ -64,6 +64,9 @@ type GinJWTMiddleware struct {
6464
// - "query:<name>"
6565
// - "cookie:<name>"
6666
TokenLookup string
67+
68+
// TokenHeadName is a string in the header. Default value is "Bearer"
69+
TokenHeadName string
6770
}
6871

6972
// Login form structure.
@@ -87,6 +90,11 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
8790
mw.Timeout = time.Hour
8891
}
8992

93+
mw.TokenHeadName = strings.TrimSpace(mw.TokenHeadName)
94+
if len(mw.TokenHeadName) == 0 {
95+
mw.TokenHeadName = "Bearer"
96+
}
97+
9098
if mw.Authorizator == nil {
9199
mw.Authorizator = func(userID string, c *gin.Context) bool {
92100
return true
@@ -289,7 +297,7 @@ func (mw *GinJWTMiddleware) jwtFromHeader(c *gin.Context, key string) (string, e
289297
}
290298

291299
parts := strings.SplitN(authHeader, " ", 2)
292-
if !(len(parts) == 2 && parts[0] == "Bearer") {
300+
if !(len(parts) == 2 && parts[0] == mw.TokenHeadName) {
293301
return "", errors.New("invalid auth header")
294302
}
295303

auth_jwt_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -707,3 +707,39 @@ func TestTokenFromCookieString(t *testing.T) {
707707
assert.Equal(t, http.StatusOK, r.Code)
708708
})
709709
}
710+
711+
func TestDefineTokenHeadName(t *testing.T) {
712+
// the middleware to test
713+
authMiddleware := &GinJWTMiddleware{
714+
Realm: "test zone",
715+
Key: key,
716+
Timeout: time.Hour,
717+
TokenHeadName: "JWTTOKEN ",
718+
Authenticator: func(userId string, password string, c *gin.Context) (string, bool) {
719+
if userId == "admin" && password == "admin" {
720+
return userId, true
721+
}
722+
return userId, false
723+
},
724+
}
725+
726+
handler := ginHandler(authMiddleware)
727+
728+
r := gofight.New()
729+
730+
r.GET("/auth/hello").
731+
SetHeader(gofight.H{
732+
"Authorization": "Bearer " + makeTokenString("HS256", "admin"),
733+
}).
734+
Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
735+
assert.Equal(t, http.StatusUnauthorized, r.Code)
736+
})
737+
738+
r.GET("/auth/hello").
739+
SetHeader(gofight.H{
740+
"Authorization": "JWTTOKEN " + makeTokenString("HS256", "admin"),
741+
}).
742+
Run(handler, func(r gofight.HTTPResponse, rq gofight.HTTPRequest) {
743+
assert.Equal(t, http.StatusOK, r.Code)
744+
})
745+
}

0 commit comments

Comments
 (0)