forked from shaj13/go-guardian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
47 lines (41 loc) · 1.21 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
package basic
import (
"crypto"
"github.com/m87carlson/go-guardian/v2/auth"
"github.com/m87carlson/go-guardian/v2/auth/internal"
)
// SetHash apply password hashing using h,
// SetHash only used when caching the auth decision,
// to mitigates brute force attacks.
func SetHash(h crypto.Hash) auth.Option {
b := basicHashing{h}
return SetComparator(b)
}
// SetComparator set password comparator,
// to be used when caching the auth decision.
func SetComparator(c Comparator) auth.Option {
return auth.OptionFunc(func(v interface{}) {
if v, ok := v.(*cachedBasic); ok {
v.comparator = c
}
})
}
// SetParser set credentials parser.
func SetParser(p Parser) auth.Option {
return auth.OptionFunc(func(v interface{}) {
if v, ok := v.(*basic); ok {
v.parser = p
}
})
}
// SetUserNameHash apply username hashing based on HMAC with h and key,
// SetUserNameHash only used when caching the auth decision,
// to prevent precomputation and length extension attacks,
// and to mitigates hash map DOS attacks via collisions.
func SetUserNameHash(h crypto.Hash, key []byte) auth.Option {
return auth.OptionFunc(func(v interface{}) {
if v, ok := v.(*cachedBasic); ok {
v.hasher = internal.NewHMACHasher(h, key)
}
})
}