Token class
An immutable OAuth-style token pair.
expiresAt is optional: not all auth schemes expose lifetime information.
When it's null, the token is considered non-expiring and proactive
refresh is disabled for it.
metadata carries arbitrary extra claims — tenant IDs, user IDs, feature
flags, or any non-standard JWT payload fields. tryParseJwt populates it
automatically from non-standard claims.
Constructors
Properties
- accessToken → String
-
Bearer token used to authenticate requests.
final
- expiresAt → DateTime?
-
Absolute expiry of accessToken.
nullmeans unknown / non-expiring.final - hashCode → int
-
The hash code for this object.
no setterinherited
- maskedAccessToken → String
-
Returns a partially-redacted form of accessToken suitable for logs.
no setter
-
metadata
→ Map<
String, dynamic> -
Arbitrary extra data associated with this token.
final
-
props
→ List<
Object?> -
The list of properties that will be used to determine whether
two instances are equal.
no setter
- refreshToken → String?
-
Long-lived token used to obtain a new accessToken. May be
nullfor flows that do not issue refresh tokens (e.g. client-credentials).final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
scopes
→ List<
String> -
Scopes granted with this token.
final
- stringify → bool
-
If set to
true, the toString method will be overridden to output this instance's props.no setter
Methods
-
copyWith(
{String? accessToken, String? refreshToken, DateTime? expiresAt, List< String> ? scopes, Map<String, dynamic> ? metadata, bool clearRefreshToken = false, bool clearExpiresAt = false}) → Token - Returns a copy with the supplied fields replaced.
-
expiresInSeconds(
[DateTime? now]) → int? -
Returns the number of whole seconds until expiresAt, mirroring the
OAuth 2.0
expires_infield used in token responses. -
hasAllScopes(
List< String> required) → bool -
Returns
trueif all ofrequiredare present in scopes. -
hasAnyScope(
List< String> any) → bool -
Returns
trueif at least one ofanyis present in scopes. -
hasScope(
String scope) → bool -
Returns
trueifscopeis present in scopes. -
isExpired(
[DateTime? now]) → bool -
Returns
trueif expiresAt is non-null and not in the future. -
isValid(
[DateTime? now]) → bool -
Returns
truewhen the token is NOT expired. -
isValidWithAllScopes(
List< String> required, [DateTime? now]) → bool -
Returns
trueif the token is valid and has all ofrequiredscopes. -
isValidWithAnyScope(
List< String> any, [DateTime? now]) → bool -
Returns
trueif the token is valid and has at least one ofanyscopes. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
remainingLifetime(
[DateTime? now]) → Duration? - Returns the time remaining until expiresAt.
-
requiresRefresh(
{Duration window = const Duration(minutes: 5), DateTime? now}) → bool -
Returns
trueif the token should be refreshed soon. -
toJson(
) → Map< String, dynamic> - JSON representation suitable for any TokenStorage backend.
-
toString(
) → String - A string representation of this object.
-
willExpireWithin(
Duration duration, [DateTime? now]) → bool -
Returns
trueif the token will expire withindurationfromnow.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
fromJsonOrNull(
Map< String, dynamic> json) → Token? -
Like fromJson but returns
nullon any parse error instead of throwing. Prefer this at storage boundaries where the persisted data may be stale or corrupt. -
tryParseJwt(
String jwt, {String? refreshToken}) → Token? -
Parses a JWT
jwtand returns a Token seeded with theexpandscope/scopes/scpclaims found in the payload.