authserver.go 16.8 KB
Newer Older
ale's avatar
ale committed
1
2
3
4
package server

import (
	"context"
5
	"errors"
ale's avatar
ale committed
6
7
8
9
10
11
12
	"fmt"
	"io/ioutil"
	"log"
	"path/filepath"
	"strings"

	"github.com/pquerna/otp/totp"
ale's avatar
ale committed
13
	"github.com/prometheus/client_golang/prometheus"
ale's avatar
ale committed
14
15
16
	"github.com/tstranex/u2f"
	"gopkg.in/yaml.v2"

ale's avatar
ale committed
17
	"git.autistici.org/ai3/go-common/clientutil"
ale's avatar
ale committed
18
	"git.autistici.org/ai3/go-common/pwhash"
ale's avatar
ale committed
19
20
21
22
23
24
25
26
27
	"git.autistici.org/id/auth"
)

// User contains the attributes of a user account as relevant to the
// authentication server. It is only used internally, to communicate
// between the authserver and its storage backends.
type User struct {
	Name                 string
	Email                string
ale's avatar
ale committed
28
	Shard                string
ale's avatar
ale committed
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
	EncryptedPassword    []byte
	TOTPSecret           string
	U2FRegistrations     []u2f.Registration
	AppSpecificPasswords []*AppSpecificPassword
	Groups               []string
}

// AppSpecificPassword is a password tied to a single service.
type AppSpecificPassword struct {
	Service           string
	EncryptedPassword []byte
}

// Has2FA returns true if the user supports any 2FA method.
func (u *User) Has2FA() bool {
	return u.HasU2F() || u.HasOTP()
}

// HasOTP returns true if the user supports (T)OTP.
func (u *User) HasOTP() bool {
	return u.TOTPSecret != ""
}

// HasU2F returns true if the user supports U2F.
func (u *User) HasU2F() bool {
	return len(u.U2FRegistrations) > 0
}

// UserInfo returns extra user information in the format required by
// the auth wire protocol.
func (u *User) UserInfo() *auth.UserInfo {
	return &auth.UserInfo{
		Email:  u.Email,
ale's avatar
ale committed
62
		Shard:  u.Shard,
ale's avatar
ale committed
63
64
65
66
67
68
69
70
71
72
		Groups: u.Groups,
	}
}

// UserBackend provides us with per-service user information.
type UserBackend interface {
	Close()
	GetUser(context.Context, *BackendSpec, string) (*User, bool)
}

73
74
75
76
77
78
79
// OTPShortTermStorage stores short-term otp tokens for replay
// protection purposes.
type OTPShortTermStorage interface {
	AddToken(string, string) error
	HasToken(string, string) bool
}

ale's avatar
ale committed
80
81
82
83
84
85
// U2FShortTermStorage stores short-term u2f challenges.
type U2FShortTermStorage interface {
	SetUserChallenge(string, *u2f.Challenge) error
	GetUserChallenge(string) (*u2f.Challenge, bool)
}

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
type ratelimitKeyFunc func(*User, *auth.Request) string

func usernameKey(user *User, _ *auth.Request) string {
	return user.Name
}

func ipAddrKey(_ *User, req *auth.Request) string {
	if req.DeviceInfo != nil {
		return req.DeviceInfo.RemoteAddr
	}
	return ""
}

type authRatelimiterConfig struct {
	Limit         int      `yaml:"limit"`
	Period        int      `yaml:"period"`
	BlacklistTime int      `yaml:"blacklist_for"`
	OnFailure     bool     `yaml:"on_failure"`
	Keys          []string `yaml:"keys"`

	keyFuncs []ratelimitKeyFunc
}

func (r *authRatelimiterConfig) compile() error {
	for _, k := range r.Keys {
		var f ratelimitKeyFunc
		switch k {
		case "ip":
			f = ipAddrKey
		case "user":
			f = usernameKey
		default:
			return fmt.Errorf("unknown key %s", k)
		}
		r.keyFuncs = append(r.keyFuncs, f)
	}
	return nil
}

