token_manager_grpc
gRPC client interceptor for token_manager — attaches valid tokens to every call and, when the server rejects them, refreshes once and replays the call once.
Part of the dart_token_manager workspace. The core owns storage and refresh; this package only adapts it to
package:grpc.
Contents
- Install
- Quick start
- Unary and streaming calls
- The delegate
- Errors
- Client-streaming and bidirectional calls
- Migration from 0.1
- Reference
Install
dependencies:
token_manager: ^0.2.0
token_manager_grpc: ^0.2.0
Quick start
import 'package:grpc/grpc.dart' as grpc;
import 'package:token_manager/token_manager.dart';
import 'package:token_manager_grpc/token_manager_grpc.dart';
// Your refresher: exchange the refresh token for a new TokenSet.
final class MyRefresher implements TokenRefresher<TokenSet> {
const MyRefresher();
@override
Future<TokenSet> refresh(TokenSet current) async => current;
}
// Tells the interceptor how to carry the tokens.
final class BearerDelegate extends RefreshInterceptorDelegate<TokenSet> {
const BearerDelegate();
@override
grpc.CallOptions buildCallOptions(TokenSet? tokens) => grpc.CallOptions(
metadata: {
if (tokens != null) 'authorization': 'Bearer ${tokens.accessToken}',
},
);
}
grpc.Client buildClient(grpc.ClientChannel channel) {
final manager = TokenManager<TokenSet>.create(
store: InMemoryTokenStore<TokenSet>(), // or a persistent TokenStore
refresher: const MyRefresher(),
info: const TokenSetInfo(),
);
// Pass the same list to your generated client:
// GreeterClient(channel, interceptors: [...]).
return grpc.Client(
channel,
interceptors: [
GrpcTokenInterceptor<TokenSet>(
manager: manager,
delegate: const BearerDelegate(),
),
],
);
}
doc/getting_started.md walks through the same setup step by step.
Unary and streaming calls
Unary calls go through TokenManager.authorize, streaming calls through
TokenManager.authorizeStream. A call is replayed at most once, and concurrent rejections
share one refresh.
- Unary — rejected by
shouldRefresh: refresh, then replay once. A second rejection or any other error reaches the caller. - Streaming — reopened only before the first response, so an event is never delivered
twice. After the first response an error is passed on; resubscribe when
manager.statesreports the new session. - Pause, resume, cancel — all reach the gRPC call in flight.
cancel()during a refresh prevents the replay. headersandtrailers— follow the attempt that answered, which may be the replay. For a streaming call they resolve with the first response, error or end, or on cancel.
A streaming call is made when its response stream is listened to; a stream nobody listens to sends nothing.
The delegate
RefreshInterceptorDelegate has two members:
| Member | Role |
|---|---|
buildCallOptions(tokens) |
Returns the CallOptions (usually metadata) that carry the tokens; merged into the call's options. |
shouldRefresh(error) |
Returns true when the GrpcError means the server rejected the tokens. Default: UNAUTHENTICATED. |
Override shouldRefresh when your server uses another status, for example PERMISSION_DENIED:
import 'package:grpc/grpc.dart' as grpc;
import 'package:token_manager/token_manager.dart';
import 'package:token_manager_grpc/token_manager_grpc.dart';
final class StrictDelegate extends RefreshInterceptorDelegate<TokenSet> {
const StrictDelegate();
@override
grpc.CallOptions buildCallOptions(TokenSet? tokens) => grpc.CallOptions(
metadata: {
if (tokens != null) 'authorization': 'Bearer ${tokens.accessToken}',
},
);
@override
bool shouldRefresh(grpc.GrpcError? error) =>
error?.code == grpc.StatusCode.unauthenticated ||
error?.code == grpc.StatusCode.permissionDenied;
}
Errors
NotAuthenticated— there are no tokens (logged out); the call is not sent.InvalidGrant— the refresh was rejected for good; the session ended and the manager is signed out.Transient— the refresh failed for a reason worth retrying; the session is kept.- The original
GrpcError— any other failure, or a rejection after the replay.
Only Exceptions are handled; an Error thrown by the delegate or an invoker propagates.
Client-streaming and bidirectional calls
A call is reopened only when its request stream ended after at most one request, which is the
shape of a server-streaming call. A call that sent more than one request, or whose request stream
had not ended when the server rejected it, is never reopened, whatever its method type: the
original error reaches the caller. The rejected tokens are still reported to the manager
(validToken(rejected: ...)), so a refresh starts for the next call.
A bidirectional call must listen to its responses before it awaits headers: headers resolves
with the first response, error or end, so a response stream nobody listens to never resolves it.
Migration from 0.1
RefreshInterceptor is now GrpcTokenInterceptor (the old name is a deprecated subclass), refresh
failures reach the call, and signed-out calls fail instead of going out without credentials. See
doc/migration.md.
Reference
- doc/getting_started.md · doc/migration.md · CHANGELOG.md · example/main.dart
- Core package: token_manager
- Generate API docs:
cd packages/token_manager_grpc && dart doc .