-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
37 lines (34 loc) · 854 Bytes
/
api.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 common
import (
"os"
"regexp"
"strings"
)
func VerifyApiKey(api_key string) (host string, err error) {
isApiEnabled := Config.Get("features.api.enable").Bool()
allowedApiKeys := Config.Get("features.api.api_key").String()
if isApiEnabled == false {
return "", NewError("Api is not enabled", 503)
} else if api_key == os.Getenv("API_KEY") {
return "*", nil
}
lines := strings.Split(allowedApiKeys, "\n")
for _, line := range lines {
line = regexp.MustCompile(` #.*`).ReplaceAllString(line, "") // remove comment
chunks := strings.SplitN(line, " ", 2)
if len(chunks) == 0 {
continue
} else if chunks[0] != api_key {
continue
}
if len(chunks) == 1 {
return "", nil
}
chunks[1] = strings.TrimSpace(chunks[1])
if chunks[1] == "" {
return "*", nil
}
return chunks[1], nil
}
return "", ErrNotValid
}