summaryrefslogtreecommitdiff
path: root/web/middleware/dns_manage.go
blob: d3b420b5a3c5e0532d3806e055437f9db86a9865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package middleware

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

const dnsUserId = "DNSManageUserID"

func DnsManageAuthMiddleware(c *gin.Context) {
	cfg := GetServerConfig(c)

	user, pwd, ok := c.Request.BasicAuth()
	if !ok {
		c.Request.Header.Set("WWW-Authenticate", `Basic realm="closed site"`)
		c.AbortWithStatus(http.StatusUnauthorized)
		return
	}

	if !cfg.DNSUserAuth(user, pwd) {
		c.AbortWithStatus(http.StatusForbidden)
		return
	} else {
		c.Set(dnsUserId, user)
	}

	c.Next()
}

func GetDnsAuthContext(c *gin.Context) string {
	return c.GetString(dnsUserId)
}