This repository has been archived by the owner on May 28, 2022. It is now read-only.
/
config.go
110 lines (92 loc) · 2.05 KB
/
config.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package ingester
import (
"encoding/json"
"errors"
"os"
"strings"
)
type Configuration []ProviderConfig
type ProviderConfig struct {
Name string `json:"name"`
AppID string `json:"app_id"`
VerifierConfig VerifierConfig `json:"verifier_config"`
}
type VerifierConfig struct {
*HmacConfig
*BasicAuthConfig
*APIKeyConfig
*IPAddressConfig
}
type HmacConfig struct {
Header string `json:"header"`
Hash string `json:"hash"`
Secret string `json:"secret"`
}
type BasicAuthConfig struct {
Username string `json:"username"`
Password string `json:"password"`
}
type APIKeyConfig struct {
Header string `json:"header"`
APIKey string `json:"api_key"`
}
type IPAddressConfig struct {
IPSafelist []string `json:"ip_safelist"`
}
func (vC *VerifierConfig) UnmarshalJSON(data []byte) error {
temp := struct {
Type string `json:"type"`
}{}
if err := json.Unmarshal(data, &temp); err != nil {
return err
}
// Invalidate all others so it won't affect.
vC.HmacConfig = nil
vC.APIKeyConfig = nil
vC.BasicAuthConfig = nil
vC.IPAddressConfig = nil
switch temp.Type {
case "hmac":
var c HmacConfig
if err := json.Unmarshal(data, &c); err != nil {
return err
}
vC.HmacConfig = &c
return nil
case "api_key":
var c APIKeyConfig
if err := json.Unmarshal(data, &c); err != nil {
return err
}
vC.APIKeyConfig = &c
return nil
case "basic_auth":
var c BasicAuthConfig
if err := json.Unmarshal(data, &c); err != nil {
return err
}
vC.BasicAuthConfig = &c
return nil
case "ip_address":
var c IPAddressConfig
if err := json.Unmarshal(data, &c); err != nil {
return err
}
vC.IPAddressConfig = &c
return nil
default:
//TODO(subomi): rewrite this to an error type
return errors.New("Invalid verification config")
}
}
func LoadConfig(env string) error {
f := os.Getenv(env)
if len(strings.TrimSpace(f)) == 0 {
return errors.New("Configuration cannot be empty")
}
s := strings.NewReader(f)
if err := json.NewDecoder(s).Decode(&configStore); err != nil {
return err
}
return nil
}