BasicAuthForProxy returns a Basic HTTP Proxy-Authorization middleware. If the realm is empty, "Proxy Authorization Required" will be used by default.
(accounts Accounts, realm string)
| 96 | // BasicAuthForProxy returns a Basic HTTP Proxy-Authorization middleware. |
| 97 | // If the realm is empty, "Proxy Authorization Required" will be used by default. |
| 98 | func BasicAuthForProxy(accounts Accounts, realm string) HandlerFunc { |
| 99 | if realm == "" { |
| 100 | realm = "Proxy Authorization Required" |
| 101 | } |
| 102 | realm = "Basic realm=" + strconv.Quote(realm) |
| 103 | pairs := processAccounts(accounts) |
| 104 | return func(c *Context) { |
| 105 | proxyUser, found := pairs.searchCredential(c.requestHeader("Proxy-Authorization")) |
| 106 | if !found { |
| 107 | // Credentials doesn't match, we return 407 and abort handlers chain. |
| 108 | c.Header("Proxy-Authenticate", realm) |
| 109 | c.AbortWithStatus(http.StatusProxyAuthRequired) |
| 110 | return |
| 111 | } |
| 112 | // The proxy_user credentials was found, set proxy_user's id to key AuthProxyUserKey in this context, the proxy_user's id can be read later using |
| 113 | // c.MustGet(gin.AuthProxyUserKey). |
| 114 | c.Set(AuthProxyUserKey, proxyUser) |
| 115 | } |
| 116 | } |