/
config.go
73 lines (60 loc) · 1.47 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
package didcomauth
import (
"errors"
"net/http"
)
type CacheType int
const (
CacheTypeRedis CacheType = iota
CacheTypeMemory
)
const (
defaultRedisHost = "localhost:6379"
defaultAuthPath = "/auth"
defaultChallengePath = "/challenge"
defaultProtectedPath = "/protected"
defaultCommercioLCD = "http://localhost:1317"
)
// ProtectedMapping represents a URI resource handled under the DID-authenticated protected path.
type ProtectedMapping struct {
Methods []string
Path string
Handler http.HandlerFunc
}
// Config holds data regarding the didcomauth module configuration, such as redis host, Challenge and protected base
// URL path.
type Config struct {
RedisHost string
ProtectedBasePath string
ProtectedPaths []ProtectedMapping
JWTSecret string
CommercioLCD string
CacheType CacheType
CacheProvider cache
}
func (c *Config) Validate() error {
if c.RedisHost == "" {
c.RedisHost = defaultRedisHost
}
if c.ProtectedBasePath == "" {
c.ProtectedBasePath = defaultProtectedPath
}
if c.CommercioLCD == "" {
c.CommercioLCD = defaultCommercioLCD
}
if c.ProtectedPaths == nil {
return errors.New("no protected paths specificed")
}
if c.JWTSecret == "" {
return errors.New("jwt secret is empty")
}
switch c.CacheType {
case CacheTypeMemory:
c.CacheProvider = newMem()
case CacheTypeRedis:
c.CacheProvider = newRedis(c.RedisHost)
default:
return errors.New("cache type not recognized")
}
return nil
}