Skip to content

Commit

Permalink
small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiki270 committed May 12, 2018
1 parent 6936200 commit 8423af3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions Sources/S3/Extensions/S3+Bucket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public extension S3 {
// }
//
// let headers = [
// "Host": host
// "host": host
// ]
// let awsHeaders = try signer.headers(for: .GET, urlString: url.absoluteString, region: region, headers: headers, payload: .none)
// return try make(request: url, method: .GET, headers: awsHeaders, data: "".convertToData(), on: container).map(to: Bucket.Location.self) { response in
Expand Down Expand Up @@ -53,7 +53,7 @@ public extension S3 {
}

let headers = [
"Host": host
"host": host
]
let awsHeaders = try signer.headers(for: .DELETE, urlString: url.absoluteString, region: region, headers: headers, payload: .none)
return try make(request: url, method: .DELETE, headers: awsHeaders, data: "".convertToData(), on: container).map(to: Void.self) { response in
Expand All @@ -80,7 +80,7 @@ public extension S3 {
let data = content.convertToData()

let headers = [
"Host": host
"host": host
]
let awsHeaders = try signer.headers(for: .PUT, urlString: url.absoluteString, region: region, headers: headers, payload: .bytes(data))
return try make(request: url, method: .PUT, headers: awsHeaders, data: data, on: container).map(to: Void.self) { response in
Expand Down
2 changes: 1 addition & 1 deletion Sources/S3/Extensions/S3+List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension S3 {
throw S3.Error.invalidUrl
}
var headers = headers
headers["Host"] = host
headers["host"] = host
let awsHeaders = try signer.headers(for: .GET, urlString: url.absoluteString, region: region, headers: headers, payload: .none)
return try make(request: url, method: .GET, headers: awsHeaders, data: "".convertToData(), on: container).map(to: BucketResults.self) { response in
try self.check(response)
Expand Down
4 changes: 2 additions & 2 deletions Sources/S3/Extensions/S3+Put.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public extension S3 {
let url = try self.url(file: file, on: container)

var awsHeaders: [String: String] = headers
awsHeaders["Content-Type"] = file.mime.description
awsHeaders["X-Amz-Acl"] = file.access.rawValue
awsHeaders["content-Type"] = file.mime.description
awsHeaders["x-amz-acl"] = file.access.rawValue
let headers = try signer.headers(for: .PUT, urlString: url.absoluteString, headers: awsHeaders, payload: Payload.bytes(file.data))

let request = Request(using: container)
Expand Down
1 change: 1 addition & 0 deletions Sources/S3DemoApp/S3DemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Vapor


public func routes(_ router: Router) throws {

// Get all available buckets
router.get("buckets") { req -> Future<BucketsInfo> in
let s3 = try req.makeS3Client()
Expand Down
17 changes: 8 additions & 9 deletions Sources/S3Signer/S3Signer+Private.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import HTTP
import Crypto



/// Private interface
extension S3Signer {


// MARK: Private interface

func canonicalHeaders(_ headers: [String: String]) -> String {
let headerList = Array(headers.keys)
.map { "\($0.lowercased()):\(headers[$0]!)" }
Expand Down Expand Up @@ -73,7 +72,7 @@ extension S3Signer {
let signHeaders = signed(headers: headers).encode(type: .queryAllowed) else {
throw Error.invalidEncoding
}
let fullURL = "\(url.absoluteString)?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=\(config.accessKey)%2F\(credScope)&X-Amz-Date=\(dates.long)&X-Amz-Expires=\(expiration.value)&X-Amz-SignedHeaders=\(signHeaders)"
let fullURL = "\(url.absoluteString)?x-amz-algorithm=AWS4-HMAC-SHA256&x-amz-credential=\(config.accessKey)%2F\(credScope)&x-amz-date=\(dates.long)&x-amz-expires=\(expiration.value)&x-amz-signedheaders=\(signHeaders)"

// This should never throw.
guard let url = URL(string: fullURL) else {
Expand Down Expand Up @@ -109,16 +108,16 @@ extension S3Signer {

func update(headers: [String: String], url: URL, longDate: String, bodyDigest: String, region: Region?) -> [String: String] {
var updatedHeaders = headers
updatedHeaders["X-Amz-Date"] = longDate
if (updatedHeaders["Host"] ?? updatedHeaders["host"]) == nil {
updatedHeaders["Host"] = url.host ?? (region ?? config.region).host
updatedHeaders["x-amz-date"] = longDate
if (updatedHeaders["host"] ?? updatedHeaders["host"]) == nil {
updatedHeaders["host"] = url.host ?? (region ?? config.region).host
}
if bodyDigest != "UNSIGNED-PAYLOAD" && config.service == "s3" {
updatedHeaders["X-Amz-Content-SHA256"] = bodyDigest
updatedHeaders["x-amz-content-sha256"] = bodyDigest
}
// According to http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html#RequestWithSTS
if let token = config.securityToken {
updatedHeaders["X-Amz-Security-Token"] = token
updatedHeaders["x-amz-security-token"] = token
}
return updatedHeaders
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/S3Signer/S3Signer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ extension S3Signer {
var updatedHeaders = update(headers: headers, url: url, longDate: dates.long, bodyDigest: bodyDigest, region: region)

if httpMethod == .PUT && payload.isBytes {
updatedHeaders["Content-MD5"] = try MD5.hash(payload.bytes).base64EncodedString()
updatedHeaders["content-md5"] = try MD5.hash(payload.bytes).base64EncodedString()
}

if httpMethod == .PUT || httpMethod == .DELETE {
updatedHeaders["Content-Length"] = payload.size()
updatedHeaders["content-length"] = payload.size()
if httpMethod == .PUT && url.pathExtension != "" {
updatedHeaders["Content-Type"] = (MediaType.fileExtension(url.pathExtension) ?? .plainText).description
updatedHeaders["content-type"] = (MediaType.fileExtension(url.pathExtension) ?? .plainText).description
}
}

updatedHeaders["Authorization"] = try generateAuthHeader(httpMethod, url: url, headers: updatedHeaders, bodyDigest: bodyDigest, dates: dates, region: region)
updatedHeaders["authorization"] = try generateAuthHeader(httpMethod, url: url, headers: updatedHeaders, bodyDigest: bodyDigest, dates: dates, region: region)

var headers = HTTPHeaders()
for (key, value) in updatedHeaders {
Expand All @@ -93,13 +93,13 @@ extension S3Signer {

let region = region ?? config.region

updatedHeaders["Host"] = url.host ?? region.host
updatedHeaders["host"] = url.host ?? region.host

let (canonRequest, fullURL) = try presignedURLCanonRequest(httpMethod, dates: dates, expiration: expiration, url: url, region: region, headers: updatedHeaders)

let stringToSign = try createStringToSign(canonRequest, dates: dates, region: region)
let signature = try createSignature(stringToSign, timeStampShort: dates.short, region: region)
let presignedURL = URL(string: fullURL.absoluteString.appending("&X-Amz-Signature=\(signature)"))
let presignedURL = URL(string: fullURL.absoluteString.appending("&x-amz-signature=\(signature)"))
return presignedURL
}

Expand Down

0 comments on commit 8423af3

Please sign in to comment.