-
Notifications
You must be signed in to change notification settings - Fork 21
/
conf.go
120 lines (103 loc) · 2.72 KB
/
conf.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
package main
import (
"errors"
"gopkg.in/yaml.v1"
"io/ioutil"
"github.com/martini-contrib/oauth2"
)
const (
noAuthServiceName = "nothing" // for testing only (undocumented)
)
type Conf struct {
Addr string `yaml:"address"`
SSL SSLConf `yaml:"ssl"`
Auth AuthConf `yaml:"auth"`
Restrictions []string `yaml:"restrictions"`
Proxies []ProxyConf `yaml:"proxy"`
Paths PathConf `yaml:"paths"`
Htdocs string `yaml:"htdocs"`
}
type SSLConf struct {
Cert string `yaml:"cert"`
Key string `yaml:"key"`
}
type AuthConf struct {
Session AuthSessionConf `yaml:"session"`
Info AuthInfoConf `yaml:"info"`
}
type AuthSessionConf struct {
Key string `yaml:"key"`
CookieDomain string `yaml:"cookie_domain"`
}
type AuthInfoConf struct {
Service string `yaml:"service"`
ClientId string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
RedirectURL string `yaml:"redirect_url"`
Endpoint string `yaml:"endpoint"`
ApiEndpoint string `yaml:"api_endpoint"`
}
type ProxyConf struct {
Path string `yaml:"path"`
Dest string `yaml:"dest"`
Strip bool `yaml:"strip_path"`
Host string `yaml:"host"`
}
type PathConf struct {
Login string `yaml:"login"`
Logout string `yaml:"logout"`
Callback string `yaml:"callback"`
Error string `yaml:"error"`
}
func ParseConf(path string) (*Conf, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
c := &Conf{}
if err := yaml.Unmarshal(data, c); err != nil {
return nil, err
}
if c.Addr == "" {
return nil, errors.New("address config is required")
}
if c.Auth.Session.Key == "" {
return nil, errors.New("auth.session.key config is required")
}
if c.Auth.Info.Service == "" {
return nil, errors.New("auth.info.service config is required")
}
if c.Auth.Info.ClientId == "" {
return nil, errors.New("auth.info.client_id config is required")
}
if c.Auth.Info.ClientSecret == "" {
return nil, errors.New("auth.info.client_secret config is required")
}
if c.Auth.Info.RedirectURL == "" {
return nil, errors.New("auth.info.redirect_url config is required")
}
if c.Htdocs == "" {
c.Htdocs = "."
}
if c.Auth.Info.Service == "github" && c.Auth.Info.Endpoint == "" {
c.Auth.Info.Endpoint = "https://github.com"
}
if c.Auth.Info.Service == "github" && c.Auth.Info.ApiEndpoint == "" {
c.Auth.Info.ApiEndpoint = "https://api.github.com"
}
return c, nil
}
func (c *Conf) SetOAuth2Paths() {
if c.Paths.Login != "" {
oauth2.PathLogin = c.Paths.Login
}
if c.Paths.Logout != "" {
oauth2.PathLogout = c.Paths.Logout
}
if c.Paths.Callback != "" {
oauth2.PathCallback = c.Paths.Callback
}
if c.Paths.Error != "" {
oauth2.PathError = c.Paths.Error
}
}