Skip to content

Commit d7a7f5a

Browse files
authored
save sso cache token expiresAt in UTC (#2709)
1 parent 87cea8b commit d7a7f5a

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "ebcf2e12-f2ba-4d0f-9aa4-4b48b850c41f",
3+
"type": "bugfix",
4+
"description": "Save SSO cached token expiry in UTC to ensure cross-SDK compatibility.",
5+
"modules": [
6+
"credentials"
7+
]
8+
}

credentials/ssocreds/sso_cached_token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func (r *rfc3339) UnmarshalJSON(bytes []byte) (err error) {
225225
}
226226

227227
func (r *rfc3339) MarshalJSON() ([]byte, error) {
228-
value := time.Time(*r).Format(time.RFC3339)
228+
value := time.Time(*r).UTC().Format(time.RFC3339)
229229

230230
// Use JSON unmarshal to unescape the quoted value making use of JSON's
231231
// quoting rules.

credentials/ssocreds/sso_cached_token_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ func TestLoadCachedToken(t *testing.T) {
102102
},
103103
},
104104
},
105+
"non-utc token": {
106+
filename: filepath.Join("testdata", "non_utc_token.json"),
107+
expectToken: token{
108+
tokenKnownFields: tokenKnownFields{
109+
AccessToken: "dGhpcyBpcyBub3QgYSByZWFsIHZhbHVl",
110+
ExpiresAt: (*rfc3339)(aws.Time(time.Date(2044, 4, 4, 7, 0, 1, 0, time.UTC))),
111+
ClientID: "client id",
112+
ClientSecret: "client secret",
113+
RefreshToken: "refresh token",
114+
},
115+
UnknownFields: map[string]interface{}{
116+
"unknownField": "some value",
117+
"registrationExpiresAt": "2044-04-04T07:00:01Z",
118+
"region": "region",
119+
"startURL": "start URL",
120+
},
121+
},
122+
},
105123
}
106124

107125
for name, c := range cases {
@@ -120,7 +138,7 @@ func TestLoadCachedToken(t *testing.T) {
120138
t.Fatalf("expect no error, got %v", err)
121139
}
122140

123-
if diff := cmpDiff(c.expectToken, actualToken); diff != "" {
141+
if diff := cmpDiffToken(c.expectToken, actualToken); diff != "" {
124142
t.Errorf("expect tokens match\n%s", diff)
125143
}
126144
})
@@ -162,6 +180,25 @@ func TestStoreCachedToken(t *testing.T) {
162180
},
163181
},
164182
},
183+
"non-utc token": {
184+
filename: filepath.Join(tempDir, "token_file.json"),
185+
fileMode: 0600,
186+
token: token{
187+
tokenKnownFields: tokenKnownFields{
188+
AccessToken: "dGhpcyBpcyBub3QgYSByZWFsIHZhbHVl",
189+
ExpiresAt: (*rfc3339)(aws.Time(time.Date(2044, 4, 4, 7, 0, 1, 0, time.FixedZone("UTC-8", -8*60*60)))),
190+
ClientID: "client id",
191+
ClientSecret: "client secret",
192+
RefreshToken: "refresh token",
193+
},
194+
UnknownFields: map[string]interface{}{
195+
"unknownField": "some value",
196+
"registrationExpiresAt": "2044-04-04T07:00:01Z",
197+
"region": "region",
198+
"startURL": "start URL",
199+
},
200+
},
201+
},
165202
}
166203

167204
for name, c := range cases {
@@ -176,7 +213,7 @@ func TestStoreCachedToken(t *testing.T) {
176213
t.Fatalf("failed to load stored token, %v", err)
177214
}
178215

179-
if diff := cmpDiff(c.token, actual); diff != "" {
216+
if diff := cmpDiffToken(c.token, actual); diff != "" {
180217
t.Errorf("expect tokens match\n%s", diff)
181218
}
182219
})

credentials/ssocreds/sso_token_provider_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func TestSSOTokenProvider(t *testing.T) {
109109
},
110110
}
111111

112-
if diff := cmpDiff(expect, actual); diff != "" {
112+
if diff := cmpDiffToken(expect, actual); diff != "" {
113113
return fmt.Errorf("expect token file match\n%s", diff)
114114
}
115115
return nil
@@ -233,3 +233,19 @@ func cmpDiff(e, a interface{}) string {
233233
}
234234
return ""
235235
}
236+
237+
func cmpDiffToken(e token, a token) string {
238+
if !reflect.DeepEqual(e.UnknownFields, a.UnknownFields) {
239+
return fmt.Sprintf("%v != %v", e, a)
240+
}
241+
// treats token times as the same if they are the same in UTC
242+
if time.Time(*e.ExpiresAt).UTC() != time.Time(*a.ExpiresAt).UTC() {
243+
return fmt.Sprintf("%v != %v", e, a)
244+
}
245+
eTokenKnownFields := e.tokenKnownFields
246+
eTokenKnownFields.ExpiresAt = a.tokenKnownFields.ExpiresAt
247+
if !reflect.DeepEqual(eTokenKnownFields, a.tokenKnownFields) {
248+
return fmt.Sprintf("%v != %v", e, a)
249+
}
250+
return ""
251+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"accessToken": "dGhpcyBpcyBub3QgYSByZWFsIHZhbHVl",
3+
"expiresAt": "2044-04-04T00:00:01-07:00",
4+
5+
"refreshToken": "refresh token",
6+
"clientId": "client id",
7+
"clientSecret": "client secret",
8+
9+
"unknownField": "some value",
10+
"region": "region",
11+
"registrationExpiresAt": "2044-04-04T07:00:01Z",
12+
"startURL": "start URL"
13+
}

0 commit comments

Comments
 (0)