Skip to content

Commit 5f458dd

Browse files
feat(auth): add proxy-server authentication (#3877)
1 parent 97eab7d commit 5f458dd

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

auth.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
// AuthUserKey is the cookie name for user credential in basic auth.
1717
const AuthUserKey = "user"
18+
const AuthProxyUserKey = "proxy_user"
1819

1920
// Accounts defines a key/value for user/pass list of authorized logins.
2021
type Accounts map[string]string
@@ -89,3 +90,23 @@ func authorizationHeader(user, password string) string {
8990
base := user + ":" + password
9091
return "Basic " + base64.StdEncoding.EncodeToString(bytesconv.StringToBytes(base))
9192
}
93+
94+
func BasicAuthForProxy(accounts Accounts, realm string) HandlerFunc {
95+
if realm == "" {
96+
realm = "Proxy Authorization Required"
97+
}
98+
realm = "Basic realm=" + strconv.Quote(realm)
99+
pairs := processAccounts(accounts)
100+
return func(c *Context) {
101+
proxyUser, found := pairs.searchCredential(c.requestHeader("Proxy-Authorization"))
102+
if !found {
103+
// Credentials doesn't match, we return 407 and abort handlers chain.
104+
c.Header("Proxy-Authenticate", realm)
105+
c.AbortWithStatus(http.StatusProxyAuthRequired)
106+
return
107+
}
108+
// 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
109+
// c.MustGet(gin.AuthProxyUserKey).
110+
c.Set(AuthProxyUserKey, proxyUser)
111+
}
112+
}

auth_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,40 @@ func TestBasicAuth401WithCustomRealm(t *testing.T) {
137137
assert.Equal(t, http.StatusUnauthorized, w.Code)
138138
assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.Header().Get("WWW-Authenticate"))
139139
}
140+
141+
func TestBasicAuthForProxySucceed(t *testing.T) {
142+
accounts := Accounts{"admin": "password"}
143+
router := New()
144+
router.Use(BasicAuthForProxy(accounts, ""))
145+
router.Any("/*proxyPath", func(c *Context) {
146+
c.String(http.StatusOK, c.MustGet(AuthProxyUserKey).(string))
147+
})
148+
149+
w := httptest.NewRecorder()
150+
req, _ := http.NewRequest("GET", "/test", nil)
151+
req.Header.Set("Proxy-Authorization", authorizationHeader("admin", "password"))
152+
router.ServeHTTP(w, req)
153+
154+
assert.Equal(t, http.StatusOK, w.Code)
155+
assert.Equal(t, "admin", w.Body.String())
156+
}
157+
158+
func TestBasicAuthForProxy407(t *testing.T) {
159+
called := false
160+
accounts := Accounts{"foo": "bar"}
161+
router := New()
162+
router.Use(BasicAuthForProxy(accounts, ""))
163+
router.Any("/*proxyPath", func(c *Context) {
164+
called = true
165+
c.String(http.StatusOK, c.MustGet(AuthProxyUserKey).(string))
166+
})
167+
168+
w := httptest.NewRecorder()
169+
req, _ := http.NewRequest("GET", "/test", nil)
170+
req.Header.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:password")))
171+
router.ServeHTTP(w, req)
172+
173+
assert.False(t, called)
174+
assert.Equal(t, http.StatusProxyAuthRequired, w.Code)
175+
assert.Equal(t, "Basic realm=\"Proxy Authorization Required\"", w.Header().Get("Proxy-Authenticate"))
176+
}

0 commit comments

Comments
 (0)