loadBearerTokenWithSource function
Loads the bearer token with source information.
Returns a TokenLoadResult containing the token and its source.
Implementation
TokenLoadResult loadBearerTokenWithSource() {
// 1. Check environment variable
final envToken = _normalize(Platform.environment[AuthEnvVars.authToken]);
if (envToken != null) {
return TokenLoadResult(envToken, 'KUMIHO_AUTH_TOKEN env var');
}
// 2. Load from credentials file
final credentials = loadCredentials();
if (credentials == null) {
return const TokenLoadResult(null, null);
}
// 3. Check preference for Control Plane token
final preferCp = _envFlag(AuthEnvVars.useControlPlaneToken);
if (preferCp && credentials.controlPlaneToken != null) {
return TokenLoadResult(
credentials.controlPlaneToken,
'Control Plane token from credentials file',
);
}
// 4. Prefer Firebase token
if (credentials.idToken.isNotEmpty) {
return TokenLoadResult(
credentials.idToken,
'Firebase ID token from credentials file',
);
}
// 5. Fallback to Control Plane token
if (credentials.controlPlaneToken != null) {
return TokenLoadResult(
credentials.controlPlaneToken,
'Control Plane token from credentials file',
);
}
return const TokenLoadResult(null, null);
}