-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
112 lines (89 loc) · 3.02 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
111
112
package corbado
import (
"net/http"
"os"
"time"
"github.com/pkg/errors"
"github.com/corbado/corbado-go/v2/internal/assert"
"github.com/corbado/corbado-go/v2/pkg/generated/api"
)
type Config struct {
ProjectID string
APISecret string
FrontendAPI string
BackendAPI string
CacheMaxAge time.Duration
JWKSRefreshInterval time.Duration
JWKSRefreshRateLimit time.Duration
JWKSRefreshTimeout time.Duration
HTTPClient *http.Client
ExtraClientOptions []api.ClientOption
}
const (
configDefaultCacheMaxAge = time.Minute
configDefaultJWKSRefreshInterval = time.Hour
configDefaultJWKSRefreshRateLimit = 5 * time.Minute
configDefaultJWKSRefreshTimeout = 10 * time.Second
)
// NewConfig returns new config with sane defaults
func NewConfig(projectID string, apiSecret string, frontendAPI string, backendAPI string) (*Config, error) {
if err := assert.StringNotEmpty(projectID); err != nil {
return nil, err
}
if err := assert.StringNotEmpty(apiSecret); err != nil {
return nil, err
}
if err := assert.StringNotEmpty(frontendAPI); err != nil {
return nil, err
}
if err := assert.StringNotEmpty(backendAPI); err != nil {
return nil, err
}
return &Config{
ProjectID: projectID,
APISecret: apiSecret,
FrontendAPI: frontendAPI,
BackendAPI: backendAPI,
CacheMaxAge: configDefaultCacheMaxAge,
JWKSRefreshInterval: configDefaultJWKSRefreshInterval,
JWKSRefreshRateLimit: configDefaultJWKSRefreshRateLimit,
JWKSRefreshTimeout: configDefaultJWKSRefreshTimeout,
}, nil
}
// NewConfigFromEnv created new config be reading the following environment variables: CORBADO_PROJECT_ID,
// CORBADO_API_SECRET, CORBADO_FRONTEND_API and CORBADO_BACKEND_API
func NewConfigFromEnv() (*Config, error) {
return NewConfig(
os.Getenv("CORBADO_PROJECT_ID"),
os.Getenv("CORBADO_API_SECRET"),
os.Getenv("CORBADO_FRONTEND_API"),
os.Getenv("CORBADO_BACKEND_API"),
)
}
func (c *Config) validate() error {
if err := assert.ValidProjectID(c.ProjectID); err != nil {
return errors.WithMessage(err, "Invalid ProjectID given")
}
if err := assert.ValidAPISecret(c.APISecret); err != nil {
return errors.WithMessage(err, "Invalid APISecret given")
}
if err := assert.ValidAPIEndpoint(c.FrontendAPI); err != nil {
return errors.WithMessage(err, "Invalid FrontendAPI given")
}
if err := assert.ValidAPIEndpoint(c.BackendAPI); err != nil {
return errors.WithMessage(err, "Invalid BackendAPI given")
}
if err := assert.DurationNotEmpty(c.CacheMaxAge); err != nil {
return errors.WithMessage(err, "Invalid CacheMaxAge given")
}
if err := assert.DurationNotEmpty(c.JWKSRefreshInterval); err != nil {
return errors.WithMessage(err, "Invalid JWKSRefreshInterval given")
}
if err := assert.DurationNotEmpty(c.JWKSRefreshRateLimit); err != nil {
return errors.WithMessage(err, "Invalid JWKSRefreshRateLimit given")
}
if err := assert.DurationNotEmpty(c.JWKSRefreshTimeout); err != nil {
return errors.WithMessage(err, "Invalid JWKSRefreshTimeout given")
}
return nil
}