Skip to content

Commit

Permalink
feat: Added callbacks onTokenExpiring and onTokenExpired
Browse files Browse the repository at this point in the history
  • Loading branch information
akaegi committed Jun 12, 2024
1 parent 5a5b31d commit 878f36f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
13 changes: 4 additions & 9 deletions packages/oidc_core/lib/src/managers/user_manager_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -803,6 +794,8 @@ abstract class OidcUserManagerBase {

@protected
Future<void> handleTokenExpiring(OidcToken event) async {
settings.onTokenExpiring?.call(event);

final refreshToken = event.refreshToken;
if (refreshToken == null) {
return;
Expand Down Expand Up @@ -843,6 +836,8 @@ abstract class OidcUserManagerBase {

@protected
void handleTokenExpired(OidcToken event) {
settings.onTokenExpired?.call(event);

if (!settings.supportOfflineAuth) {
forgetUser();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -35,6 +37,8 @@ class OidcUserManagerSettings {
this.sessionManagementSettings = const OidcSessionManagementSettings(),
this.getIdToken,
this.supportOfflineAuth = false,
this.onTokenExpiring,
this.onTokenExpired,
});

/// The default scopes
Expand Down Expand Up @@ -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;
}

///
Expand Down

0 comments on commit 878f36f

Please sign in to comment.