mirror of
https://github.com/aler9/gortsplib
synced 2025-10-06 15:46:51 +08:00
support authentication with vlc
This commit is contained in:
27
auth.go
27
auth.go
@@ -141,12 +141,35 @@ func (as *AuthServer) ValidateHeader(header []string, method Method, ur *url.URL
|
||||
return fmt.Errorf("wrong username")
|
||||
}
|
||||
|
||||
if inUri != ur.String() {
|
||||
uri := ur.String()
|
||||
|
||||
if inUri != uri {
|
||||
// VLC strips any subpath
|
||||
newUrl := *ur
|
||||
newUrl.Path = func() string {
|
||||
ret := newUrl.Path
|
||||
|
||||
// remove leading slash
|
||||
if len(ret) > 1 {
|
||||
ret = ret[1:]
|
||||
}
|
||||
|
||||
// strip any subpath
|
||||
if n := strings.Index(ret, "/"); n >= 0 {
|
||||
ret = ret[:n]
|
||||
}
|
||||
|
||||
return "/" + ret
|
||||
}()
|
||||
uri = newUrl.String()
|
||||
|
||||
if inUri != uri {
|
||||
return fmt.Errorf("wrong url")
|
||||
}
|
||||
}
|
||||
|
||||
response := md5Hex(md5Hex(as.user+":"+as.realm+":"+as.pass) +
|
||||
":" + as.nonce + ":" + md5Hex(string(method)+":"+ur.String()))
|
||||
":" + as.nonce + ":" + md5Hex(string(method)+":"+uri))
|
||||
|
||||
if inResponse != response {
|
||||
return fmt.Errorf("wrong response")
|
||||
|
@@ -46,11 +46,17 @@ func ReadHeaderAuth(in string) (*HeaderAuth, error) {
|
||||
func (ha *HeaderAuth) Write() string {
|
||||
ret := ha.Prefix + " "
|
||||
|
||||
// always put realm first, otherwise VLC does not send back the response
|
||||
var sortedKeys []string
|
||||
for key := range ha.Values {
|
||||
if key != "realm" {
|
||||
sortedKeys = append(sortedKeys, key)
|
||||
}
|
||||
}
|
||||
sort.Strings(sortedKeys)
|
||||
if _, ok := ha.Values["realm"]; ok {
|
||||
sortedKeys = append([]string{"realm"}, sortedKeys...)
|
||||
}
|
||||
|
||||
var tmp []string
|
||||
for _, key := range sortedKeys {
|
||||
|
@@ -26,7 +26,7 @@ var casesHeaderAuth = []struct {
|
||||
{
|
||||
"digest request 1",
|
||||
`Digest realm="4419b63f5e51", nonce="8b84a3b789283a8bea8da7fa7d41f08b", stale="FALSE"`,
|
||||
`Digest nonce="8b84a3b789283a8bea8da7fa7d41f08b", realm="4419b63f5e51", stale="FALSE"`,
|
||||
`Digest realm="4419b63f5e51", nonce="8b84a3b789283a8bea8da7fa7d41f08b", stale="FALSE"`,
|
||||
&HeaderAuth{
|
||||
Prefix: "Digest",
|
||||
Values: map[string]string{
|
||||
@@ -39,7 +39,7 @@ var casesHeaderAuth = []struct {
|
||||
{
|
||||
"digest request 2",
|
||||
`Digest realm="4419b63f5e51", nonce="8b84a3b789283a8bea8da7fa7d41f08b", stale=FALSE`,
|
||||
`Digest nonce="8b84a3b789283a8bea8da7fa7d41f08b", realm="4419b63f5e51", stale="FALSE"`,
|
||||
`Digest realm="4419b63f5e51", nonce="8b84a3b789283a8bea8da7fa7d41f08b", stale="FALSE"`,
|
||||
&HeaderAuth{
|
||||
Prefix: "Digest",
|
||||
Values: map[string]string{
|
||||
@@ -52,7 +52,7 @@ var casesHeaderAuth = []struct {
|
||||
{
|
||||
"digest response",
|
||||
`Digest username="aa", realm="bb", nonce="cc", uri="dd", response="ee"`,
|
||||
`Digest nonce="cc", realm="bb", response="ee", uri="dd", username="aa"`,
|
||||
`Digest realm="bb", nonce="cc", response="ee", uri="dd", username="aa"`,
|
||||
&HeaderAuth{
|
||||
Prefix: "Digest",
|
||||
Values: map[string]string{
|
||||
|
Reference in New Issue
Block a user