/
security.go
53 lines (45 loc) · 1.46 KB
/
security.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package server
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"github.com/unrolled/secure"
)
var ErrAccessDenied = errors.New("access denied")
func SecurityMiddleware(dev bool) gin.HandlerFunc {
// Maybe should add Feature-Policy, and Expect-CT
secureMiddleware := secure.New(secure.Options{
SSLRedirect: !dev,
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
STSSeconds: 31536000,
STSIncludeSubdomains: true,
STSPreload: true,
FrameDeny: true,
ContentTypeNosniff: true,
BrowserXssFilter: true,
ContentSecurityPolicy: "img-src 'self'; style-src 'self' fonts.googleapis.com maxcdn.bootstrapcdn.com 'unsafe-inline' blob:; script-src 'self'; connect-src 'self'; font-src fonts.gstatic.com maxcdn.bootstrapcdn.com",
ReferrerPolicy: "same-origin",
// FeaturePolicy: "autoplay 'none'; camera 'none'; display-capture 'none'; document-domain 'none';",
})
return func(c *gin.Context) {
err := secureMiddleware.Process(c.Writer, c.Request)
if err != nil {
c.Abort()
return
}
// Avoid header rewrite if response is a redirection.
if status := c.Writer.Status(); status > 300 && status < 399 {
c.Abort()
}
}
}
func AuthRequired(apiAuth string) gin.HandlerFunc {
return func(c *gin.Context) {
auth := c.Request.Header.Get("Authorization")
if auth != apiAuth {
_ = c.AbortWithError(http.StatusForbidden, ErrAccessDenied)
return
}
c.Next()
}
}