@@ -6,67 +6,97 @@ import (
6
6
"fmt"
7
7
"hash"
8
8
"io"
9
- "strings"
9
+ "regexp"
10
+ "time"
10
11
)
11
12
12
- type authorizationHeader struct {
13
+ type authorization struct {
13
14
Algorithm string // unquoted
14
15
Cnonce string // quoted
15
- Nc string // unquoted
16
- Nounce string // quoted
16
+ Nc int // unquoted
17
+ Nonce string // quoted
17
18
Opaque string // quoted
18
19
Qop string // unquoted
19
20
Realm string // quoted
20
- Resposne string // quoted
21
+ Response string // quoted
21
22
Uri string // quoted
22
- Userhash string // quoted
23
+ Userhash bool // quoted
23
24
Username string // quoted
24
25
Username_ string // quoted
25
26
}
26
27
27
- func (ah * authorizationHeader ) ComputeResponse () authorizationHeader {
28
- return * ah
28
+ func newAuthorization (wa * wwwAuthenticate , dr * DigestRequest ) (* authorization , error ) {
29
+
30
+ auth := authorization {
31
+ Algorithm : wa .Algorithm ,
32
+ Cnonce : "" ,
33
+ Nc : 1 , // TODO
34
+ Nonce : wa .Nonce ,
35
+ Opaque : wa .Opaque ,
36
+ Qop : "" ,
37
+ Realm : wa .Realm ,
38
+ Response : "" ,
39
+ Uri : dr .Uri ,
40
+ Userhash : wa .Userhash ,
41
+ Username : dr .Username ,
42
+ Username_ : "" , // TODO
43
+ }
44
+
45
+ auth .Cnonce = auth .hash (fmt .Sprintf ("%d:%s:dfjosbn3kjd01" , time .Now ().UnixNano (), dr .Username ))
46
+
47
+ if auth .Userhash {
48
+ auth .Username = auth .hash (fmt .Sprintf ("%s:%s" , auth .Username , auth .Realm ))
49
+ }
50
+
51
+ auth .Response = auth .computeResponse (wa , dr )
52
+
53
+ return & auth , nil
54
+ }
55
+
56
+ func (ah * authorization ) computeResponse (wa * wwwAuthenticate , dr * DigestRequest ) (s string ) {
57
+
58
+ kdSecret := ah .hash (ah .computeA1 (wa , dr ))
59
+ kdData := fmt .Sprintf ("%s:%s:%s:%s:%s" , ah .Nonce , ah .Nc , ah .Cnonce , ah .Qop , ah .hash (ah .computeA2 (wa , dr )))
60
+
61
+ return ah .hash (fmt .Sprintf ("%s:%s" , kdSecret , kdData ))
29
62
}
30
63
31
- func (ah * authorizationHeader ) ComputeA1 ( password string ) ( s string ) {
64
+ func (ah * authorization ) computeA1 ( wa * wwwAuthenticate , dr * DigestRequest ) string {
32
65
33
- if strings .Compare (ah .Algorithm , "" ) == 0 ||
34
- strings .Compare (ah .Algorithm , "MD5" ) == 0 ||
35
- strings .Compare (ah .Algorithm , "SHA-256" ) == 0 {
36
- s = fmt .Sprintf ("%s:%s:%s" , ah .Username , ah .Realm , password )
66
+ if ah .Algorithm == "" || ah .Algorithm == "MD5" || ah .Algorithm == "SHA-256" {
67
+ return fmt .Sprintf ("%s:%s:%s" , ah .Username , ah .Realm , dr .Password )
37
68
}
38
69
39
- if strings .Compare (ah .Algorithm , "MD5-sess" ) ||
40
- strings .Compare (ah .Algorithm , "SHA-256-sess" ) {
41
- upHash := ah .Hash (fmt .Sprintf ("%s:%s:%s" , ah .Username , ah .Realm , password ))
42
- s = fmt .Sprintf ("%s:%s:%s" , upHash , ah .Nc )
70
+ if ah .Algorithm == "MD5-sess" || ah .Algorithm == "SHA-256-sess" {
71
+ upHash := ah .hash (fmt .Sprintf ("%s:%s:%s" , ah .Username , ah .Realm , dr .Password ))
72
+ return fmt .Sprintf ("%s:%s:%s" , upHash , ah .Nc )
43
73
}
44
74
45
- return
75
+ return ""
46
76
}
47
77
48
- func (ah * authorizationHeader ) ComputeA2 () ( s string ) {
78
+ func (ah * authorization ) computeA2 ( wa * wwwAuthenticate , dr * DigestRequest ) string {
49
79
50
- if strings .Compare (ah .Qop , "auth" ) == 0 || strings .Compare (ah .Qop , "" ) == 0 {
51
- s = fmt .Sprintf ("%s:%s" , ah .Method , ah .Uri )
80
+ if matched , _ := regexp .MatchString ("auth-int" , wa .Qop ); matched {
81
+ ah .Qop = "auth-int"
82
+ return fmt .Sprintf ("%s:%s:%s" , dr .Method , ah .Uri , ah .hash (dr .Body ))
52
83
}
53
84
54
- if strings .Compare (ah .Qop , "auth-int" ) == 0 {
55
- s = fmt .Sprintf ("%s:%s" , s , ah .Hash (ah .Body ))
85
+ if ah .Qop == "auth" || ah .Qop == "" {
86
+ ah .Qop = "auth"
87
+ return fmt .Sprintf ("%s:%s" , dr .Method , ah .Uri )
56
88
}
57
89
58
- return
90
+ return ""
59
91
}
60
92
61
- func (ah * authorizationHeader ) Hash (a string ) (s string ) {
93
+ func (ah * authorization ) hash (a string ) (s string ) {
62
94
63
95
var h hash.Hash
64
96
65
- if strings .Compare (ah .Algorithm , "MD5" ) == 0 ||
66
- strings .Compare (ah .Algorithm , "MD5-sess" ) == 0 {
97
+ if ah .Algorithm == "MD5" || ah .Algorithm == "MD5-sess" {
67
98
h = md5 .New ()
68
- } else if strings .Compare (ah .Algorithm , "SHA-256" ) == 0 ||
69
- strings .Compare (ah .Algorithm , "SHA-256-sess" ) == 0 {
99
+ } else if ah .Algorithm == "SHA-256" || ah .Algorithm == "SHA-256-sess" {
70
100
h = sha256 .New ()
71
101
}
72
102
@@ -75,3 +105,7 @@ func (ah *authorizationHeader) Hash(a string) (s string) {
75
105
76
106
return
77
107
}
108
+
109
+ func (ah * authorization ) toString () string {
110
+ return ""
111
+ }
0 commit comments