2021-09-27 13:45:51 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-04-09 12:06:25 +00:00
|
|
|
var reCredential = regexp.MustCompile(`^[a-zA-Z0-9!\$\(\)\*\+\.;<=>\[\]\^_\-\{\}@#&]+$`)
|
2021-09-27 13:45:51 +00:00
|
|
|
|
2023-04-09 12:06:25 +00:00
|
|
|
const credentialSupportedChars = "A-Z,0-9,!,$,(,),*,+,.,;,<,=,>,[,],^,_,-,\",\",@,#,&"
|
2021-09-27 13:45:51 +00:00
|
|
|
|
|
|
|
// Credential is a parameter that is used as username or password.
|
|
|
|
type Credential string
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// MarshalJSON implements json.Marshaler.
|
2021-09-27 13:45:51 +00:00
|
|
|
func (d Credential) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(string(d))
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:41:15 +00:00
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
2021-09-27 13:45:51 +00:00
|
|
|
func (d *Credential) UnmarshalJSON(b []byte) error {
|
|
|
|
var in string
|
|
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if in != "" &&
|
|
|
|
!strings.HasPrefix(in, "sha256:") &&
|
|
|
|
!reCredential.MatchString(in) {
|
2023-04-09 12:06:25 +00:00
|
|
|
return fmt.Errorf("credential contains unsupported characters. Supported are: %s", credentialSupportedChars)
|
2021-09-27 13:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*d = Credential(in)
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-11 09:46:40 +00:00
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
// UnmarshalEnv implements envUnmarshaler.
|
|
|
|
func (d *Credential) UnmarshalEnv(s string) error {
|
2021-10-11 09:46:40 +00:00
|
|
|
return d.UnmarshalJSON([]byte(`"` + s + `"`))
|
|
|
|
}
|