forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
154 lines (129 loc) · 3.38 KB
/
validate.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
package auth
import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/bass3m/gortsplib/pkg/base"
"github.com/bass3m/gortsplib/pkg/headers"
"github.com/bass3m/gortsplib/pkg/url"
)
func md5Hex(in string) string {
h := md5.New()
h.Write([]byte(in))
return hex.EncodeToString(h.Sum(nil))
}
// GenerateNonce generates a nonce that can be used in Validate().
func GenerateNonce() (string, error) {
byts := make([]byte, 16)
_, err := rand.Read(byts)
if err != nil {
return "", err
}
return hex.EncodeToString(byts), nil
}
// GenerateWWWAuthenticate generates a WWW-Authenticate header.
func GenerateWWWAuthenticate(methods []headers.AuthMethod, realm string, nonce string) base.HeaderValue {
if methods == nil {
methods = []headers.AuthMethod{headers.AuthBasic, headers.AuthDigest}
}
var ret base.HeaderValue
for _, m := range methods {
switch m {
case headers.AuthBasic:
ret = append(ret, (&headers.Authenticate{
Method: headers.AuthBasic,
Realm: &realm,
}).Marshal()...)
case headers.AuthDigest:
ret = append(ret, headers.Authenticate{
Method: headers.AuthDigest,
Realm: &realm,
Nonce: &nonce,
}.Marshal()...)
}
}
return ret
}
func contains(list []headers.AuthMethod, item headers.AuthMethod) bool {
for _, i := range list {
if i == item {
return true
}
}
return false
}
// Validate validates a request sent by a client.
func Validate(
req *base.Request,
user string,
pass string,
baseURL *url.URL,
methods []headers.AuthMethod,
realm string,
nonce string,
) error {
if methods == nil {
methods = []headers.AuthMethod{headers.AuthBasic, headers.AuthDigest}
}
var auth headers.Authorization
err := auth.Unmarshal(req.Header["Authorization"])
if err != nil {
return err
}
switch {
case auth.Method == headers.AuthBasic && contains(methods, headers.AuthBasic):
if auth.BasicUser != user {
return fmt.Errorf("authentication failed")
}
if auth.BasicPass != pass {
return fmt.Errorf("authentication failed")
}
case auth.Method == headers.AuthDigest && contains(methods, headers.AuthDigest):
if auth.DigestValues.Realm == nil {
return fmt.Errorf("realm is missing")
}
if auth.DigestValues.Nonce == nil {
return fmt.Errorf("nonce is missing")
}
if auth.DigestValues.Username == nil {
return fmt.Errorf("username is missing")
}
if auth.DigestValues.URI == nil {
return fmt.Errorf("uri is missing")
}
if auth.DigestValues.Response == nil {
return fmt.Errorf("response is missing")
}
if *auth.DigestValues.Nonce != nonce {
return fmt.Errorf("wrong nonce")
}
if *auth.DigestValues.Realm != realm {
return fmt.Errorf("wrong realm")
}
if *auth.DigestValues.Username != user {
return fmt.Errorf("authentication failed")
}
ur := req.URL
if *auth.DigestValues.URI != ur.String() {
// in SETUP requests, VLC strips the control attribute.
// try again with the base URL.
if baseURL != nil {
ur = baseURL
if *auth.DigestValues.URI != ur.String() {
return fmt.Errorf("wrong URL")
}
} else {
return fmt.Errorf("wrong URL")
}
}
response := md5Hex(md5Hex(user+":"+realm+":"+pass) +
":" + nonce + ":" + md5Hex(string(req.Method)+":"+ur.String()))
if *auth.DigestValues.Response != response {
return fmt.Errorf("authentication failed")
}
default:
return fmt.Errorf("no supported authentication methods found")
}
return nil
}