-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathsetup.go
101 lines (83 loc) · 2.47 KB
/
setup.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
package caddy
import (
"flag"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/tarent/loginsrv/logging"
"github.com/tarent/loginsrv/login"
// Import all backends, packaged with the caddy plugin
_ "github.com/tarent/loginsrv/htpasswd"
_ "github.com/tarent/loginsrv/httpupstream"
_ "github.com/tarent/loginsrv/oauth2"
_ "github.com/tarent/loginsrv/osiam"
)
func init() {
caddy.RegisterPlugin("login", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// setup configures a new loginsrv instance.
func setup(c *caddy.Controller) error {
logging.Set("info", true)
for c.Next() {
args := c.RemainingArgs()
config, err := parseConfig(c)
if err != nil {
return err
}
if config.Template != "" && !filepath.IsAbs(config.Template) {
config.Template = filepath.Join(httpserver.GetConfig(c).Root, config.Template)
}
if len(args) == 1 {
logging.Logger.Warnf("DEPRECATED: Please set the login path by parameter login_path and not as directive argument (%v:%v)", c.File(), c.Line())
config.LoginPath = path.Join(args[0], "/login")
}
if e, isset := os.LookupEnv("JWT_SECRET"); isset {
config.JwtSecret = e
} else {
os.Setenv("JWT_SECRET", config.JwtSecret)
}
loginHandler, err := login.NewHandler(config)
if err != nil {
return err
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return NewCaddyHandler(next, loginHandler, config)
})
}
return nil
}
func parseConfig(c *caddy.Controller) (*login.Config, error) {
cfg := login.DefaultConfig()
cfg.Host = ""
cfg.Port = ""
cfg.LogLevel = ""
fs := flag.NewFlagSet("loginsrv-config", flag.ContinueOnError)
cfg.ConfigureFlagSet(fs)
for c.NextBlock() {
// caddy prefers '_' in parameter names,
// so we map them to the '-' from the command line flags
// the replacement supports both, for backwards compatibility
name := strings.Replace(c.Val(), "_", "-", -1)
args := c.RemainingArgs()
if len(args) != 1 {
return cfg, fmt.Errorf("Wrong number of arguments for %v: %v (%v:%v)", name, args, c.File(), c.Line())
}
value := args[0]
f := fs.Lookup(name)
if f == nil {
return cfg, fmt.Errorf("Unknown parameter for login directive: %v (%v:%v)", name, c.File(), c.Line())
}
err := f.Value.Set(value)
if err != nil {
return cfg, fmt.Errorf("Invalid value for parameter %v: %v (%v:%v)", name, value, c.File(), c.Line())
}
}
return cfg, nil
}