Skip to content

Commit 637029e

Browse files
wurikijidnys1
authored andcommitted
fix(aws_signature_v4): Presign Min/Max Expirations (#1683)
* bug(signature_v4): Allow Min/Max Expirations - change `>` to `>=` and `<` to `<=` - modify examples to print 7 days and 1 second link * chore(signature_v4): revert example.dart
1 parent ccb8a51 commit 637029e

File tree

2 files changed

+47
-30
lines changed

2 files changed

+47
-30
lines changed

packages/aws_signature_v4/example/bin/example.dart

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// ignore_for_file: omit_local_variable_types
16+
1517
import 'dart:convert';
1618
import 'dart:io';
1719
import 'dart:math';
@@ -36,34 +38,47 @@ Future<void> main(List<String> args) async {
3638
const bucketArg = 'bucket';
3739
const regionArg = 'region';
3840

39-
argParser.addOption(accessKeyIdArg,
40-
abbr: 'a', valueHelp: $awsAccessKeyId, mandatory: false);
41-
argParser.addOption(secretAccessKeyArg,
42-
abbr: 's', valueHelp: $awsSecretAccessKey, mandatory: false);
43-
argParser.addOption(sessionTokenArg,
44-
abbr: 't', valueHelp: $awsSessionToken, mandatory: false);
45-
argParser.addOption(
46-
bucketArg,
47-
abbr: 'b',
48-
help: 'The name of the bucket to create',
49-
valueHelp: 'BUCKET',
50-
mandatory: false,
51-
);
52-
argParser.addOption(
53-
regionArg,
54-
abbr: 'r',
55-
help: 'The region of the bucket',
56-
valueHelp: 'REGION',
57-
mandatory: true,
58-
);
41+
argParser
42+
..addOption(
43+
accessKeyIdArg,
44+
abbr: 'a',
45+
valueHelp: $awsAccessKeyId,
46+
mandatory: false,
47+
)
48+
..addOption(
49+
secretAccessKeyArg,
50+
abbr: 's',
51+
valueHelp: $awsSecretAccessKey,
52+
mandatory: false,
53+
)
54+
..addOption(
55+
sessionTokenArg,
56+
abbr: 't',
57+
valueHelp: $awsSessionToken,
58+
mandatory: false,
59+
)
60+
..addOption(
61+
bucketArg,
62+
abbr: 'b',
63+
help: 'The name of the bucket to create',
64+
valueHelp: 'BUCKET',
65+
mandatory: false,
66+
)
67+
..addOption(
68+
regionArg,
69+
abbr: 'r',
70+
help: 'The region of the bucket',
71+
valueHelp: 'REGION',
72+
mandatory: true,
73+
);
5974

6075
final parsedArgs = argParser.parse(args);
61-
final String? accessKeyId =
62-
Platform.environment[$awsAccessKeyId] ?? parsedArgs[accessKeyIdArg];
76+
final String? accessKeyId = Platform.environment[$awsAccessKeyId] ??
77+
parsedArgs[accessKeyIdArg] as String?;
6378
final String? secretAccessKey = Platform.environment[$awsSecretAccessKey] ??
64-
parsedArgs[secretAccessKeyArg];
65-
final String? sessionToken =
66-
Platform.environment[$awsSessionToken] ?? parsedArgs[sessionTokenArg];
79+
parsedArgs[secretAccessKeyArg] as String?;
80+
final String? sessionToken = Platform.environment[$awsSessionToken] ??
81+
parsedArgs[sessionTokenArg] as String?;
6782

6883
if (accessKeyId == null || secretAccessKey == null) {
6984
exitWithError('No AWS credentials found');
@@ -95,11 +110,13 @@ Future<void> main(List<String> args) async {
95110
final ServiceConfiguration serviceConfiguration = S3ServiceConfiguration();
96111

97112
// Create the bucket
98-
final List<int> createBody = utf8.encode('''
113+
final List<int> createBody = utf8.encode(
114+
'''
99115
<CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
100116
<LocationConstraint>$region</LocationConstraint>
101117
</CreateBucketConfiguration>
102-
''');
118+
''',
119+
);
103120
final AWSHttpRequest createRequest = AWSHttpRequest.put(
104121
Uri.https(host, '/'),
105122
body: createBody,

packages/aws_signature_v4/lib/src/request/canonical_request/canonical_request.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import 'package:path/path.dart';
2525
part 'canonical_headers.dart';
2626
part 'canonical_path.dart';
2727
part 'canonical_query_parameters.dart';
28-
part 'signed_headers.dart';
2928
part 'canonical_request_util.dart';
29+
part 'signed_headers.dart';
3030

3131
/// {@template aws_signature_v4.canonical_request}
3232
/// A canonicalized request, used for signing via the SigV4 signing process.
@@ -164,8 +164,8 @@ class CanonicalRequest {
164164

165165
// Per https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
166166
assert(
167-
expiresIn > const Duration(seconds: 1) &&
168-
expiresIn < const Duration(days: 7),
167+
expiresIn >= const Duration(seconds: 1) &&
168+
expiresIn <= const Duration(days: 7),
169169
'Expiration must be greater than 1 second and less than 7 days',
170170
);
171171

0 commit comments

Comments
 (0)