const rlKeySep = ";"

func (r *authRatelimiterConfig) key(user *User, req *auth.Request) string {
	if len(r.keyFuncs) == 1 {
		return r.keyFuncs[0](user, req)
	}

	var parts []string
	for _, f := range r.keyFuncs {
		parts = append(parts, f(user, req))
	}
	return strings.Join(parts, rlKeySep)
}

type authRatelimiter struct {
	*authRatelimiterConfig
	rl *Ratelimiter
}

func (r *authRatelimiter) AllowIncr(user *User, req *auth.Request) bool {
	return r.rl.AllowIncr(r.key(user, req))
}

type authBlacklist struct {
	*authRatelimiterConfig
	bl *Blacklist
}

func (b *authBlacklist) Allow(user *User, req *auth.Request) bool {
	return b.bl.Allow(b.key(user, req))
}

func (b *authBlacklist) Incr(user *User, req *auth.Request, resp *auth.Response) {
	if b.OnFailure && resp.Status == auth.StatusOK {
		return
	}
	b.bl.Incr(b.key(user, req))
}

164
165
166
type requestFilter interface {
	Filter(*User, *auth.Request, *auth.Response) *auth.Response
}
167

ale's avatar
ale committed
168
169
// BackendSpec specifies backend-specific configuration for a service.
type BackendSpec struct {
170
171
172
	LDAPSpec     *LDAPServiceConfig `yaml:"ldap"`
	FileSpec     string             `yaml:"file"`
	StaticGroups []string           `yaml:"static_groups"`
ale's avatar
ale committed
173
174
}

175
// ServiceConfig defines the authentication backends for a service.
ale's avatar
ale committed
176
type ServiceConfig struct {
177
178
179
180
	BackendSpecs         []*BackendSpec `yaml:"backends"`
	ChallengeResponse    bool           `yaml:"challenge_response"`
	Enforce2FA           bool           `yaml:"enforce_2fa"`
	EnableDeviceTracking bool           `yaml:"enable_device_tracking"`
181
182
183
184
185

	Ratelimits []string `yaml:"rate_limits"`

	rl      []*authRatelimiter
	bl      []*authBlacklist
186
	filters []requestFilter
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
}

func (c *ServiceConfig) checkRateLimits(user *User, req *auth.Request) bool {
	for _, rl := range c.rl {
		if !rl.AllowIncr(user, req) {
			return false
		}
	}
	for _, bl := range c.bl {
		if !bl.Allow(user, req) {
			return false
		}
	}
	return true
}

func (c *ServiceConfig) notifyBlacklists(user *User, req *auth.Request, resp *auth.Response) {
	for _, bl := range c.bl {
		bl.Incr(user, req, resp)
	}
ale's avatar
ale committed
207
208
}

209
type shortTermStorageConfig struct {
210
211
212
	Servers []string `yaml:"memcache_servers"`
}

ale's avatar
ale committed
213
214
215
216
217
218
219
220
221
222
223
// Config for the authentication server.
type Config struct {
	// Global configuration for backends.
	LDAPConfig *LDAPConfig `yaml:"ldap_config"`

	// List of enabled backends.
	EnabledBackends []string `yaml:"enabled_backends"`

	// Service-specific configuration.
	Services map[string]*ServiceConfig `yaml:"services"`

224
225
226
	// If set, load more service definitions from *.yml files in this directory.
	ServicesDir string `yaml:"services_dir"`

227
228
229
	// Named rate limiter configurations.
	RateLimiters map[string]*authRatelimiterConfig `yaml:"rate_limits"`

230
	// Configuration for the user-meta-server backend.
ale's avatar
ale committed
231
	UserMetaDBConfig *clientutil.BackendConfig `yaml:"user_meta_server"`
232

233
234
	// Configuration for the U2F short-term challenge storage
	// (backed by memcached).
235
236
237
238
239
	U2FShortTerm *shortTermStorageConfig `yaml:"u2f_short_term_storage"`

	// Configuration for the OTP short-term replay protection
	// storage (backed by memcached).
	OTPShortTerm *shortTermStorageConfig `yaml:"otp_short_term_storage"`
240

241
242
243
244
245
	// Runtime versions of the above. These objects are shared by
	// all services, as they contain the actual map data.
	rl map[string]*Ratelimiter
	bl map[string]*Blacklist

ale's avatar
ale committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
	path string
}

