Skip to content

Commit

Permalink
fix: use utc for token expiry timestamps #47 (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
slightfoot authored Nov 28, 2024
1 parent 774a299 commit 4436df5
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/clerk_auth/lib/src/clerk_api/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 8 additions & 4 deletions packages/clerk_auth/lib/src/clerk_api/token_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}';

Expand Down Expand Up @@ -67,6 +68,7 @@ class TokenCache {
final milliseconds = await _persistor.read(_sessionTokenExpiryKey) ?? '';
final sessionTokenExpiry = DateTime.fromMillisecondsSinceEpoch(
int.tryParse(milliseconds) ?? 0,
isUtc: true,
);

_sessionId = sessionId;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk_auth/lib/src/clerk_auth/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/clerk_auth/lib/src/models/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 4436df5

Please sign in to comment.