From 85ba41cef689b852e102a65ec6550580489fb4bc Mon Sep 17 00:00:00 2001 From: Andreas Roth Date: Wed, 27 Nov 2024 10:17:52 +0100 Subject: [PATCH] feat: Added `OidcTokenExpiredEvent` and `OidcTokenExpiringEvent` (#91) * Added callbacks onTokenExpiring and onTokenExpired * Replaced callbacks onTokenExpiring and onTokenExpired by event types OidcTokenExpiringEvent/OidcTokenExpiredEvent * Do not try to refresh token if not supported by server * format --------- Co-authored-by: ahmednfwela --- .../lib/src/managers/user_manager_base.dart | 23 +++++++++++-------- .../lib/src/models/events/_exports.dart | 2 ++ .../models/events/token_expired_event.dart | 18 +++++++++++++++ .../models/events/token_expiring_event.dart | 18 +++++++++++++++ 4 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 packages/oidc_core/lib/src/models/events/token_expired_event.dart create mode 100644 packages/oidc_core/lib/src/models/events/token_expiring_event.dart diff --git a/packages/oidc_core/lib/src/managers/user_manager_base.dart b/packages/oidc_core/lib/src/managers/user_manager_base.dart index 86bdb31c..afc0a28d 100644 --- a/packages/oidc_core/lib/src/managers/user_manager_base.dart +++ b/packages/oidc_core/lib/src/managers/user_manager_base.dart @@ -784,15 +784,6 @@ abstract class OidcUserManagerBase { if (user == null) { tokenEventsManager.unload(); } else { - if (!discoveryDocument.grantTypesSupportedOrDefault - .contains(OidcConstants_GrantType.refreshToken)) { - //Server doesn't support refresh_token grant. - return; - } - if (user.token.refreshToken == null) { - // Can't refresh the access token anyway. - return; - } if (user.token.expiresIn == null) { // Can't know how much time is left. return; @@ -803,6 +794,16 @@ abstract class OidcUserManagerBase { @protected Future handleTokenExpiring(OidcToken event) async { + eventsController.add( + OidcTokenExpiringEvent.now(currentToken: event), + ); + + if (!discoveryDocument.grantTypesSupportedOrDefault + .contains(OidcConstants_GrantType.refreshToken)) { + //Server doesn't support refresh_token grant. + return; + } + final refreshToken = event.refreshToken; if (refreshToken == null) { return; @@ -843,6 +844,10 @@ abstract class OidcUserManagerBase { @protected void handleTokenExpired(OidcToken event) { + eventsController.add( + OidcTokenExpiredEvent.now(currentToken: event), + ); + if (!settings.supportOfflineAuth) { forgetUser(); } diff --git a/packages/oidc_core/lib/src/models/events/_exports.dart b/packages/oidc_core/lib/src/models/events/_exports.dart index 432099d2..a3ee6696 100644 --- a/packages/oidc_core/lib/src/models/events/_exports.dart +++ b/packages/oidc_core/lib/src/models/events/_exports.dart @@ -1,2 +1,4 @@ export 'event.dart'; export 'pre_logout_event.dart'; +export 'token_expired_event.dart'; +export 'token_expiring_event.dart'; diff --git a/packages/oidc_core/lib/src/models/events/token_expired_event.dart b/packages/oidc_core/lib/src/models/events/token_expired_event.dart new file mode 100644 index 00000000..3eba93f3 --- /dev/null +++ b/packages/oidc_core/lib/src/models/events/token_expired_event.dart @@ -0,0 +1,18 @@ +import 'package:oidc_core/oidc_core.dart'; + +/// An event that gets raised after a token expired. +class OidcTokenExpiredEvent extends OidcEvent { + /// + const OidcTokenExpiredEvent({ + required this.currentToken, + required super.at, + }); + + /// + OidcTokenExpiredEvent.now({ + required this.currentToken, + }) : super.now(); + + /// The current token that is expired. + final OidcToken currentToken; +} diff --git a/packages/oidc_core/lib/src/models/events/token_expiring_event.dart b/packages/oidc_core/lib/src/models/events/token_expiring_event.dart new file mode 100644 index 00000000..76566329 --- /dev/null +++ b/packages/oidc_core/lib/src/models/events/token_expiring_event.dart @@ -0,0 +1,18 @@ +import 'package:oidc_core/oidc_core.dart'; + +/// An event that gets raised before a token expires. +class OidcTokenExpiringEvent extends OidcEvent { + /// + const OidcTokenExpiringEvent({ + required this.currentToken, + required super.at, + }); + + /// + OidcTokenExpiringEvent.now({ + required this.currentToken, + }) : super.now(); + + /// The current token that is expiring. + final OidcToken currentToken; +}