-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
37 lines (30 loc) · 820 Bytes
/
auth.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
package server
import (
"crypto/sha256"
"crypto/subtle"
"regexp"
"strings"
"github.com/equals215/deepsentinel/config"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/keyauth"
)
var protectedURLs = []*regexp.Regexp{
regexp.MustCompile("^/probe(/.*)?$"),
}
func authFilter(c *fiber.Ctx) bool {
originalURL := strings.ToLower(c.OriginalURL())
for _, pattern := range protectedURLs {
if pattern.MatchString(originalURL) {
return false
}
}
return true
}
func validateAuth(_ *fiber.Ctx, givenKey string) (bool, error) {
hashedKey := sha256.Sum256([]byte(config.Server.AuthToken))
hashedGivenKey := sha256.Sum256([]byte(givenKey))
if subtle.ConstantTimeCompare(hashedKey[:], hashedGivenKey[:]) == 1 {
return true, nil
}
return false, keyauth.ErrMissingOrMalformedAPIKey
}