-
Notifications
You must be signed in to change notification settings - Fork 1
/
authorization.go
178 lines (134 loc) · 3.76 KB
/
authorization.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
package wserest
import (
"bytes"
"crypto/md5"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"net/url"
"regexp"
"strings"
"time"
)
type authorization struct {
Algorithm string // unquoted
Cnonce string // quoted
Nc int // unquoted
Nonce string // quoted
Opaque string // quoted
Qop string // unquoted
Realm string // quoted
Response string // quoted
URI string // quoted
Userhash bool // quoted
Username string // quoted
}
func newAuthorization(dr *digestRequest) (*authorization, error) {
ah := authorization{
Algorithm: dr.Wa.Algorithm,
Cnonce: "",
Nc: 0,
Nonce: dr.Wa.Nonce,
Opaque: dr.Wa.Opaque,
Qop: "",
Realm: dr.Wa.Realm,
Response: "",
URI: "",
Userhash: dr.Wa.Userhash,
Username: "",
}
return ah.refreshAuthorization(dr)
}
func (ah *authorization) refreshAuthorization(dr *digestRequest) (*authorization, error) {
ah.Username = dr.Username
if ah.Userhash {
ah.Username = ah.hash(fmt.Sprintf("%s:%s", ah.Username, ah.Realm))
}
ah.Nc++
ah.Cnonce = ah.hash(fmt.Sprintf("%d:%s:my_value", time.Now().UnixNano(), dr.Username))
url, err := url.Parse(dr.URI)
if err != nil {
return nil, err
}
ah.URI = url.RequestURI()
ah.Response = ah.computeResponse(dr)
return ah, nil
}
func (ah *authorization) computeResponse(dr *digestRequest) (s string) {
kdSecret := ah.hash(ah.computeA1(dr))
kdData := fmt.Sprintf("%s:%08x:%s:%s:%s", ah.Nonce, ah.Nc, ah.Cnonce, ah.Qop, ah.hash(ah.computeA2(dr)))
return ah.hash(fmt.Sprintf("%s:%s", kdSecret, kdData))
}
func (ah *authorization) computeA1(dr *digestRequest) string {
if ah.Algorithm == "" || ah.Algorithm == "MD5" || ah.Algorithm == "SHA-256" {
return fmt.Sprintf("%s:%s:%s", ah.Username, ah.Realm, dr.Password)
}
if ah.Algorithm == "MD5-sess" || ah.Algorithm == "SHA-256-sess" {
upHash := ah.hash(fmt.Sprintf("%s:%s:%s", ah.Username, ah.Realm, dr.Password))
return fmt.Sprintf("%s:%s:%s", upHash, ah.Nonce, ah.Cnonce)
}
return ""
}
func (ah *authorization) computeA2(dr *digestRequest) string {
if matched, _ := regexp.MatchString("auth-int", dr.Wa.Qop); matched {
ah.Qop = "auth-int"
return fmt.Sprintf("%s:%s:%s", dr.Method, ah.URI, ah.hash(dr.Body))
}
if dr.Wa.Qop == "auth" || dr.Wa.Qop == "" {
ah.Qop = "auth"
return fmt.Sprintf("%s:%s", dr.Method, ah.URI)
}
return ""
}
func (ah *authorization) hash(a string) (s string) {
var h hash.Hash
if ah.Algorithm == "" || ah.Algorithm == "MD5" || ah.Algorithm == "MD5-sess" {
h = md5.New()
} else if ah.Algorithm == "SHA-256" || ah.Algorithm == "SHA-256-sess" {
h = sha256.New()
}
io.WriteString(h, a)
s = hex.EncodeToString(h.Sum(nil))
return
}
func (ah *authorization) toString() string {
var buffer bytes.Buffer
buffer.WriteString("Digest ")
if ah.Algorithm != "" {
buffer.WriteString(fmt.Sprintf("algorithm=%s, ", ah.Algorithm))
}
if ah.Cnonce != "" {
buffer.WriteString(fmt.Sprintf("cnonce=\"%s\", ", ah.Cnonce))
}
if ah.Nc != 0 {
buffer.WriteString(fmt.Sprintf("nc=%08x, ", ah.Nc))
}
if ah.Opaque != "" {
buffer.WriteString(fmt.Sprintf("opaque=\"%s\", ", ah.Opaque))
}
if ah.Nonce != "" {
buffer.WriteString(fmt.Sprintf("nonce=\"%s\", ", ah.Nonce))
}
if ah.Qop != "" {
buffer.WriteString(fmt.Sprintf("qop=%s, ", ah.Qop))
}
if ah.Realm != "" {
buffer.WriteString(fmt.Sprintf("realm=\"%s\", ", ah.Realm))
}
if ah.Response != "" {
buffer.WriteString(fmt.Sprintf("response=\"%s\", ", ah.Response))
}
if ah.URI != "" {
buffer.WriteString(fmt.Sprintf("uri=\"%s\", ", ah.URI))
}
if ah.Userhash {
buffer.WriteString("userhash=true, ")
}
if ah.Username != "" {
buffer.WriteString(fmt.Sprintf("username=\"%s\", ", ah.Username))
}
s := buffer.String()
return strings.TrimSuffix(s, ", ")
}