bearerToken static method

Future<FunctionAuth> bearerToken(
  1. String path, {
  2. String? section,
  3. String tokenKey = 'token',
  4. String headerName = 'Authorization',
  5. String headerPrefix = 'Bearer ',
})

Load a JSON file and create a FunctionAuth that sets a Bearer token via the Authorization header.

Implementation

static Future<FunctionAuth> bearerToken(
  String path, {
  String? section,
  String tokenKey = 'token',
  String headerName = 'Authorization',
  String headerPrefix = 'Bearer ',
}) async {
  final data = await _load(path, section);
  final token = data[tokenKey];
  if (token == null) {
    throw FileSecretsError(
      'Missing "$tokenKey" in ${_desc(path, section)}',
    );
  }
  return FunctionAuth((req) {
    return req.copyWith(
      headers: req.headers.copy()..[headerName] = '$headerPrefix$token',
    );
  });
}