signedHeaders method
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,
override
Generates SIGV4-signed headers.
path
: The complete path of your requestmethod
: The HTTP verb your request is usingquery
: Query parameters, if any. required to be included if usedheaders
: Any additional headers. DO NOT add headers to your request after generating signed headersbody
: An encodable objectdateTime
: An AWS-compatible time string. You'll probably want to leave it blank.encoding
: The payload encoding. if anysignPayload
: 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>();
}