mirror of
https://github.com/bluenviron/mediamtx
synced 2024-12-13 10:14:58 +00:00
9c6ba7e2c7
This is a new authentication system that covers all the features exposed by the server, including playback, API, metrics and PPROF, improves internal authentication by adding permissions, improves HTTP-based authentication by adding the ability to exclude certain actions from being authenticated, adds an additional method (JWT-based authentication).
64 lines
1.0 KiB
Go
64 lines
1.0 KiB
Go
package conf
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
)
|
|
|
|
// AuthMethod is an authentication method.
|
|
type AuthMethod int
|
|
|
|
// authentication methods.
|
|
const (
|
|
AuthMethodInternal AuthMethod = iota
|
|
AuthMethodHTTP
|
|
AuthMethodJWT
|
|
)
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
func (d AuthMethod) MarshalJSON() ([]byte, error) {
|
|
var out string
|
|
|
|
switch d {
|
|
case AuthMethodInternal:
|
|
out = "internal"
|
|
|
|
case AuthMethodHTTP:
|
|
out = "http"
|
|
|
|
default:
|
|
out = "jwt"
|
|
}
|
|
|
|
return json.Marshal(out)
|
|
}
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
func (d *AuthMethod) UnmarshalJSON(b []byte) error {
|
|
var in string
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
|
return err
|
|
}
|
|
|
|
switch in {
|
|
case "internal":
|
|
*d = AuthMethodInternal
|
|
|
|
case "http":
|
|
*d = AuthMethodHTTP
|
|
|
|
case "jwt":
|
|
*d = AuthMethodJWT
|
|
|
|
default:
|
|
return fmt.Errorf("invalid authMethod: '%s'", in)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// UnmarshalEnv implements env.Unmarshaler.
|
|
func (d *AuthMethod) UnmarshalEnv(_ string, v string) error {
|
|
return d.UnmarshalJSON([]byte(`"` + v + `"`))
|
|
}
|