AuthFunc defines basic strategy as the gin authentication middleware.
()
| 32 | |
| 33 | // AuthFunc defines basic strategy as the gin authentication middleware. |
| 34 | func (b BasicStrategy) AuthFunc() gin.HandlerFunc { |
| 35 | return func(c *gin.Context) { |
| 36 | auth := strings.SplitN(c.Request.Header.Get("Authorization"), " ", 2) |
| 37 | |
| 38 | if len(auth) != 2 || auth[0] != "Basic" { |
| 39 | core.WriteResponse( |
| 40 | c, |
| 41 | errors.WithCode(code.ErrSignatureInvalid, "Authorization header format is wrong."), |
| 42 | nil, |
| 43 | ) |
| 44 | c.Abort() |
| 45 | |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | payload, _ := base64.StdEncoding.DecodeString(auth[1]) |
| 50 | pair := strings.SplitN(string(payload), ":", 2) |
| 51 | |
| 52 | if len(pair) != 2 || !b.compare(pair[0], pair[1]) { |
| 53 | core.WriteResponse( |
| 54 | c, |
| 55 | errors.WithCode(code.ErrSignatureInvalid, "Authorization header format is wrong."), |
| 56 | nil, |
| 57 | ) |
| 58 | c.Abort() |
| 59 | |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | c.Set(middleware.UsernameKey, pair[0]) |
| 64 | |
| 65 | c.Next() |
| 66 | } |
| 67 | } |