/
digest.go
101 lines (83 loc) · 2.43 KB
/
digest.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
// Package digest provides authentication strategy,
// to authenticate HTTP requests using the standard digest scheme as described in RFC 7616.
package digest
import (
"context"
"crypto"
_ "crypto/md5" //nolint:gosec
"crypto/subtle"
"encoding/hex"
"errors"
"net/http"
"github.com/shaj13/go-guardian/v2/auth"
)
// ErrInvalidResponse is returned by Strategy when client authz response does not match server hash.
var ErrInvalidResponse = errors.New("strategies/digest: Invalid Response")
// FetchUser a callback function to return the user password and user info.
type FetchUser func(userName string) (string, auth.Info, error)
// Digest authentication strategy.
type Digest struct {
fn FetchUser
chash crypto.Hash
c auth.Cache
h Header
}
// Authenticate user request and returns user info, Otherwise error.
func (d *Digest) Authenticate(ctx context.Context, r *http.Request) (auth.Info, error) {
authz := r.Header.Get("Authorization")
h := make(Header)
if err := h.Parse(authz); err != nil {
return nil, err
}
passwd, info, err := d.fn(h.UserName())
if err != nil {
return nil, err
}
HA1 := d.hash(h.UserName() + ":" + h.Realm() + ":" + passwd)
HA2 := d.hash(r.Method + ":" + r.RequestURI)
HKD := d.hash(HA1 + ":" + h.Nonce() + ":" + h.NC() + ":" + h.Cnonce() + ":" + h.QOP() + ":" + HA2)
if subtle.ConstantTimeCompare([]byte(HKD), []byte(h.Response())) != 1 {
return nil, ErrInvalidResponse
}
if _, ok := d.c.Load(h.Nonce()); !ok {
return nil, ErrInvalidResponse
}
// validate the header values.
ch := d.h.Clone()
ch.SetNonce(h.Nonce())
if err := ch.Compare(h); err != nil {
return nil, err
}
return info, nil
}
// GetChallenge returns string indicates the authentication scheme.
// Typically used to adds a HTTP WWW-Authenticate header.
func (d *Digest) GetChallenge() string {
h := d.h.Clone()
str := h.WWWAuthenticate()
d.c.Store(h.Nonce(), struct{}{})
return str
}
func (d *Digest) hash(str string) string {
h := d.chash.New()
_, _ = h.Write([]byte(str))
p := h.Sum(nil)
return hex.EncodeToString(p)
}
// New returns digest authentication strategy.
// Digest strategy use MD5 as default hash.
// Digest use cache to store nonce.
func New(f FetchUser, c auth.Cache, opts ...auth.Option) *Digest {
d := new(Digest)
d.fn = f
d.chash = crypto.MD5
d.c = c
d.h = make(Header)
d.h.SetRealm("Users")
d.h.SetAlgorithm("md5")
d.h.SetOpaque(secretKey())
for _, opt := range opts {
opt.Apply(d)
}
return d
}