-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
144 lines (126 loc) · 3.65 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
package adapter
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/goccy/go-yaml"
"github.com/gorilla/sessions"
)
// Config is a configration for go-nginx-oauth2-adapter.
type Config struct {
Address string `yaml:"address" json:"address"`
Secrets []*string `yaml:"secrets" json:"secrets"`
SessionName string `yaml:"session_name" json:"session_name"`
Providers map[string]map[string]interface{} `yaml:"providers" json:"providers"`
AppRefreshInterval string `yaml:"app_refresh_interval" json:"app_refresh_interval"`
// set with -configtest option.
ConfigTest bool `yaml:"-" json:"-"`
// Fields are a subset of http.Cookie fields.
Cookie *CookieConfig `yaml:"cookie" json:"cookie"`
}
// CookieConfig is a configration for the cookie of HTTP.
type CookieConfig struct {
Path string `yaml:"path" json:"path"`
Domain string `yaml:"domain" json:"domain"`
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'.
// MaxAge>0 means Max-Age attribute present and given in seconds.
MaxAge int `yaml:"max_age" json:"max_age"`
Secure bool `yaml:"secure" json:"secure"`
HTTPOnly bool `yaml:"http_only" json:"http_only"`
SameSite string `yaml:"same_site" json:"same_site"`
}
// NewConfig returns a new config.
func NewConfig() *Config {
return &Config{
Address: ":18081",
Secrets: nil,
SessionName: "go-nginx-oauth2-session",
Providers: map[string]map[string]interface{}{},
AppRefreshInterval: "24h",
Cookie: &CookieConfig{
Path: "/",
MaxAge: 60 * 60 * 24 * 3,
Secure: true,
HTTPOnly: true,
SameSite: "lax",
},
}
}
// LoadYaml loads the config from yaml file.
func (c *Config) LoadYaml(filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
return yaml.Unmarshal(data, c)
}
// LoadEnv loads the config from the environment values.
func (c *Config) LoadEnv() error {
if v := os.Getenv("NGX_OMNIAUTH_SESSION_COOKIE_TIMEOUT"); v != "" {
i, err := strconv.Atoi(v)
if err != nil {
return err
}
c.Cookie.MaxAge = i
}
if v := os.Getenv("NGX_OMNIAUTH_SESSION_COOKIE_SECURE"); v != "" {
b, err := strconv.ParseBool(v)
if err != nil {
return err
}
c.Cookie.Secure = b
}
if v := os.Getenv("NGX_OMNIAUTH_SESSION_COOKIE_HTTP_ONLY"); v != "" {
b, err := strconv.ParseBool(v)
if err != nil {
return err
}
c.Cookie.HTTPOnly = b
}
if v := os.Getenv("NGX_OMNIAUTH_SESSION_SECRET"); v != "" {
for _, v := range strings.Split(v, ",") {
v := v
c.Secrets = append(c.Secrets, &v)
}
}
if v := os.Getenv("NGX_OMNIAUTH_SESSION_COOKIE_NAME"); v != "" {
c.SessionName = v
}
if v := os.Getenv("NGX_OMNIAUTH_APP_REFRESH_INTERVAL"); v != "" {
c.AppRefreshInterval = v
}
if v := os.Getenv("NGX_OMNIAUTH_ADDRESS"); v != "" {
c.Address = v
}
return nil
}
// Options returns the session config.
func (c *CookieConfig) Options() (*sessions.Options, error) {
if c == nil {
return &sessions.Options{}, nil
}
var sameSite http.SameSite
switch strings.ToLower(c.SameSite) {
case "", "default":
sameSite = http.SameSiteDefaultMode
case "lax":
sameSite = http.SameSiteLaxMode
case "strict":
sameSite = http.SameSiteStrictMode
case "none":
sameSite = http.SameSiteNoneMode
default:
return nil, fmt.Errorf("unknown same site option: %s", c.SameSite)
}
return &sessions.Options{
Path: c.Path,
Domain: c.Domain,
MaxAge: c.MaxAge,
Secure: c.Secure,
HttpOnly: c.HTTPOnly,
SameSite: sameSite,
}, nil
}