Skip to content

Commit 233b179

Browse files
authored
fix(core): Add proper handling of expired credentials (#2637)
1 parent 327e451 commit 233b179

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

AmplifyPlugins/Core/AWSPluginsCore/Auth/Provider/AmplifyAWSCredentialsProvider.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ extension AWSCredentials {
2727

2828
func toAWSSDKCredentials() -> AWSClientRuntime.AWSCredentials {
2929
if let tempCredentials = self as? AWSTemporaryCredentials {
30+
31+
let expirationTimeSinceNow = tempCredentials.expiration.timeIntervalSinceNow
32+
let expirationTimeout = UInt64(max(0, expirationTimeSinceNow))
33+
3034
return AWSClientRuntime.AWSCredentials(
3135
accessKey: tempCredentials.accessKeyId,
3236
secret: tempCredentials.secretAccessKey,
33-
expirationTimeout: UInt64(tempCredentials.expiration.timeIntervalSinceNow),
37+
expirationTimeout: expirationTimeout,
3438
sessionToken: tempCredentials.sessionToken)
3539
} else {
3640
return AWSClientRuntime.AWSCredentials(

AmplifyPlugins/Core/AWSPluginsCoreTests/Auth/AWSAuthServiceTests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import XCTest
99

1010
@testable import Amplify
1111
@testable import AWSPluginsCore
12+
import AWSClientRuntime
1213

1314
class AWSAuthServiceTests: XCTestCase {
1415

@@ -153,4 +154,41 @@ class AWSAuthServiceTests: XCTestCase {
153154

154155
XCTAssertEqual(iat, 1_551_307_661)
155156
}
157+
158+
159+
/// Given: A credentials that will expire after 100 second
160+
/// When: I convert the credentials to AWS SDK ClientRuntime
161+
/// Then: I should get a valid CRT credentials
162+
func testValidCredentialsToCRTConversion() throws {
163+
164+
let credentials = MockCredentials(
165+
sessionToken: "somesession",
166+
accessKeyId: "accessKeyId",
167+
secretAccessKey: "secretAccessKey",
168+
expiration: Date().addingTimeInterval(100))
169+
let sdkCredentials = credentials.toAWSSDKCredentials()
170+
XCTAssertNotNil(sdkCredentials)
171+
}
172+
173+
/// Given: A credentials that expired 100 second back
174+
/// When: I convert the credentials to AWS SDK ClientRuntime
175+
/// Then: I should get a valid CRT credentials
176+
func testExpiredCredentialsToCRTConversion() throws {
177+
178+
let credentials = MockCredentials(
179+
sessionToken: "somesession",
180+
accessKeyId: "accessKeyId",
181+
secretAccessKey: "secretAccessKey",
182+
expiration: Date().addingTimeInterval(-100))
183+
let sdkCredentials = credentials.toAWSSDKCredentials()
184+
XCTAssertNotNil(sdkCredentials)
185+
}
186+
}
187+
188+
189+
struct MockCredentials: AWSTemporaryCredentials {
190+
let sessionToken: String
191+
let accessKeyId: String
192+
let secretAccessKey: String
193+
let expiration: Date
156194
}

0 commit comments

Comments
 (0)