func (c *Config) walkBackendSpecs(f func(*BackendSpec) error) error {
	for _, svc := range c.Services {
		for _, spec := range svc.BackendSpecs {
			if err := f(spec); err != nil {
				return err
			}
		}
	}
	return nil
}

func (c *Config) relativePath(path string) string {
	if strings.HasPrefix(path, "/") {
		return path
	}
	return filepath.Join(filepath.Dir(c.path), path)
}

267
func (c *Config) compile() error {
268
	// Build the global rate limiters and blacklists.
269
270
271
272
273
274
275
276
277
278
279
280
281
	c.rl = make(map[string]*Ratelimiter)
	c.bl = make(map[string]*Blacklist)
	for name, params := range c.RateLimiters {
		if err := params.compile(); err != nil {
			return err
		}
		if params.BlacklistTime > 0 {
			c.bl[name] = newBlacklist(params.Limit, params.Period, params.BlacklistTime)
		} else {
			c.rl[name] = newRatelimiter(params.Limit, params.Period)
		}
	}

282
	// Compile each service definition.
283
284
285
286
287
288
289
290
291
292
293
294
295
296
	for _, sc := range c.Services {
		for _, name := range sc.Ratelimits {
			config, ok := c.RateLimiters[name]
			if !ok {
				return fmt.Errorf("unknown rate limiter %s", name)
			}
			if rl, ok := c.rl[name]; ok {
				sc.rl = append(sc.rl, &authRatelimiter{config, rl})
			} else if bl, ok := c.bl[name]; ok {
				sc.bl = append(sc.bl, &authBlacklist{config, bl})
			} else {
				panic("can't find rl/bl")
			}
		}
297
298
299
300
301
302
303
304
305
306
307

		if sc.EnableDeviceTracking {
			if c.UserMetaDBConfig == nil {
				return errors.New("usermetadb config is missing")
			}
			dt, err := newDeviceFilter(c.UserMetaDBConfig)
			if err != nil {
				return err
			}
			sc.filters = append(sc.filters, dt)
		}
308
309
310
311
312
	}

	return nil
}

313
314
315
// Load a standalone service configuration: a YAML-encoded file that
// may contain one or more ServiceConfig definitions.
func loadStandaloneServiceConfig(path string) (map[string]*ServiceConfig, error) {
316
	data, err := ioutil.ReadFile(path) // #nosec
317
318
319
320
321
322
323
324
325
326
	if err != nil {
		return nil, err
	}
	var out map[string]*ServiceConfig
	if err = yaml.Unmarshal(data, &out); err != nil {
		return nil, err
	}
	return out, nil
}

ale's avatar
ale committed
327
328
// LoadConfig loads the configuration from a YAML-encoded file.
func LoadConfig(path string) (*Config, error) {
329
	data, err := ioutil.ReadFile(path) // #nosec
ale's avatar
ale committed
330
331
332
333
	if err != nil {
		return nil, err
	}
	config := Config{path: path}
334
	if err = yaml.Unmarshal(data, &config); err != nil {
ale's avatar
ale committed
335
336
		return nil, err
	}
337
338
339
340

	// Load service definitions from a directory if necessary, and
	// merge them into config.Services.
	if config.ServicesDir != "" {
341
		files, _ := filepath.Glob(filepath.Join(config.ServicesDir, "*.yml")) // nolint
342
343
344
345
346
347
348
349
350
351
352
353
354
355
		for _, f := range files {
			if strings.HasPrefix(filepath.Base(f), ".") {
				continue
			}
			tmp, err := loadStandaloneServiceConfig(f)
			if err != nil {
				return nil, fmt.Errorf("%s: %v", f, err)
			}
			for name, svc := range tmp {
				config.Services[name] = svc
			}
		}
	}

356
357
358
	if err := config.compile(); err != nil {
		return nil, err
	}
ale's avatar
ale committed
359
360
361
	return &config, nil
}

