-
Notifications
You must be signed in to change notification settings - Fork 4
/
options.go
78 lines (65 loc) · 1.82 KB
/
options.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
// Copyright 2016 Lars Wiegman. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.
package multipass
import (
"html/template"
"os"
"path"
"time"
"github.com/namsral/multipass/services/io"
)
// Option describes a functional option for configuring the Multipass
// instance.
type Option func(*Multipass)
// Basepath overrides the default base path of `/multipass`.
// The given basepath is made absolute before it is set.
func Basepath(basepath string) Option {
return func(m *Multipass) {
p := path.Join("/", path.Clean(basepath))
if p == "/" {
return
}
m.opts.Basepath = p
}
}
// Expires sets the maximum age for JWT tokens. When a token exceeds the maximum
// age it is no longer valid.
func Expires(d time.Duration) Option {
return func(m *Multipass) {
m.opts.Expires = d
}
}
// CSRF sets a bool wether to protect against CSRF attaks. The default is true.
func CSRF(b bool) Option {
return func(m *Multipass) {
m.opts.CSRF = b
}
}
// Template sets the template to use for generating the web interface.
func Template(t template.Template) Option {
return func(m *Multipass) {
m.opts.Template = t
}
}
// Service sets the UserService and overrides DefaultUserService.
func Service(s UserService) Option {
return func(m *Multipass) {
m.opts.Service = s
}
}
// parseOptions parses the given option functions and returns a configured
// Multipass instance.
func parseOptions(opts ...Option) *Multipass {
m := new(Multipass)
// set default for the options
m.opts.Expires = time.Hour * 24
m.opts.Basepath = "/multipass"
m.opts.Service = io.NewUserService(os.Stdout)
m.opts.Template = *loadTemplates()
m.opts.CSRF = true
// range over each option function and apply it to the instance.
for _, option := range opts {
option(m)
}
return m
}