2021-09-27 08:36:28 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
// AuthMethod is an authentication method.
|
|
|
|
type AuthMethod int
|
|
|
|
|
|
|
|
// authentication methods.
|
|
|
|
const (
|
|
|
|
AuthMethodInternal AuthMethod = iota
|
|
|
|
AuthMethodHTTP
|
|
|
|
AuthMethodJWT
|
|
|
|
)
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2024-03-04 13:20:34 +00:00
|
|
|
func (d AuthMethod) MarshalJSON() ([]byte, error) {
|
|
|
|
var out string
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
switch d {
|
|
|
|
case AuthMethodInternal:
|
|
|
|
out = "internal"
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
case AuthMethodHTTP:
|
|
|
|
out = "http"
|
2023-03-31 14:22:08 +00:00
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
default:
|
|
|
|
out = "jwt"
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(out)
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2024-03-04 13:20:34 +00:00
|
|
|
func (d *AuthMethod) UnmarshalJSON(b []byte) error {
|
|
|
|
var in string
|
2021-10-11 09:46:40 +00:00
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
2021-09-27 08:36:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
switch in {
|
|
|
|
case "internal":
|
|
|
|
*d = AuthMethodInternal
|
2023-05-06 21:00:42 +00:00
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
case "http":
|
|
|
|
*d = AuthMethodHTTP
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
case "jwt":
|
|
|
|
*d = AuthMethodJWT
|
2021-09-27 08:36:28 +00:00
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid authMethod: '%s'", in)
|
2021-09-27 08:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-11 09:46:40 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
// UnmarshalEnv implements env.Unmarshaler.
|
2024-03-04 13:20:34 +00:00
|
|
|
func (d *AuthMethod) UnmarshalEnv(_ string, v string) error {
|
|
|
|
return d.UnmarshalJSON([]byte(`"` + v + `"`))
|
2021-10-11 09:46:40 +00:00
|
|
|
}
|