|
1 | 1 | package management |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "gopkg.in/auth0.v5" |
| 9 | +) |
| 10 | + |
3 | 11 | type Client struct { |
4 | 12 | // The name of the client |
5 | 13 | Name *string `json:"name,omitempty"` |
@@ -88,7 +96,7 @@ type Client struct { |
88 | 96 |
|
89 | 97 | type ClientJWTConfiguration struct { |
90 | 98 | // The amount of seconds the JWT will be valid (affects exp claim) |
91 | | - LifetimeInSeconds *int `json:"lifetime_in_seconds,omitempty"` |
| 99 | + LifetimeInSeconds *int `json:"-"` |
92 | 100 |
|
93 | 101 | // True if the client secret is base64 encoded, false otherwise. Defaults to |
94 | 102 | // true |
@@ -196,3 +204,56 @@ func (m *ClientManager) RotateSecret(id string, opts ...RequestOption) (c *Clien |
196 | 204 | func (m *ClientManager) Delete(id string, opts ...RequestOption) error { |
197 | 205 | return m.Request("DELETE", m.URI("clients", id), nil, opts...) |
198 | 206 | } |
| 207 | + |
| 208 | +// UnmarshalJSON implements the json.Unmarshaler interface. |
| 209 | +// |
| 210 | +// It is required to handle the json field lifetime_in_seconds, which can either |
| 211 | +// be an int, or a string in older tenants. |
| 212 | +func (jc *ClientJWTConfiguration) UnmarshalJSON(b []byte) error { |
| 213 | + type clientJWTConfiguration ClientJWTConfiguration |
| 214 | + type clientJWTConfigurationWrapper struct { |
| 215 | + *clientJWTConfiguration |
| 216 | + RawLifetimeInSeconds interface{} `json:"lifetime_in_seconds,omitempty"` |
| 217 | + } |
| 218 | + |
| 219 | + alias := &clientJWTConfigurationWrapper{(*clientJWTConfiguration)(jc), nil} |
| 220 | + |
| 221 | + err := json.Unmarshal(b, alias) |
| 222 | + if err != nil { |
| 223 | + return err |
| 224 | + } |
| 225 | + |
| 226 | + unexpectedTypeError := fmt.Errorf("unexpected type for field lifetime_in_seconds") |
| 227 | + |
| 228 | + if alias.RawLifetimeInSeconds != nil { |
| 229 | + switch rawLifetimeInSeconds := alias.RawLifetimeInSeconds.(type) { |
| 230 | + case float64: |
| 231 | + jc.LifetimeInSeconds = auth0.Int(int(rawLifetimeInSeconds)) |
| 232 | + case string: |
| 233 | + value, err := strconv.Atoi(rawLifetimeInSeconds) |
| 234 | + if err != nil { |
| 235 | + return unexpectedTypeError |
| 236 | + } |
| 237 | + jc.LifetimeInSeconds = &value |
| 238 | + default: |
| 239 | + return unexpectedTypeError |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + return nil |
| 244 | +} |
| 245 | + |
| 246 | +func (jc *ClientJWTConfiguration) MarshalJSON() ([]byte, error) { |
| 247 | + type clientJWTConfiguration ClientJWTConfiguration |
| 248 | + type clientJWTConfigurationWrapper struct { |
| 249 | + *clientJWTConfiguration |
| 250 | + RawLifetimeInSeconds interface{} `json:"lifetime_in_seconds,omitempty"` |
| 251 | + } |
| 252 | + |
| 253 | + alias := &clientJWTConfigurationWrapper{(*clientJWTConfiguration)(jc), nil} |
| 254 | + if jc.LifetimeInSeconds != nil { |
| 255 | + alias.RawLifetimeInSeconds = jc.LifetimeInSeconds |
| 256 | + } |
| 257 | + |
| 258 | + return json.Marshal(alias) |
| 259 | +} |
0 commit comments