Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit db63b55

Browse files
committed
Get unreserved. Get UTF-8.
1 parent dd901fd commit db63b55

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

Sources/Driver/Authentication/Authentication.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ struct Authentication {
4949
self.service = service
5050
self.host = host
5151
self.region = region
52-
self.baseURL = baseURL
52+
//TODO(Brett): Proper encoding and error handling.
53+
self.baseURL = baseURL.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!
5354
self.key = key
5455
self.secret = secret
5556
self.requestParam = requestParam?.addingPercentEncoding(
@@ -75,7 +76,7 @@ struct Authentication {
7576
func getCanonicalRequest() -> String {
7677
var request: String = ""
7778

78-
let uri = "/"
79+
let uri = baseURL
7980
let queryString = self.requestParam ?? ""
8081
let headers = "host:\(host)\nx-amz-date:\(amzDate)\n"
8182
let signedHeaders = "host;x-amz-date"

Tests/AWSTests/SignatureTestSuite.swift

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import Foundation
1212

1313
class SignatureTestSuite: XCTestCase {
1414
static var allTests = [
15+
("testGetUnreserved", testGetUnreserved),
16+
("testGetUTF8", testGetUTF8),
1517
("testPostVanilla", testPostVanilla),
1618
("testPostVanillaQuery", testPostVanillaQuery),
1719
("testPostVanillaQueryNonunreserved", testPostVanillaQueryNonunreserved)
@@ -24,6 +26,45 @@ class SignatureTestSuite: XCTestCase {
2426
return _dateFormatter
2527
}()
2628

29+
func testGetUnreserved() {
30+
let expectedCanonicalRequest = "GET\n/-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n\nhost:example.amazonaws.com\nx-amz-date:20150830T123600Z\n\nhost;x-amz-date\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
31+
32+
let expectedCredentialScope = "20150830/us-east-1/service/aws4_request"
33+
34+
let expectedCanonicalHeaders: [HeaderKey : String] = [
35+
"X-Amz-Date": "20150830T123600Z",
36+
"Authorization": "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=07ef7494c76fa4850883e2b006601f940f8a34d404d0cfa977f52a65bbf5f24f"
37+
]
38+
39+
let result = sign(
40+
method: .get,
41+
path: "/-._~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
42+
)
43+
result.expect(
44+
canonicalRequest: expectedCanonicalRequest,
45+
credentialScope: expectedCredentialScope,
46+
canonicalHeaders: expectedCanonicalHeaders
47+
)
48+
}
49+
50+
func testGetUTF8() {
51+
let expectedCanonicalRequest = "GET\n/%E1%88%B4\n\nhost:example.amazonaws.com\nx-amz-date:20150830T123600Z\n\nhost;x-amz-date\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
52+
53+
let expectedCredentialScope = "20150830/us-east-1/service/aws4_request"
54+
55+
let expectedCanonicalHeaders: [HeaderKey : String] = [
56+
"X-Amz-Date": "20150830T123600Z",
57+
"Authorization": "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=8318018e0b0f223aa2bbf98705b62bb787dc9c0e678f255a891fd03141be5d85"
58+
]
59+
60+
let result = sign(method: .get, path: "/ሴ")
61+
result.expect(
62+
canonicalRequest: expectedCanonicalRequest,
63+
credentialScope: expectedCredentialScope,
64+
canonicalHeaders: expectedCanonicalHeaders
65+
)
66+
}
67+
2768
func testPostVanilla() {
2869
let expectedCanonicalRequest = "POST\n/\n\nhost:example.amazonaws.com\nx-amz-date:20150830T123600Z\n\nhost;x-amz-date\ne3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
2970

@@ -34,7 +75,7 @@ class SignatureTestSuite: XCTestCase {
3475
"Authorization": "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=5da7c1a2acd57cee7505fc6676e4e544621c30862966e37dddb68e92efbe5d6b"
3576
]
3677

37-
let result = post(path: "/")
78+
let result = sign(method: .post, path: "/")
3879
result.expect(
3980
canonicalRequest: expectedCanonicalRequest,
4081
credentialScope: expectedCredentialScope,
@@ -52,7 +93,7 @@ class SignatureTestSuite: XCTestCase {
5293
"Authorization": "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=28038455d6de14eafc1f9222cf5aa6f1a96197d7deb8263271d420d138af7f11"
5394
]
5495

55-
let result = post(path: "/", requestParam: "Param1=value1")
96+
let result = sign(method: .post, path: "/", requestParam: "Param1=value1")
5697
result.expect(
5798
canonicalRequest: expectedCanonicalRequest,
5899
credentialScope: expectedCredentialScope,
@@ -70,7 +111,7 @@ class SignatureTestSuite: XCTestCase {
70111
"Authorization": "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=88d3e39e4fa54b971f51c0a09140368e1a51aafb76c4652d9998f93cf3038074"
71112
]
72113

73-
let result = post(path: "/", requestParam: "@#$%^&+=/,?><`\";:\\|][{}")
114+
let result = sign(method: .post, path: "/", requestParam: "@#$%^&+=/,?><`\";:\\|][{}")
74115
result.expect(
75116
canonicalRequest: expectedCanonicalRequest,
76117
credentialScope: expectedCredentialScope,
@@ -85,9 +126,13 @@ extension SignatureTestSuite {
85126
return SignatureTestSuite.dateFormatter.date(from: "20150830T123600Z")!
86127
}
87128

88-
func post(path: String, requestParam: String? = nil) -> SignerResult {
129+
func sign(
130+
method: Driver.Authentication.Method,
131+
path: String,
132+
requestParam: String? = nil
133+
) -> SignerResult {
89134
var auth = Driver.Authentication(
90-
method: .post,
135+
method: method,
91136
service: "service",
92137
host: "example.amazonaws.com",
93138
region: "us-east-1",

0 commit comments

Comments
 (0)