ale's avatar
ale committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Instrumentation.
var (
	authRequestsCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "auth_requests",
			Help: "Number of authentication requests.",
		},
		[]string{"service", "status"},
	)
	ratelimitCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "auth_requests_ratelimited",
			Help: "Number of rate-limited authentication requests.",
		},
		[]string{"service"},
	)
)

func init() {
	prometheus.MustRegister(authRequestsCounter)
	prometheus.MustRegister(ratelimitCounter)
}

ale's avatar
ale committed
385
386
387
388
389
// Server is the main authentication server object.
type Server struct {
	backends     []UserBackend
	config       *Config
	u2fShortTerm U2FShortTermStorage
390
	otpShortTerm OTPShortTermStorage
ale's avatar
ale committed
391
392
393
394
395
396
397
398
399
400
401
}

func newError() *auth.Response {
	return &auth.Response{Status: auth.StatusError}
}

func newOK() *auth.Response {
	return &auth.Response{Status: auth.StatusOK}
}

// NewServer creates a Server using the given configuration.
402
func NewServer(config *Config) (*Server, error) {
ale's avatar
ale committed
403
	s := &Server{
404
		config: config,
ale's avatar
ale committed
405
	}
406
407
408
409

	if config.U2FShortTerm != nil {
		s.u2fShortTerm = newMemcacheU2FStorage(config.U2FShortTerm.Servers)
	}
410
411
412
413
414
	if config.OTPShortTerm != nil {
		s.otpShortTerm = newMemcacheOTPStorage(config.OTPShortTerm.Servers)
	} else {
		s.otpShortTerm = &nullOTPStorage{}
	}
415

ale's avatar
ale committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
	for _, name := range config.EnabledBackends {
		var b UserBackend
		var err error
		switch name {
		case "file":
			b, err = newFileBackend(config)
		case "ldap":
			b, err = newLDAPBackend(config)
		default:
			err = fmt.Errorf("unknown backend %s", name)
		}
		if err != nil {
			return nil, err
		}
		s.backends = append(s.backends, b)
	}
	return s, nil
}

// Close the authentication server and release all associated resources.
func (s *Server) Close() {
	for _, b := range s.backends {
		b.Close()
	}
}

func (s *Server) getServiceConfig(service string) (*ServiceConfig, bool) {
	c, ok := s.config.Services[service]
	if !ok {
		c, ok = s.config.Services["default"]
	}
	return c, ok
}

450
451
452
453
func (s *Server) getUser(ctx context.Context, serviceConfig *ServiceConfig, username string) (*User, bool) {
	for _, spec := range serviceConfig.BackendSpecs {
		for _, b := range s.backends {
			if user, ok := b.GetUser(ctx, spec, username); ok {
454
455
456
				if len(spec.StaticGroups) > 0 {
					user.Groups = append(user.Groups, spec.StaticGroups...)
				}
457
458
459
460
461
462
463
				return user, true
			}
		}
	}
	return nil, false
}

