-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.go
52 lines (43 loc) · 1.43 KB
/
validator.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
// Copyright 2017 The Bulma Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bulma
var (
_ Validator = (*ValidateFunc)(nil)
_ Validator = (*User)(nil)
)
// Credential is provided when the client is requesting access to
// protected resources.
type Credential struct {
Username string
Password string
Authorization bool
}
// Validator is the interface indicating the type implementing it
// supports credentials validation.
type Validator interface {
// Validate validates the given credentials and
// returns false if validation fails.
Validate(*Credential) bool
}
// ValidateFunc implements a validator.
type ValidateFunc func(*Credential) bool
// Validate validates the given credentials and
// returns false if validation fails.
func (f ValidateFunc) Validate(c *Credential) bool {
return f(c)
}
// Auth is a basic credentials function implementing a validator.
func Auth(username, password string) Validator {
return ValidateFunc(func(c *Credential) bool {
return c.Authorization && c.Username == username && c.Password == password
})
}
// User is a list of users implementing a validator.
type User map[string]string
// Validate validates the given credentials and
// returns false if validation fails.
func (u User) Validate(c *Credential) bool {
password, ok := u[c.Username]
return ok && Auth(c.Username, password).Validate(c)
}