@@ -14,7 +14,7 @@ import (
14
14
// is returned. On success, the wrapped middleware is called, and the userID is made available as
15
15
// c.Get("userID").(string).
16
16
// 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
18
18
type GinJWTMiddleware struct {
19
19
// Realm name to display to the user. Required.
20
20
Realm string
@@ -64,6 +64,9 @@ type GinJWTMiddleware struct {
64
64
// - "query:<name>"
65
65
// - "cookie:<name>"
66
66
TokenLookup string
67
+
68
+ // TokenHeadName is a string in the header. Default value is "Bearer"
69
+ TokenHeadName string
67
70
}
68
71
69
72
// Login form structure.
@@ -87,6 +90,11 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
87
90
mw .Timeout = time .Hour
88
91
}
89
92
93
+ mw .TokenHeadName = strings .TrimSpace (mw .TokenHeadName )
94
+ if len (mw .TokenHeadName ) == 0 {
95
+ mw .TokenHeadName = "Bearer"
96
+ }
97
+
90
98
if mw .Authorizator == nil {
91
99
mw .Authorizator = func (userID string , c * gin.Context ) bool {
92
100
return true
@@ -289,7 +297,7 @@ func (mw *GinJWTMiddleware) jwtFromHeader(c *gin.Context, key string) (string, e
289
297
}
290
298
291
299
parts := strings .SplitN (authHeader , " " , 2 )
292
- if ! (len (parts ) == 2 && parts [0 ] == "Bearer" ) {
300
+ if ! (len (parts ) == 2 && parts [0 ] == mw . TokenHeadName ) {
293
301
return "" , errors .New ("invalid auth header" )
294
302
}
295
303
0 commit comments