From 4436df520fa84483e2eed0f6e470fd9620f2eefb Mon Sep 17 00:00:00 2001 From: Simon Lightfoot Date: Thu, 28 Nov 2024 20:28:11 +0000 Subject: [PATCH] fix: use utc for token expiry timestamps #47 (#49) --- packages/clerk_auth/lib/src/clerk_api/api.dart | 2 +- .../clerk_auth/lib/src/clerk_api/token_cache.dart | 12 ++++++++---- packages/clerk_auth/lib/src/clerk_auth/auth.dart | 2 +- packages/clerk_auth/lib/src/models/helpers.dart | 2 +- .../test/integration/clerk_api/sign_in_test.dart | 5 +++-- .../test/integration/clerk_api/sign_up_test.dart | 5 +++-- .../integration/clerk_api/user_details_test.dart | 5 +++-- .../test/integration/clerk_auth/sign_in_test.dart | 2 +- .../test/integration/clerk_auth/sign_up_test.dart | 2 +- 9 files changed, 22 insertions(+), 15 deletions(-) diff --git a/packages/clerk_auth/lib/src/clerk_api/api.dart b/packages/clerk_auth/lib/src/clerk_api/api.dart index 468afcf..90de92d 100644 --- a/packages/clerk_auth/lib/src/clerk_api/api.dart +++ b/packages/clerk_auth/lib/src/clerk_api/api.dart @@ -494,7 +494,7 @@ class Api with Logging { await sessionToken(); // make sure updated - final diff = _tokenCache.sessionTokenExpiry.difference(DateTime.now()); + final diff = _tokenCache.sessionTokenExpiry.difference(DateTime.timestamp()); final delay = diff.isNegative ? const Duration(seconds: 55) : diff; _pollTimer = Timer(delay, _pollForSessionToken); } diff --git a/packages/clerk_auth/lib/src/clerk_api/token_cache.dart b/packages/clerk_auth/lib/src/clerk_api/token_cache.dart index 8c89f00..27380f5 100644 --- a/packages/clerk_auth/lib/src/clerk_api/token_cache.dart +++ b/packages/clerk_auth/lib/src/clerk_api/token_cache.dart @@ -18,7 +18,8 @@ class TokenCache { final String _publicKey; final Persistor _persistor; - DateTime _sessionTokenExpiry = DateTime.fromMillisecondsSinceEpoch(0); + DateTime _sessionTokenExpiry = + DateTime.fromMillisecondsSinceEpoch(0, isUtc: true); /// the date at which, if in the future, the current [sessionToken] /// is due to expire @@ -38,7 +39,7 @@ class TokenCache { clientToken.isNotEmpty && sessionId.isNotEmpty; bool get _sessionTokenHasExpired => - DateTime.now().isAfter(sessionTokenExpiry); + DateTime.timestamp().isAfter(sessionTokenExpiry); String get _sessionIdKey => '_clerkSessionId_${_publicKey.hashCode}'; @@ -67,6 +68,7 @@ class TokenCache { final milliseconds = await _persistor.read(_sessionTokenExpiryKey) ?? ''; final sessionTokenExpiry = DateTime.fromMillisecondsSinceEpoch( int.tryParse(milliseconds) ?? 0, + isUtc: true, ); _sessionId = sessionId; @@ -81,7 +83,7 @@ class TokenCache { _sessionId = ''; _clientToken = ''; _sessionToken = ''; - _sessionTokenExpiry = DateTime.fromMillisecondsSinceEpoch(0); + _sessionTokenExpiry = DateTime.fromMillisecondsSinceEpoch(0, isUtc: true); for (final key in _persistorKeys) { _persistor.delete(key); } @@ -127,7 +129,9 @@ class TokenCache { final expirySeconds = jwt.payload['exp']; if (expirySeconds is int) { final expiry = DateTime.fromMillisecondsSinceEpoch( - expirySeconds * Duration.millisecondsPerSecond); + expirySeconds * Duration.millisecondsPerSecond, + isUtc: true, + ); _sessionTokenExpiry = expiry.subtract(_tokenExpiryBuffer); _sessionToken = token; _persistor.write(_sessionTokenKey, token); diff --git a/packages/clerk_auth/lib/src/clerk_auth/auth.dart b/packages/clerk_auth/lib/src/clerk_auth/auth.dart index 865050c..b8af411 100644 --- a/packages/clerk_auth/lib/src/clerk_auth/auth.dart +++ b/packages/clerk_auth/lib/src/clerk_auth/auth.dart @@ -353,7 +353,7 @@ class Auth { if (client.user is User) return client; final expiry = client.signIn?.firstFactorVerification?.expireAt; - if (expiry?.isAfter(DateTime.now()) != true) { + if (expiry?.isAfter(DateTime.timestamp()) != true) { throw AuthError( message: 'Awaited user action not completed in required timeframe'); } diff --git a/packages/clerk_auth/lib/src/models/helpers.dart b/packages/clerk_auth/lib/src/models/helpers.dart index fa529f7..0049215 100644 --- a/packages/clerk_auth/lib/src/models/helpers.dart +++ b/packages/clerk_auth/lib/src/models/helpers.dart @@ -3,7 +3,7 @@ import 'package:clerk_auth/clerk_auth.dart'; /// Convert a value to a [DateTime?]. For JsonSerializable DateTime? intToDateTime(dynamic input) { if (input is num) { - return DateTime.fromMillisecondsSinceEpoch(input.toInt()); + return DateTime.fromMillisecondsSinceEpoch(input.toInt(), isUtc: true); } return null; } diff --git a/packages/clerk_auth/test/integration/clerk_api/sign_in_test.dart b/packages/clerk_auth/test/integration/clerk_api/sign_in_test.dart index 0bf78f5..03ba4c1 100644 --- a/packages/clerk_auth/test/integration/clerk_api/sign_in_test.dart +++ b/packages/clerk_auth/test/integration/clerk_api/sign_in_test.dart @@ -9,8 +9,9 @@ void main() { late final Api api; late final TestEnv env; final httpClient = TestHttpClient(); - final expireAt = - DateTime.now().add(const Duration(minutes: 5)).millisecondsSinceEpoch; + final expireAt = DateTime.timestamp() // + .add(const Duration(minutes: 5)) + .millisecondsSinceEpoch; setUpAll(() async { env = TestEnv('.env.test'); diff --git a/packages/clerk_auth/test/integration/clerk_api/sign_up_test.dart b/packages/clerk_auth/test/integration/clerk_api/sign_up_test.dart index 2b92381..1665877 100644 --- a/packages/clerk_auth/test/integration/clerk_api/sign_up_test.dart +++ b/packages/clerk_auth/test/integration/clerk_api/sign_up_test.dart @@ -14,8 +14,9 @@ void main() { late final Api api; late final TestEnv env; final httpClient = TestHttpClient(); - final expireAt = - DateTime.now().add(const Duration(minutes: 5)).millisecondsSinceEpoch; + final expireAt = DateTime.timestamp() // + .add(const Duration(minutes: 5)) + .millisecondsSinceEpoch; String emailAddress = ''; String phoneNumber = ''; diff --git a/packages/clerk_auth/test/integration/clerk_api/user_details_test.dart b/packages/clerk_auth/test/integration/clerk_api/user_details_test.dart index e4701ea..8019a02 100644 --- a/packages/clerk_auth/test/integration/clerk_api/user_details_test.dart +++ b/packages/clerk_auth/test/integration/clerk_api/user_details_test.dart @@ -14,8 +14,9 @@ void main() { late final Api api; late final TestEnv env; final httpClient = TestHttpClient(); - final expireAt = - DateTime.now().add(const Duration(minutes: 5)).millisecondsSinceEpoch; + final expireAt = DateTime.timestamp() // + .add(const Duration(minutes: 5)) + .millisecondsSinceEpoch; setUpAll(() async { env = TestEnv('.env.test'); diff --git a/packages/clerk_auth/test/integration/clerk_auth/sign_in_test.dart b/packages/clerk_auth/test/integration/clerk_auth/sign_in_test.dart index a2e0a3e..cb6fa2b 100644 --- a/packages/clerk_auth/test/integration/clerk_auth/sign_in_test.dart +++ b/packages/clerk_auth/test/integration/clerk_auth/sign_in_test.dart @@ -8,7 +8,7 @@ void main() { late final Auth auth; late final TestEnv env; final httpClient = TestHttpClient(); - final expireAt = DateTime.now() // + final expireAt = DateTime.timestamp() // .add(const Duration(minutes: 5)) .millisecondsSinceEpoch; diff --git a/packages/clerk_auth/test/integration/clerk_auth/sign_up_test.dart b/packages/clerk_auth/test/integration/clerk_auth/sign_up_test.dart index d38e434..db78fcb 100644 --- a/packages/clerk_auth/test/integration/clerk_auth/sign_up_test.dart +++ b/packages/clerk_auth/test/integration/clerk_auth/sign_up_test.dart @@ -9,7 +9,7 @@ void main() { late final Auth auth; late final TestEnv env; final httpClient = TestHttpClient(); - final expireAt = DateTime.now() // + final expireAt = DateTime.timestamp() // .add(const Duration(minutes: 5)) .millisecondsSinceEpoch;