-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
config.go
146 lines (124 loc) · 4.2 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package service
import (
"fmt"
"time"
"github.com/plgd-dev/hub/v2/pkg/config"
"github.com/plgd-dev/hub/v2/pkg/config/property/urischeme"
"github.com/plgd-dev/hub/v2/pkg/log"
"github.com/plgd-dev/hub/v2/pkg/net/http"
"github.com/plgd-dev/hub/v2/pkg/net/http/server"
"github.com/plgd-dev/hub/v2/pkg/net/listener"
)
type AsymmetricKey struct {
PrivateFile string
PublicFile string
}
type AccessTokenType string
const AccessTokenType_JWT AccessTokenType = "jwt"
type AllowedGrantType string
const (
AllowedGrantType_AUTHORIZATION_CODE AllowedGrantType = "authorization_code"
AllowedGrantType_CLIENT_CREDENTIALS AllowedGrantType = "client_credentials"
AllowedGrantType_PASSWORD AllowedGrantType = "password"
AllowedGrantType_REFRESH_TOKEN AllowedGrantType = "refresh_token"
)
type Client struct {
ID string `yaml:"id"`
ClientSecret string `yaml:"secret"`
AuthorizationCodeLifetime time.Duration `yaml:"authorizationCodeLifetime"`
AccessTokenLifetime time.Duration `yaml:"accessTokenLifetime"`
CodeRestrictionLifetime time.Duration `yaml:"codeRestrictionLifetime"`
RefreshTokenRestrictionLifetime time.Duration `yaml:"refreshTokenRestrictionLifetime"`
ConsentScreenEnabled bool `yaml:"consentScreenEnabled"`
RequireIssuedAuthorizationCode bool `yaml:"requireIssuedAuthorizationCode"`
RequiredScope []string `yaml:"requiredScope"`
RequiredResponseType string `yaml:"requiredResponseType"`
RequiredRedirectURI string `yaml:"requiredRedirectURI"`
}
func (c *Client) Validate() error {
if c.ID == "" {
return fmt.Errorf("id('%v')", c.ID)
}
return nil
}
type OAuthClientsConfig []*Client
func (c OAuthClientsConfig) Find(id string) *Client {
for _, client := range c {
if client.ID == id {
return client
}
}
return nil
}
type ClientsConfig struct {
OpenTelemetryCollector http.OpenTelemetryCollectorConfig `yaml:"openTelemetryCollector" json:"openTelemetryCollector"`
}
func (c *ClientsConfig) Validate() error {
if err := c.OpenTelemetryCollector.Validate(); err != nil {
return fmt.Errorf("openTelemetryCollector.%w", err)
}
return nil
}
// Config represents application configuration
type Config struct {
Log log.Config `yaml:"log" json:"log"`
APIs APIsConfig `yaml:"apis" json:"apis"`
Clients ClientsConfig `yaml:"clients" json:"clients"`
OAuthSigner OAuthSignerConfig `yaml:"oauthSigner" json:"oauthSigner"`
}
func (c *Config) Validate() error {
if err := c.Log.Validate(); err != nil {
return fmt.Errorf("log.%w", err)
}
if err := c.APIs.Validate(); err != nil {
return fmt.Errorf("apis.%w", err)
}
if err := c.Clients.Validate(); err != nil {
return fmt.Errorf("clients.%w", err)
}
if err := c.OAuthSigner.Validate(); err != nil {
return fmt.Errorf("oauthSigner.%w", err)
}
return nil
}
// Config represent application configuration
type APIsConfig struct {
HTTP HTTPConfig `yaml:"http" json:"http"`
}
func (c *APIsConfig) Validate() error {
if err := c.HTTP.Validate(); err != nil {
return fmt.Errorf("http.%w", err)
}
return nil
}
type HTTPConfig struct {
Connection listener.Config `yaml:",inline" json:",inline"`
Server server.Config `yaml:",inline" json:",inline"`
}
func (c *HTTPConfig) Validate() error {
return c.Connection.Validate()
}
type OAuthSignerConfig struct {
IDTokenKeyFile urischeme.URIScheme `yaml:"idTokenKeyFile" json:"idTokenKeyFile"`
AccessTokenKeyFile urischeme.URIScheme `yaml:"accessTokenKeyFile" json:"accessTokenKeyFile"`
Domain string `yaml:"domain" json:"domain"`
Clients OAuthClientsConfig `yaml:"clients" json:"clients"`
}
func (c *OAuthSignerConfig) Validate() error {
if c.IDTokenKeyFile == "" {
return fmt.Errorf("idTokenKeyFile('%v')", c.IDTokenKeyFile)
}
if c.AccessTokenKeyFile == "" {
return fmt.Errorf("accessTokenKeyFile('%v')", c.AccessTokenKeyFile)
}
if c.Domain == "" {
return fmt.Errorf("domain('%v')", c.Domain)
}
if len(c.Clients) == 0 {
return fmt.Errorf("clients('%v')", c.Clients)
}
return nil
}
func (c Config) String() string {
return config.ToString(c)
}