2021-09-27 08:36:28 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-10-17 13:40:17 +00:00
|
|
|
"sort"
|
2021-10-04 06:58:08 +00:00
|
|
|
"strings"
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/headers"
|
2021-09-27 08:36:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AuthMethods is the authMethods parameter.
|
|
|
|
type AuthMethods []headers.AuthMethod
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2021-09-27 08:36:28 +00:00
|
|
|
func (d AuthMethods) MarshalJSON() ([]byte, error) {
|
2021-10-17 13:40:17 +00:00
|
|
|
out := make([]string, len(d))
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2021-10-17 13:40:17 +00:00
|
|
|
for i, v := range d {
|
2021-09-27 08:36:28 +00:00
|
|
|
switch v {
|
|
|
|
case headers.AuthBasic:
|
2021-10-17 13:40:17 +00:00
|
|
|
out[i] = "basic"
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2023-03-31 14:22:08 +00:00
|
|
|
case headers.AuthDigest:
|
2021-10-17 13:40:17 +00:00
|
|
|
out[i] = "digest"
|
2023-03-31 14:22:08 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid authentication method: %v", v)
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 13:40:17 +00:00
|
|
|
sort.Strings(out)
|
|
|
|
|
2021-09-27 08:36:28 +00:00
|
|
|
return json.Marshal(out)
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2021-09-27 08:36:28 +00:00
|
|
|
func (d *AuthMethods) UnmarshalJSON(b []byte) error {
|
2021-10-11 09:46:40 +00:00
|
|
|
var in []string
|
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
2021-09-27 08:36:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
*d = nil
|
|
|
|
|
2021-10-11 09:46:40 +00:00
|
|
|
for _, v := range in {
|
2021-09-27 08:36:28 +00:00
|
|
|
switch v {
|
|
|
|
case "basic":
|
|
|
|
*d = append(*d, headers.AuthBasic)
|
|
|
|
|
|
|
|
case "digest":
|
|
|
|
*d = append(*d, headers.AuthDigest)
|
|
|
|
|
|
|
|
default:
|
2023-03-31 14:22:08 +00:00
|
|
|
return fmt.Errorf("invalid authentication method: '%s'", v)
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-11 09:46:40 +00:00
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
// UnmarshalEnv implements envUnmarshaler.
|
|
|
|
func (d *AuthMethods) UnmarshalEnv(s string) error {
|
2021-10-11 09:46:40 +00:00
|
|
|
byts, _ := json.Marshal(strings.Split(s, ","))
|
|
|
|
return d.UnmarshalJSON(byts)
|
|
|
|
}
|