fix: allow RFC3339 encoded time strings

Fixes #292
This commit is contained in:
Tim Möhlmann 2023-03-22 15:29:41 +02:00
parent 95ffcb5bdc
commit a64b97dd5a
2 changed files with 76 additions and 5 deletions

View file

@ -127,12 +127,27 @@ func (s SpaceDelimitedArray) Value() (driver.Value, error) {
type Time time.Time
func (t *Time) UnmarshalJSON(data []byte) error {
var i int64
if err := json.Unmarshal(data, &i); err != nil {
return err
func (ts *Time) UnmarshalJSON(data []byte) error {
var v any
if err := json.Unmarshal(data, &v); err != nil {
return fmt.Errorf("oidc.Time: %w", err)
}
switch x := v.(type) {
case float64:
*ts = Time(time.Unix(int64(x), 0))
case string:
// Compatibility with Auth0:
// https://github.com/zitadel/oidc/issues/292
tt, err := time.Parse(time.RFC3339, x)
if err != nil {
return fmt.Errorf("oidc.Time: %w", err)
}
*ts = Time(tt.Round(time.Second))
case nil:
*ts = Time{}
default:
return fmt.Errorf("oidc.Time: unable to parse type %T with value %v", x, x)
}
*t = Time(time.Unix(i, 0).UTC())
return nil
}