From 878f36f5c28883b38552d0d34cb88405d2b69847 Mon Sep 17 00:00:00 2001 From: Andreas Roth Date: Wed, 12 Jun 2024 14:20:00 +0200 Subject: [PATCH] feat: Added callbacks onTokenExpiring and onTokenExpired --- .../lib/src/managers/user_manager_base.dart | 13 ++++--------- .../src/models/settings/user_manager_settings.dart | 10 ++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) 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 86bdb31..80a7d28 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,8 @@ abstract class OidcUserManagerBase { @protected Future handleTokenExpiring(OidcToken event) async { + settings.onTokenExpiring?.call(event); + final refreshToken = event.refreshToken; if (refreshToken == null) { return; @@ -843,6 +836,8 @@ abstract class OidcUserManagerBase { @protected void handleTokenExpired(OidcToken event) { + settings.onTokenExpired?.call(event); + if (!settings.supportOfflineAuth) { forgetUser(); } diff --git a/packages/oidc_core/lib/src/models/settings/user_manager_settings.dart b/packages/oidc_core/lib/src/models/settings/user_manager_settings.dart index f4c820e..4f056e9 100644 --- a/packages/oidc_core/lib/src/models/settings/user_manager_settings.dart +++ b/packages/oidc_core/lib/src/models/settings/user_manager_settings.dart @@ -3,6 +3,8 @@ import 'package:oidc_core/oidc_core.dart'; /// The callback used to determine the `expiring` duration. typedef OidcRefreshBeforeCallback = Duration? Function(OidcToken token); +typedef OidcTokenCallback = void Function(OidcToken token); + /// The default refreshBefore function, which refreshes 1 minute before the token expires. Duration? defaultRefreshBefore(OidcToken token) { return const Duration(minutes: 1); @@ -35,6 +37,8 @@ class OidcUserManagerSettings { this.sessionManagementSettings = const OidcSessionManagementSettings(), this.getIdToken, this.supportOfflineAuth = false, + this.onTokenExpiring, + this.onTokenExpired, }); /// The default scopes @@ -127,6 +131,12 @@ class OidcUserManagerSettings { /// platform-specific options. final OidcPlatformSpecificOptions? options; + + /// Callback that is called before the token expires. + final OidcTokenCallback? onTokenExpiring; + + /// Callback that is called after the token expired. + final OidcTokenCallback? onTokenExpired; } ///