Skip to content
This repository was archived by the owner on Feb 16, 2022. It is now read-only.

Commit 4fa0021

Browse files
authored
Merge pull request #213 from Widcket/fix/lifetime_in_seconds
Fix JSON encode/decode of lifetime_in_seconds
2 parents a45c51d + 82e33c2 commit 4fa0021

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

management/client.go

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package management
22

3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"strconv"
7+
8+
"gopkg.in/auth0.v5"
9+
)
10+
311
type Client struct {
412
// The name of the client
513
Name *string `json:"name,omitempty"`
@@ -88,7 +96,7 @@ type Client struct {
8896

8997
type ClientJWTConfiguration struct {
9098
// The amount of seconds the JWT will be valid (affects exp claim)
91-
LifetimeInSeconds *int `json:"lifetime_in_seconds,omitempty"`
99+
LifetimeInSeconds *int `json:"-"`
92100

93101
// True if the client secret is base64 encoded, false otherwise. Defaults to
94102
// true
@@ -196,3 +204,56 @@ func (m *ClientManager) RotateSecret(id string, opts ...RequestOption) (c *Clien
196204
func (m *ClientManager) Delete(id string, opts ...RequestOption) error {
197205
return m.Request("DELETE", m.URI("clients", id), nil, opts...)
198206
}
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+
}

management/client_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package management
22

33
import (
4+
"encoding/json"
45
"strings"
56
"testing"
67
"time"
78

89
"gopkg.in/auth0.v5"
10+
"gopkg.in/auth0.v5/internal/testing/expect"
911
)
1012

1113
func TestClient(t *testing.T) {
@@ -78,3 +80,35 @@ func TestClient(t *testing.T) {
7880
}
7981
})
8082
}
83+
84+
func TestJWTConfiguration(t *testing.T) {
85+
86+
t.Run("MarshalJSON", func(t *testing.T) {
87+
for u, expected := range map[*ClientJWTConfiguration]string{
88+
{}: `{}`,
89+
{LifetimeInSeconds: auth0.Int(1000)}: `{"lifetime_in_seconds":1000}`,
90+
} {
91+
b, err := json.Marshal(u)
92+
if err != nil {
93+
t.Error(err)
94+
}
95+
expect.Expect(t, string(b), expected)
96+
}
97+
})
98+
99+
t.Run("UnmarshalJSON", func(t *testing.T) {
100+
for b, expected := range map[string]*ClientJWTConfiguration{
101+
`{}`: {LifetimeInSeconds: nil},
102+
`{"lifetime_in_seconds":1000}`: {LifetimeInSeconds: auth0.Int(1000)},
103+
`{"lifetime_in_seconds":"1000"}`: {LifetimeInSeconds: auth0.Int(1000)},
104+
} {
105+
var jc ClientJWTConfiguration
106+
err := json.Unmarshal([]byte(b), &jc)
107+
if err != nil {
108+
t.Error(err)
109+
}
110+
expect.Expect(t, jc.GetLifetimeInSeconds(), expected.GetLifetimeInSeconds())
111+
}
112+
})
113+
114+
}

0 commit comments

Comments
 (0)