diff options
Diffstat (limited to 'web/middleware/acme.go')
-rw-r--r-- | web/middleware/acme.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/web/middleware/acme.go b/web/middleware/acme.go new file mode 100644 index 0000000..54a4dfc --- /dev/null +++ b/web/middleware/acme.go | |||
@@ -0,0 +1,33 @@ | |||
1 | package middleware | ||
2 | |||
3 | import ( | ||
4 | "net/http" | ||
5 | |||
6 | "github.com/gin-gonic/gin" | ||
7 | ) | ||
8 | |||
9 | const acmeUserId = "ACMEAuthUserID" | ||
10 | |||
11 | func AcmeAuthMiddleware(c *gin.Context) { | ||
12 | cfg := GetServerConfig(c) | ||
13 | |||
14 | _, pwd, ok := c.Request.BasicAuth() | ||
15 | if !ok { | ||
16 | c.Request.Header.Set("WWW-Authenticate", `Basic realm="closed site"`) | ||
17 | c.AbortWithStatus(http.StatusUnauthorized) | ||
18 | return | ||
19 | } | ||
20 | |||
21 | if !cfg.AcmeSecretExists(pwd) { | ||
22 | c.AbortWithStatus(http.StatusForbidden) | ||
23 | return | ||
24 | } else { | ||
25 | c.Set(acmeUserId, pwd) | ||
26 | } | ||
27 | |||
28 | c.Next() | ||
29 | } | ||
30 | |||
31 | func GetAcmeAuthContext(c *gin.Context) string { | ||
32 | return c.GetString(acmeUserId) | ||
33 | } | ||