-
Notifications
You must be signed in to change notification settings - Fork 0
/
doorman.go
419 lines (374 loc) · 11.9 KB
/
doorman.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package doorman
import (
"bytes"
"embed"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/fs"
"net/http"
"net/url"
"os"
"time"
"github.com/benbjohnson/clock"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/go-redis/redis/v8"
"go.uber.org/zap"
)
type operationMode string
type captchaMode string
const (
defaultTokenDuration = Duration(1 * time.Minute)
defaultAccessDuration = Duration(10 * time.Hour)
operationsModeToken = operationMode("token")
operationsModeOTP = operationMode("otp")
operationsModeLink = operationMode("link")
captchaNone = captchaMode("")
captchaMath = captchaMode("math")
captchaFull = captchaMode("full")
)
//go:embed webapp/dist/*
var embeddedAsset embed.FS
var (
validOpModes = map[operationMode]bool{
operationsModeOTP: true,
operationsModeToken: true,
operationsModeLink: true,
}
)
func (op *operationMode) isToken() bool {
return *op == operationsModeToken
}
func (op *operationMode) isOTP() bool {
return *op == operationsModeOTP
}
func (op *operationMode) isLink() bool {
return *op == operationsModeLink
}
func init() {
caddy.RegisterModule(MiddlewareApp{})
caddy.RegisterModule(Middleware{})
}
type Duration time.Duration
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
*d = Duration(time.Duration(value))
return nil
case string:
tmp, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(tmp)
return nil
default:
return errors.New("invalid duration")
}
}
type TypedPlugin struct {
Type string `json:"type"`
Name string `json:"name"`
Spec json.RawMessage `json:"spec"`
}
type Plugins []TypedPlugin
type RedisConfig struct {
Address string `json:"address"`
Password string `json:"password,omitempty"`
DB int `json:"db,omitempty"`
client *redis.Client
}
type MessengerConfig struct {
Burst int `json:"burst"`
Rate Duration `json:"rate"`
From struct {
Name string `json:"name"`
EMail string `json:"email"`
} `json:"from"`
Transports Plugins `json:"transports"`
}
type StoreSettings struct {
PersistentType string `json:"persistent_type"`
MemCacheMB int `json:"memory_cache_mb"`
Redis *RedisConfig `json:"redis,omitempty"`
OTP struct {
Timeout Duration `json:"timeout,omitempty"`
Transport *TypedPlugin `json:"transport,omitempty"`
transport messageTransport
} `json:"otp"`
}
// MiddlewareApp implements an HTTP handler
type MiddlewareApp struct {
Users Plugins `json:"users,omitempty"`
Whitelist Plugins `json:"whitelist,omitempty"`
CookieHash []byte `json:"cookie_hash"`
CookieBlock []byte `json:"cookie_block"`
InsecureCookie bool `json:"insecure_cookie,omitempty"`
Domain string `json:"domain,omitempty"`
Issuer string `json:"issuer,omitempty"`
IssuerBase string `json:"issuer_base"`
Spacing string `json:"spacing,omitempty"`
OperationMode operationMode `json:"operation_mode"`
CaptchaMode captchaMode `json:"captcha_mode"`
Channels []string `json:"channels"`
AccessDuration Duration `json:"access_duration"`
TokenDuration Duration `json:"token_duration"`
Messenger MessengerConfig `json:"messenger_config"`
StoreSettings StoreSettings `json:"store_settings"`
ImprintURL string `json:"imprint_url"`
PrivacyPolicyURL string `json:"privacy_policy_url"`
logger *zap.Logger
store *persistentStore
secCookie *cookieHandler
clock clock.Clock
assets http.Handler
assetsDir http.FileSystem
transporters transporters
userbackends *userBackends
whitelister *whitelister
authHost string
}
// CaddyModule returns the Caddy module information.
func (MiddlewareApp) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "doorman",
New: func() caddy.Module { return new(MiddlewareApp) },
}
}
func (m *MiddlewareApp) Start() error {
return nil
}
func (m *MiddlewareApp) Stop() error {
return nil
}
// Provision implements caddy.Provisioner.
func (m *MiddlewareApp) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger(m)
m.clock = clock.New()
var rcli *redis.Client
if m.StoreSettings.Redis != nil {
rcli = redis.NewClient(&redis.Options{
Addr: m.StoreSettings.Redis.Address,
Password: m.StoreSettings.Redis.Password,
DB: m.StoreSettings.Redis.DB,
})
m.StoreSettings.Redis.client = rcli
}
if m.StoreSettings.OTP.Transport != nil {
t, err := fromTransportSpec(m.logger, *m.StoreSettings.OTP.Transport)
if err != nil {
return err
}
m.StoreSettings.OTP.transport = t
}
if m.StoreSettings.OTP.Timeout == 0 {
m.StoreSettings.OTP.Timeout = Duration(15 * time.Minute)
}
m.secCookie = newCookie(m.logger, m.CookieHash, m.CookieBlock, m.InsecureCookie, m.Domain)
// we check if there is a local directory named "webapp/dist". when developing the
// code, we have this directory and so we can easily change the HTML/JS code and test
// it.
if _, err := os.Stat("webapp/dist"); os.IsNotExist(err) {
// no local assets in directory, use embedded ones
sub, err := fs.Sub(embeddedAsset, "webapp/dist")
if err == nil {
m.assetsDir = http.FS(sub)
goto assetsInitialized
}
}
m.assetsDir = http.Dir("webapp/dist")
assetsInitialized:
m.assets = http.FileServer(m.assetsDir)
if m.AccessDuration == 0 {
m.AccessDuration = defaultAccessDuration
}
if m.TokenDuration == 0 {
m.TokenDuration = defaultTokenDuration
}
if m.OperationMode == "" {
m.OperationMode = operationsModeToken
}
trsp, err := fromTransportSpecs(m.logger, m.Messenger.Transports)
if err != nil {
return fmt.Errorf("cannot create messenger: %w", err)
}
m.transporters = trsp
ub, err := fromUserSpecs(m.logger, m.Users)
if err != nil {
return fmt.Errorf("cannot create user backends: %w", err)
}
m.userbackends = ub
ws, err := fromWhitelistSpecs(m.logger, m.Whitelist)
if err != nil {
return fmt.Errorf("cannot create whitelist loaders: %w", err)
}
m.whitelister = ws
store, err := newStore(m.logger, m.clock, m.StoreSettings, ws)
if err != nil {
return fmt.Errorf("cannot initialize store: %w", err)
}
m.store = store
m.logger.Info("configdata", zap.Any("config", *m))
return nil
}
// Validate implements caddy.Validator.
func (m *MiddlewareApp) Validate() error {
if len(m.CookieBlock) != 32 {
return fmt.Errorf("the value for cookie_block must be 32 bytes, for example: %v", base64.StdEncoding.EncodeToString(newRandomKey(32)))
}
if len(m.CookieHash) != 64 {
return fmt.Errorf("the value for cookie_hash must be 64 bytes, for example: %v", base64.StdEncoding.EncodeToString(newRandomKey(64)))
}
if _, valid := validOpModes[m.OperationMode]; !valid {
return fmt.Errorf("invalid operation mode: %s", m.OperationMode)
}
if m.OperationMode.isOTP() {
if !m.canDoOTP() {
return fmt.Errorf("OTP operations configured, but no transport configured")
}
if m.Issuer == "" {
return fmt.Errorf("when using OTP, you must set issuer")
}
}
if m.IssuerBase == "" {
return fmt.Errorf("you must specify the issuer_base as a base url, aka https://www.example.com")
}
if u, e := url.Parse(m.IssuerBase); e != nil {
return fmt.Errorf("you must specify the issuer_base as a base url, aka https://www.example.com: %w", e)
} else {
m.authHost = u.Host
}
return nil
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *MiddlewareApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
}
return nil
}
func (m *MiddlewareApp) searchUser(uid string) (*UserEntry, error) {
ue, err := m.userbackends.Search(m.logger, uid)
if err == nil {
m.logger.Info("found user", zap.Any("user", ue))
}
return ue, err
}
func (m *MiddlewareApp) sendMessage(usr *UserEntry, subject, msg, body string) error {
m.logger.Info("send message", zap.Any("transporters", m.transporters))
for _, c := range m.Channels {
t, ok := m.transporters[c]
if !ok {
continue
}
a := addressable{
FromMail: m.Messenger.From.EMail,
FromName: m.Messenger.From.Name,
ToMail: usr.EMail,
ToMobile: usr.SMSNumber(),
}
res, err := t.Send(m.logger, a, subject, msg, body)
if err != nil {
m.logger.Error("error with messenger", zap.String("output", res), zap.Error(err))
return err
}
m.logger.Info("message to user sent", zap.String("output", res))
return nil
}
return fmt.Errorf("no message transport found for channels: %v", m.Channels)
}
func (m *MiddlewareApp) createTempRegistration(log *zap.Logger, uid string) (string, error) {
return m.store.tokensrv.newTempRegistration(log, m.Issuer, uid)
}
func (m *MiddlewareApp) checkTempRegistration(log *zap.Logger, uid string) error {
return m.store.tokensrv.checkTempRegistration(log, m.Issuer, uid)
}
func (m *MiddlewareApp) sendOTPRegistration(uid, email, mobile, regkey string) error {
authlink := fmt.Sprintf("%s/#/signup/%s/%s?%s=1", m.IssuerBase, url.QueryEscape(uid), url.QueryEscape(regkey), dmrequest)
m.logger.Info("send otp registration", zap.String("email", email), zap.String("link", authlink))
var body bytes.Buffer
a := addressable{
FromMail: m.Messenger.From.EMail,
FromName: m.Messenger.From.Name,
ToMail: email,
ToMobile: mobile,
}
data := map[string]string{
"Registrationlink": authlink,
"Uid": uid,
"EMail": email,
"Imprint": m.ImprintURL,
"PrivacyPolicy": m.PrivacyPolicyURL,
"Sent": time.Now().UTC().Format(time.RFC3339),
}
err := otpRegisterNotification.Execute(&body, data)
if err != nil {
return fmt.Errorf("cannot generate email with template: %w", err)
}
res, err := m.StoreSettings.OTP.transport.Send(m.logger, a, "Your registration", authlink, body.String())
if err != nil {
m.logger.Error("error with otp messenger", zap.String("output", res), zap.Error(err))
return err
}
m.logger.Info("message to user sent", zap.String("output", res))
return nil
}
func (m *MiddlewareApp) allowUserIP(issuer, userid, clip string) {
if err := m.store.allowUserIP(m.logger, clip, time.Duration(m.AccessDuration)); err != nil {
m.logger.Error("cannot allow userip", zap.Error(err))
}
m.store.tokensrv.removeTempToken(m.logger, issuer, userid)
}
func (m *MiddlewareApp) canDoOTP() bool {
return m.StoreSettings.OTP.Transport != nil
}
type Middleware struct {
app *MiddlewareApp
}
func (Middleware) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.doorman",
New: func() caddy.Module { return new(Middleware) },
}
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
clip := findClientIP(r)
ipallowed := m.app.store.isAllowed(clip)
if m.app.IsAppRequest(r) {
m.app.logger.Debug("app request", zap.String("clientip", clip), zap.Bool("ipallowed", ipallowed), zap.String("url", r.URL.String()))
m.app.ServeApp(w, r, clip)
return nil
}
m.app.logger.Debug("check if access is allowed", zap.String("clientip", clip), zap.Bool("ipallowed", ipallowed))
if ipallowed {
return next.ServeHTTP(w, r)
}
m.app.ServeApp(w, r, clip)
return nil
}
func (m *Middleware) Provision(ctx caddy.Context) error {
dm, err := ctx.App("doorman")
if err != nil {
return err
}
m.app = dm.(*MiddlewareApp)
return nil
}
// Interface guards
var (
_ caddy.Provisioner = (*MiddlewareApp)(nil)
_ caddy.Validator = (*MiddlewareApp)(nil)
_ caddyhttp.MiddlewareHandler = (*Middleware)(nil)
_ caddyfile.Unmarshaler = (*MiddlewareApp)(nil)
_ caddy.App = (*MiddlewareApp)(nil)
)