authenticate method

  1. @visibleForTesting
Future<void> authenticate()

Resolves the credential, lazily and on every request.

Lazily, because constructing a backend must not require a network call or an unlocked settings container — the app wires storage up long before the user opens anything from GitHub.

On every request rather than once, because the credential is the user's to change while the backend lives: a token replaced in the Settings area or by a sign-in must be the one the next request carries, and a revoked one — a sign-out — must stop being carried then, not when the service gets round to refusing it. The source is asked each time (an in-memory lookup on every root); the client is rebuilt only when the secret differs, so its own conditional-request state survives an unchanged answer. A backend built TomGitHubStorage.withClient has no source and keeps the client it was given.

Implementation

@visibleForTesting
Future<void> authenticate() async {
  final tokens = _tokens;
  if (tokens == null) return;
  final token = await tokens.tokenFor(host);
  if (token == null) {
    _dropClient();
    throw const TomTokenRequiredException(host);
  }
  if (_client != null && _clientSecret == token.secret) return;
  _dropClient();
  _client = _clientFactory!(token.secret);
  _clientSecret = token.secret;
}