BasicAuthForRealm returns a Basic HTTP Authorization middleware. It takes as arguments a map[string]string where the key is the user name and the value is the password, as well as the name of the Realm. If the realm is empty, "Authorization Required" will be used by default. (see http://tools.ietf.o
(accounts Accounts, realm string)
| 46 | // If the realm is empty, "Authorization Required" will be used by default. |
| 47 | // (see http://tools.ietf.org/html/rfc2617#section-1.2) |
| 48 | func BasicAuthForRealm(accounts Accounts, realm string) HandlerFunc { |
| 49 | if realm == "" { |
| 50 | realm = "Authorization Required" |
| 51 | } |
| 52 | realm = "Basic realm=" + strconv.Quote(realm) |
| 53 | pairs := processAccounts(accounts) |
| 54 | return func(c *Context) { |
| 55 | // Search user in the slice of allowed credentials |
| 56 | user, found := pairs.searchCredential(c.requestHeader("Authorization")) |
| 57 | if !found { |
| 58 | // Credentials doesn't match, we return 401 and abort handlers chain. |
| 59 | c.Header("WWW-Authenticate", realm) |
| 60 | c.AbortWithStatus(http.StatusUnauthorized) |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | // The user credentials was found, set user's id to key AuthUserKey in this context, the user's id can be read later using |
| 65 | // c.MustGet(gin.AuthUserKey). |
| 66 | c.Set(AuthUserKey, user) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // BasicAuth returns a Basic HTTP Authorization middleware. It takes as argument a map[string]string where |
| 71 | // the key is the user name and the value is the password. |