Skip to content

Commit

Permalink
feat: Added OidcTokenExpiredEvent and OidcTokenExpiringEvent (#91)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
akaegi and ahmednfwela authored Nov 27, 2024
1 parent 37e5bff commit 85ba41c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
23 changes: 14 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,16 @@ abstract class OidcUserManagerBase {

@protected
Future<void> 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;
Expand Down Expand Up @@ -843,6 +844,10 @@ abstract class OidcUserManagerBase {

@protected
void handleTokenExpired(OidcToken event) {
eventsController.add(
OidcTokenExpiredEvent.now(currentToken: event),
);

if (!settings.supportOfflineAuth) {
forgetUser();
}
Expand Down
2 changes: 2 additions & 0 deletions packages/oidc_core/lib/src/models/events/_exports.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export 'event.dart';
export 'pre_logout_event.dart';
export 'token_expired_event.dart';
export 'token_expiring_event.dart';
18 changes: 18 additions & 0 deletions packages/oidc_core/lib/src/models/events/token_expired_event.dart
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions packages/oidc_core/lib/src/models/events/token_expiring_event.dart
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 85ba41c

Please sign in to comment.