2024-03-04 13:20:34 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
2024-05-15 08:28:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/auth"
|
2024-03-04 13:20:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RTSPAuthMethods is the rtspAuthMethods parameter.
|
2024-05-15 08:28:12 +00:00
|
|
|
type RTSPAuthMethods []auth.ValidateMethod
|
2024-03-04 13:20:34 +00:00
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
|
|
func (d RTSPAuthMethods) MarshalJSON() ([]byte, error) {
|
|
|
|
out := make([]string, len(d))
|
|
|
|
|
|
|
|
for i, v := range d {
|
|
|
|
switch v {
|
2024-05-15 08:28:12 +00:00
|
|
|
case auth.ValidateMethodBasic:
|
2024-03-04 13:20:34 +00:00
|
|
|
out[i] = "basic"
|
|
|
|
|
|
|
|
default:
|
|
|
|
out[i] = "digest"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(out)
|
|
|
|
|
|
|
|
return json.Marshal(out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
|
|
func (d *RTSPAuthMethods) UnmarshalJSON(b []byte) error {
|
|
|
|
var in []string
|
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = nil
|
|
|
|
|
|
|
|
for _, v := range in {
|
|
|
|
switch v {
|
|
|
|
case "basic":
|
2024-05-15 08:28:12 +00:00
|
|
|
*d = append(*d, auth.ValidateMethodBasic)
|
2024-03-04 13:20:34 +00:00
|
|
|
|
|
|
|
case "digest":
|
2024-05-15 08:28:12 +00:00
|
|
|
*d = append(*d, auth.ValidateMethodDigestMD5)
|
2024-03-04 13:20:34 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid authentication method: '%s'", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalEnv implements env.Unmarshaler.
|
|
|
|
func (d *RTSPAuthMethods) UnmarshalEnv(_ string, v string) error {
|
|
|
|
byts, _ := json.Marshal(strings.Split(v, ","))
|
|
|
|
return d.UnmarshalJSON(byts)
|
|
|
|
}
|