forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticate.go
170 lines (123 loc) · 2.71 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
// Package headers contains various RTSP headers.
package headers
import (
"fmt"
"strings"
"github.com/cobalt-robotics/gortsplib/pkg/base"
)
// AuthMethod is an authentication method.
type AuthMethod int
const (
// AuthBasic is the Basic authentication method
AuthBasic AuthMethod = iota
// AuthDigest is the Digest authentication method
AuthDigest
)
// Authenticate is an Authenticate or a WWW-Authenticate header.
type Authenticate struct {
// authentication method
Method AuthMethod
// (optional) username
Username *string
// (optional) realm
Realm *string
// (optional) nonce
Nonce *string
// (optional) uri
URI *string
// (optional) response
Response *string
// (optional) opaque
Opaque *string
// (optional) stale
Stale *string
// (optional) algorithm
Algorithm *string
}
// Unmarshal decodes an Authenticate or 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 = AuthBasic
case "Digest":
h.Method = AuthDigest
default:
return fmt.Errorf("invalid method (%s)", method)
}
kvs, err := keyValParse(v0, ',')
if err != nil {
return err
}
for k, rv := range kvs {
v := rv
switch k {
case "username":
h.Username = &v
case "realm":
h.Realm = &v
case "nonce":
h.Nonce = &v
case "uri":
h.URI = &v
case "response":
h.Response = &v
case "opaque":
h.Opaque = &v
case "stale":
h.Stale = &v
case "algorithm":
h.Algorithm = &v
}
}
return nil
}
// Marshal encodes an Authenticate or a WWW-Authenticate header.
func (h Authenticate) Marshal() base.HeaderValue {
ret := ""
switch h.Method {
case AuthBasic:
ret += "Basic"
case AuthDigest:
ret += "Digest"
}
ret += " "
var rets []string
if h.Username != nil {
rets = append(rets, "username=\""+*h.Username+"\"")
}
if h.Realm != nil {
rets = append(rets, "realm=\""+*h.Realm+"\"")
}
if h.Nonce != nil {
rets = append(rets, "nonce=\""+*h.Nonce+"\"")
}
if h.URI != nil {
rets = append(rets, "uri=\""+*h.URI+"\"")
}
if h.Response != nil {
rets = append(rets, "response=\""+*h.Response+"\"")
}
if h.Opaque != nil {
rets = append(rets, "opaque=\""+*h.Opaque+"\"")
}
if h.Stale != nil {
rets = append(rets, "stale=\""+*h.Stale+"\"")
}
if h.Algorithm != nil {
rets = append(rets, "algorithm=\""+*h.Algorithm+"\"")
}
ret += strings.Join(rets, ", ")
return base.HeaderValue{ret}
}