Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added callbacks onTokenExpiring and onTokenExpired #91

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is removed cause handleTokenExpiring should be called even if there is no refreshToken...

.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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we still refresh using refreshToken if the user provides their own onTokenExpiring callback?

also would it make sense to expose a stream of expiring/expired events instead of callbacks ?

Copy link
Contributor Author

@akaegi akaegi Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we still refresh using refreshToken if the user provides their own onTokenExpiring callback?

Maybe there are scenarios where the user has to do some preparatory work before refresh can happen?

Copy link
Contributor Author

@akaegi akaegi Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also would it make sense to expose a stream of expiring/expired events instead of callbacks ?

Whatever you prefer. I could also add events to the existing events stream using new subclasses of OidcEvent (OidcTokenExpiring and OidcTokenExpired)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahmednfwela Any news from your side? Are there any open points that prevent you from merging this PR? This is somehow important to me as I'm currently referencing my fork in our production app 🙈


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
Loading