signedHeaders method

  1. @override
Map<String, String> signedHeaders(
  1. String path, {
  2. String? method = 'GET',
  3. Map<String, dynamic>? query,
  4. Map<String, dynamic>? headers,
  5. String? body,
  6. String? dateTime,
  7. String? encoding,
  8. bool signPayload = true,
  9. bool chunked = false,
})
override

Generates SIGV4-signed headers.

  • path: The complete path of your request
  • method: The HTTP verb your request is using
  • query: Query parameters, if any. required to be included if used
  • headers: Any additional headers. DO NOT add headers to your request after generating signed headers
  • body: An encodable object
  • dateTime: An AWS-compatible time string. You'll probably want to leave it blank.
  • encoding: The payload encoding. if any
  • signPayload: If the optional payload should be signed or unsigned

Implementation

@override
Map<String, String> signedHeaders(
  String path, {
  String? method = 'GET',
  Map<String, dynamic>? query,
  Map<String, dynamic>? headers,
  String? body,
  String? dateTime,
  String? encoding,
  bool signPayload = true,
  bool chunked = false,
}) {
  if (path.isEmpty) {
    throw AssertionError('path is empty');
  }

  /// Split the URI into segments
  final parsedUri = Uri.parse(path);

  /// The endpoint used
  final baseUrl = '${parsedUri.scheme}://${parsedUri.host}';

  path = parsedUri.path;

  /// Format the `method` correctly
  method = method!.toUpperCase();
  headers ??= {};

  if (encoding != null) {
    headers['Content-Encoding'] = encoding;
  }

  /// Set the `Content-Type header`
  if (headers['Content-Type'] == null) {
    headers['Content-Type'] = defaultContentType;
  }

  /// Set the `Accept` header
  if (headers['Accept'] == null) {
    headers['Accept'] = defaultAcceptType;
  }

  /// Set the `body`, if any
  if (body == null || method == 'GET') {
    body = '';
  }

  if (signPayload) {
    headers[_x_amz_content_sha256] = Sigv4.hashPayload(body);
  }

  if (body == '') {
    headers.remove('Content-Type');
  }

  if (chunked) {
    headers['Content-Encoding'] = 'aws-chunked';
  }

  /// Sets or generate the `dateTime` parameter needed for the signature
  dateTime ??= Sigv4.generateDatetime();
  headers[_x_amz_date] = dateTime;

  /// Sets the `host` header
  final baseUri = Uri.parse(baseUrl);
  headers[_host] = baseUri.host;

  if (headers.containsKey('Content-Encoding') &&
      headers['Content-Encoding'] == 'aws-chunked') {
    headers[_x_amz_content_sha256] = _chunked_payload;
  }

  /// Generates the `Authorization` headers
  headers[_authorization] = _generateAuthorization(
    method: method,
    path: path,
    query: query,
    headers: headers,
    body: body,
    dateTime: dateTime,
  );

  /// Adds the `x-amz-security-token` header if a session token is present
  if (sessionToken != null) {
    headers[_x_amz_security_token] = sessionToken;
  }
  headers.remove(_host);

  // Return only string values
  return headers.cast<String, String>();
}