Skip to main content

Authentication with the Dart SDK

EthioConnect authenticates client requests with a short-lived access token (a JWT) sent as a Bearer token. Your users sign in to your app however you like; your backend then mints an EthioConnect token for them with your API key, and the Flutter app passes it to the SDK.

Backend-minted tokens​

Your backend holds the tenant API key and mints tokens on behalf of your own users. The Flutter app never sees the API key — it asks your backend for a token and returns it from the tokenProvider:

import 'package:commbaas/commbaas.dart';

final client = CommBaasClient(
baseUrl: 'https://rtc.ethioconnect.net/v1',
tokenProvider: () async {
// Your own backend endpoint, protected by your own app auth.
return myBackend.fetchEthioConnectToken();
},
);

On your backend, mint the token with the server SDK (see the Quickstart):

const { access_token, expires_in } = await server.auth.mintToken({
user_id: currentUser.id,
display_name: currentUser.name,
});
No mintToken on the client

Minting requires the tenant API key, which must never ship in a client app. The client SDK therefore deliberately has no mintToken method — minting belongs to your backend.

Token handling​

The SDK calls your tokenProvider whenever it needs a token and never caches or stores the result, so your closure is the single source of truth.

  • Cache in memory, refresh before expiry. Tokens are short-lived — your backend gets expires_in (seconds) alongside each one. Cache the token in memory and fetch a new one shortly before it expires, or when a request fails with AuthenticationException (401).
  • Don't persist tokens. There is no need to write EthioConnect tokens to disk; fetch a fresh one from your backend after an app restart, using your own app session.
  • Never embed the API key (cb_<prefix>.<secret>) in a Flutter app, including web builds.
class EthioConnectTokens {
EthioConnectTokens(this.backend);
final MyBackend backend;

String? _token;
DateTime _expiresAt = DateTime.fromMillisecondsSinceEpoch(0);

/// Use as the CommBaasClient tokenProvider.
Future<String> get() async {
// Refresh 60 s early so a token never expires mid-request.
if (_token == null ||
DateTime.now().isAfter(_expiresAt.subtract(const Duration(seconds: 60)))) {
final res = await backend.fetchEthioConnectToken(); // { token, expires_in }
_token = res.token;
_expiresAt = DateTime.now().add(Duration(seconds: res.expiresIn));
}
return _token!;
}

/// Call on AuthenticationException (401), then retry the request once.
void invalidate() => _token = null;
}

Under the hood​

Your backend calls the mint endpoint with the API key:

POST /v1/auth/token
Authorization: ApiKey cb_<prefix>.<secret>

{ "user_id": "usr_123", "display_name": "Alice" }

200 → { "access_token": "...", "expires_in": 900, "token_type": "Bearer" }

The SDK then attaches Authorization: Bearer <token> to every request, calling your tokenProvider each time.

Verifying tokens (backend note): EthioConnect tokens are signed with EdDSA (Ed25519). Your backend can verify them against the JWKS document at GET /.well-known/jwks.json. Flutter clients never need to verify tokens — they only present them.