-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttp_auth.go
91 lines (78 loc) · 2.23 KB
/
http_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"github.com/gofiber/fiber/v2"
"github.com/prometheus/client_golang/prometheus"
)
func failedResponse(config *config, ip string, realCheck bool) bool {
if realCheck {
defer prometheusRequestAuthFailed.Inc()
defer config.getLogger().
Info().
Str(logType, logTypeAuthFailed).
Str(logPropertyIP, ip).
Send()
}
return false
}
func successResponse(
config *config,
clientPersistChecksum string,
aclRule string,
value string,
username string,
ttl int64,
ip string,
realCheck bool,
) bool {
if realCheck {
defer prometheusRequestAuthSuccess.With(prometheus.Labels{"acl": aclRule, "value": value}).Inc()
defer config.getLogger().
Info().
Str(logType, logTypeAuthSuccess).
Str(logPropertyIP, ip).
Str(logPropertyACL, aclRule).
Str(logPropertyValue, value).
Str(logPropertyUsername, username).
Send()
}
return true
}
func checkAuth(c *fiber.Ctx, config *config, realCheck bool) bool {
ttl := getConfigTTLSeconds(c)
persistChecksum := c.Locals(localVarClientPersistChecksum).(string)
ip := c.Locals(localVarIP).(string)
defer config.getLogger().
Info().
Str(logType, logTypeAuthCheck).
Str(logPropertyIP, ip).
Send()
// api keys
success, apiClientName := aclCheckAPIKeys(c)
if success {
return successResponse(config, persistChecksum, aclRuleAPI, apiClientName, "", ttl, ip, realCheck)
}
// country
success, countryCode := aclCheckCountries(c)
if success {
return successResponse(config, persistChecksum, aclRuleCountry, countryCode, "", ttl, ip, realCheck)
}
// cidr
success, cidr := aclCheckCIDRs(c)
if success {
return successResponse(config, persistChecksum, aclRuleCIDR, cidr, "", ttl, ip, realCheck)
}
// asn
success, asn := aclCheckASNs(c)
if success {
return successResponse(config, persistChecksum, aclRuleASN, asn, "", ttl, ip, realCheck)
}
// cookie check
cookieVar := c.Cookies(c.Get(httpRequestHeaderConfigCookie, defaultCookieName), "")
if cookieVar != "" {
cookieToken, cookieErr := newPersistTokenFromString(cookieVar, config.tokenSecret)
if cookieErr == nil {
return successResponse(config, persistChecksum, aclRuleChallenge, cookieToken.Type, cookieToken.Username, ttl, ip, realCheck)
}
}
return failedResponse(config, ip, realCheck)
}