ale's avatar
ale committed
464
465
466
467
468
469
470
471
// Authenticate a user with the parameters specified in the incoming AuthRequest.
func (s *Server) Authenticate(ctx context.Context, req *auth.Request) *auth.Response {
	serviceConfig, ok := s.getServiceConfig(req.Service)
	if !ok {
		log.Printf("unknown service %s", req.Service)
		return newError()
	}

472
	user, ok := s.getUser(ctx, serviceConfig, req.Username)
ale's avatar
ale committed
473
	if !ok {
ale's avatar
ale committed
474
475
		// User is unknown to all backends. Do not proceed
		// further, but log and increment stats counters.
ale's avatar
ale committed
476
		log.Printf("unknown user %s", req.Username)
ale's avatar
ale committed
477
478
479
480
		authRequestsCounter.With(prometheus.Labels{
			"service": req.Service,
			"status":  "unknown_user",
		})
ale's avatar
ale committed
481
482
483
		return newError()
	}

484
485
486
	// Apply rate limiting and blacklisting _before_ invoking the
	// authentication handlers, as they may be CPU intensive.
	if allowed := serviceConfig.checkRateLimits(user, req); !allowed {
ale's avatar
ale committed
487
488
489
		ratelimitCounter.With(prometheus.Labels{
			"service": req.Service,
		}).Inc()
490
491
492
		return newError()
	}

ale's avatar
ale committed
493
494
495
	resp, err := s.authenticateUser(req, serviceConfig, user)
	if err != nil {
		resp = newError()
ale's avatar
ale committed
496
		log.Printf("auth: user=%s service=%s status=%s error=%s", req.Username, req.Service, resp.Status.String(), err)
ale's avatar
ale committed
497
498
	} else {
		// Log the request and response.
ale's avatar
ale committed
499
		log.Printf("auth: user=%s service=%s status=%s", req.Username, req.Service, resp.Status.String())
ale's avatar
ale committed
500
	}
501
502
503
504

	// Notify blacklists of the result.
	serviceConfig.notifyBlacklists(user, req, resp)

ale's avatar
ale committed
505
506
507
508
509
510
	// Increment stats counters.
	authRequestsCounter.With(prometheus.Labels{
		"service": req.Service,
		"status":  resp.Status.String(),
	}).Inc()

511
512
513
	return resp
}

ale's avatar
ale committed
514
515
516
// Authenticate a user. Returning an error should result in an
// AuthResponse with StatusError.
func (s *Server) authenticateUser(req *auth.Request, serviceConfig *ServiceConfig, user *User) (resp *auth.Response, err error) {
ale's avatar
ale committed
517
518
519
	// Verify different credentials depending on whether the user
	// has 2FA enabled or not, and on whether the service itself
	// supports challenge-response authentication.
ale's avatar
ale committed
520
	if serviceConfig.Enforce2FA || user.Has2FA() {
ale's avatar
ale committed
521
		if serviceConfig.ChallengeResponse {
ale's avatar
ale committed
522
			resp, err = s.authenticateUserWith2FA(user, req)
ale's avatar
ale committed
523
		} else {
ale's avatar
ale committed
524
			resp, err = s.authenticateUserWithASP(user, req)
ale's avatar
ale committed
525
526
		}
	} else {
ale's avatar
ale committed
527
528
529
530
		resp, err = s.authenticateUserWithPassword(user, req)
	}
	if err != nil {
		return
531
532
533
534
535
536
537
538
	}

	// Process the response through filters (device info checks,
	// etc) that may or may not change the response itself.
	for _, f := range serviceConfig.filters {
		if resp.Status == auth.StatusError {
			break
		}
539
		resp = f.Filter(user, req, resp)
ale's avatar
ale committed
540
541
542
543
544
545
	}

	// If the response is successful, augment it with user information.
	if resp.Status == auth.StatusOK {
		resp.UserInfo = user.UserInfo()
	}
ale's avatar
ale committed
546
	return
ale's avatar
ale committed
547
548
}

ale's avatar
ale committed
549
func (s *Server) authenticateUserWithPassword(user *User, req *auth.Request) (*auth.Response, error) {
ale's avatar
ale committed
550
551
	// Ok we only need to check the password here.
	if checkPassword(req.Password, user.EncryptedPassword) {
ale's avatar
ale committed
552
		return newOK(), nil
ale's avatar
ale committed
553
	}
ale's avatar
ale committed
554
	return nil, errors.New("wrong password")
ale's avatar
ale committed
555
556
}

