-
Notifications
You must be signed in to change notification settings - Fork 180
/
authenticate.go
186 lines (143 loc) · 3.11 KB
/
authenticate.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
179
180
181
182
183
184
185
186
// Package headers contains various RTSP headers.
package headers
import (
"fmt"
"strings"
"github.com/bluenviron/gortsplib/v4/pkg/base"
)
// AuthMethod is an authentication method.
type AuthMethod int
// authentication methods.
const (
AuthMethodBasic AuthMethod = iota
AuthMethodDigest
)
// AuthAlgorithm is a digest algorithm.
type AuthAlgorithm int
// digest algorithms.
const (
AuthAlgorithmMD5 AuthAlgorithm = iota
AuthAlgorithmSHA256
)
func parseAuthAlgorithm(v string) (AuthAlgorithm, error) {
switch {
case strings.ToLower(v) == "md5":
return AuthAlgorithmMD5, nil
case strings.ToLower(v) == "sha-256":
return AuthAlgorithmSHA256, nil
default:
return 0, fmt.Errorf("unrecognized algorithm: %v", v)
}
}
// Authenticate is a WWW-Authenticate header.
type Authenticate struct {
// authentication method
Method AuthMethod
// realm
Realm string
//
// Digest authentication fields
//
// nonce
Nonce string
// opaque
Opaque *string
// stale
Stale *string
// algorithm
Algorithm *AuthAlgorithm
}
// Unmarshal decodes a WWW-Authenticate header.
func (h *Authenticate) Unmarshal(v base.HeaderValue) error {
if len(v) == 0 {
return fmt.Errorf("value not provided")
}
if len(v) > 1 {
return fmt.Errorf("value provided multiple times (%v)", v)
}
v0 := v[0]
i := strings.IndexByte(v0, ' ')
if i < 0 {
return fmt.Errorf("unable to split between method and keys (%v)", v0)
}
method, v0 := v0[:i], v0[i+1:]
switch method {
case "Basic":
h.Method = AuthMethodBasic
case "Digest":
h.Method = AuthMethodDigest
default:
return fmt.Errorf("invalid method (%s)", method)
}
if h.Method == AuthMethodBasic {
kvs, err := keyValParse(v0, ',')
if err != nil {
return err
}
realmReceived := false
for k, rv := range kvs {
v := rv
if k == "realm" {
h.Realm = v
realmReceived = true
}
}
if !realmReceived {
return fmt.Errorf("realm is missing")
}
} else { // digest
kvs, err := keyValParse(v0, ',')
if err != nil {
return err
}
realmReceived := false
nonceReceived := false
for k, rv := range kvs {
v := rv
switch k {
case "realm":
h.Realm = v
realmReceived = true
case "nonce":
h.Nonce = v
nonceReceived = true
case "opaque":
h.Opaque = &v
case "stale":
h.Stale = &v
case "algorithm":
a, err := parseAuthAlgorithm(v)
if err != nil {
return err
}
h.Algorithm = &a
}
}
if !realmReceived || !nonceReceived {
return fmt.Errorf("one or more digest fields are missing")
}
}
return nil
}
// Marshal encodes a WWW-Authenticate header.
func (h Authenticate) Marshal() base.HeaderValue {
if h.Method == AuthMethodBasic {
return base.HeaderValue{"Basic " +
"realm=\"" + h.Realm + "\""}
}
ret := "Digest realm=\"" + h.Realm + "\", nonce=\"" + h.Nonce + "\""
if h.Opaque != nil {
ret += ", opaque=\"" + *h.Opaque + "\""
}
if h.Stale != nil {
ret += ", stale=\"" + *h.Stale + "\""
}
if h.Algorithm != nil {
if *h.Algorithm == AuthAlgorithmMD5 {
ret += ", algorithm=\"MD5\""
} else {
ret += ", algorithm=\"SHA-256\""
}
}
return base.HeaderValue{ret}
}