MCPcopy
hub / github.com/gin-gonic/gin / BasicAuthForProxy

Function BasicAuthForProxy

auth.go:98–116  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

96// BasicAuthForProxy returns a Basic HTTP Proxy-Authorization middleware.
97// If the realm is empty, "Proxy Authorization Required" will be used by default.
98func 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}

Callers 2

TestBasicAuthForProxy407Function · 0.85

Calls 6

processAccountsFunction · 0.85
searchCredentialMethod · 0.80
requestHeaderMethod · 0.80
AbortWithStatusMethod · 0.80
SetMethod · 0.80
HeaderMethod · 0.45

Tested by 2

TestBasicAuthForProxy407Function · 0.68