ale's avatar
ale committed
557
func (s *Server) authenticateUserWithASP(user *User, req *auth.Request) (*auth.Response, error) {
ale's avatar
ale committed
558
559
	for _, asp := range user.AppSpecificPasswords {
		if asp.Service == req.Service && checkPassword(req.Password, asp.EncryptedPassword) {
ale's avatar
ale committed
560
			return newOK(), nil
ale's avatar
ale committed
561
562
		}
	}
ale's avatar
ale committed
563
	return nil, errors.New("wrong app-specific password")
ale's avatar
ale committed
564
565
}

ale's avatar
ale committed
566
func (s *Server) authenticateUserWith2FA(user *User, req *auth.Request) (*auth.Response, error) {
ale's avatar
ale committed
567
568
	// First of all verify the password.
	if !checkPassword(req.Password, user.EncryptedPassword) {
ale's avatar
ale committed
569
		return nil, errors.New("wrong password")
ale's avatar
ale committed
570
571
572
573
574
575
576
577
578
	}

	// If the request contains one of the 2FA attributes, verify
	// it. But if it contains none, we return with
	// AuthStatusInsufficientCredentials and potentially a 2FA
	// hint (for U2F).
	switch {
	case req.U2FResponse != nil:
		if user.HasU2F() && s.checkU2F(user, req.U2FResponse) {
ale's avatar
ale committed
579
			return newOK(), nil
ale's avatar
ale committed
580
		}
ale's avatar
ale committed
581
		return nil, errors.New("bad U2F response")
ale's avatar
ale committed
582
	case req.OTP != "":
583
584
585
586
587
		if user.HasOTP() && s.checkOTP(user, req.OTP, user.TOTPSecret) {
			// Save the token for replay protection.
			if err := s.otpShortTerm.AddToken(user.Name, req.OTP); err != nil {
				log.Printf("error saving OTP token to short-term storage: %v", err)
			}
ale's avatar
ale committed
588
			return newOK(), nil
ale's avatar
ale committed
589
		}
ale's avatar
ale committed
590
		return nil, errors.New("bad OTP")
ale's avatar
ale committed
591
592
593
594
595
596
597
598
	default:
		resp := &auth.Response{
			Status: auth.StatusInsufficientCredentials,
		}
		if req.U2FAppID != "" && user.HasU2F() {
			resp.TFAMethod = auth.TFAMethodU2F
			signReq, err := s.u2fSignRequest(user, req.U2FAppID)
			if err != nil {
ale's avatar
ale committed
599
				return nil, err
ale's avatar
ale committed
600
601
602
603
604
			}
			resp.U2FSignRequest = signReq
		} else if user.HasOTP() {
			resp.TFAMethod = auth.TFAMethodOTP
		}
ale's avatar
ale committed
605
		return resp, nil
ale's avatar
ale committed
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
	}
}

func (s *Server) u2fSignRequest(user *User, appID string) (*u2f.WebSignRequest, error) {
	challenge, err := u2f.NewChallenge(appID, []string{appID})
	if err != nil {
		return nil, err
	}

	if err := s.u2fShortTerm.SetUserChallenge(user.Name, challenge); err != nil {
		return nil, err
	}

	return challenge.SignRequest(user.U2FRegistrations), nil
}

func (s *Server) checkU2F(user *User, resp *u2f.SignResponse) bool {
	challenge, ok := s.u2fShortTerm.GetUserChallenge(user.Name)
	if !ok {
		return false
	}
	for _, reg := range user.U2FRegistrations {
		_, err := reg.Authenticate(*resp, *challenge, 0)
		if err == nil {
			return true
		}
	}
	return false
}

func checkPassword(password, hash []byte) bool {
ale's avatar
ale committed
637
	return pwhash.ComparePassword(string(hash), string(password))
ale's avatar
ale committed
638
639
}

640
641
642
643
644
645
646
func (s *Server) checkOTP(user *User, otp, secret string) bool {
	// Check our short-ttl blacklist for the token (replay protection).
	if s.otpShortTerm.HasToken(user.Name, otp) {
		log.Printf("replay protection triggered for %s", user.Name)
		return false
	}

ale's avatar
ale committed
647
648
	return totp.Validate(otp, secret)
}