Skip to content

Commit

Permalink
Fix failure to handle session tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksloan committed Apr 23, 2024
1 parent cade4c7 commit abf6805
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Sources/Soto/Extensions/S3/S3+presignedPost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ extension S3 {
fields["x-amz-date"] = longDate
fields["x-amz-credential"] = presignedPostCredential

if let sessionToken = clientCredentials.sessionToken {
conditions.append(.match("x-amz-security-token", sessionToken))
fields["x-amz-security-token"] = sessionToken
}

// Create the policy and add to fields
let policy = PostPolicy(expiration: date.addingTimeInterval(expiresIn), conditions: conditions)
let stringToSign = try policy.stringToSign()
Expand Down
50 changes: 50 additions & 0 deletions Tests/SotoTests/Services/S3/S3ExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,54 @@ extension S3Tests {

XCTAssertEqual(credential, expectedCredential)
}

func testSessionToken() {
let clent = AWSClient(
credentialProvider: .static(
accessKeyId: "AKIAIOSFODNN7EXAMPLE",
secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
sessionToken: "EXAMPLESESSIONTOKEN"
)
)

let s3 = S3(client: client, region: .useast1)

defer { try? client.syncShutdown() }

let fields = [
"acl": "public-read",
"success_action_redirect": "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html",
"x-amz-meta-uuid": "14365123651274",
"x-amz-server-side-encryption": "AES256",
]

let conditions: [S3.PostPolicyCondition] = [
.match("acl", "public-read"),
.match("success_action_redirect", "http://sigv4examplebucket.s3.amazonaws.com/successful_upload.html"),
.match("x-amz-meta-uuid", "14365123651274"),
.match("x-amz-server-side-encryption", "AES256"),
.rule("starts-with", "$Content-Type", "image/"),
.rule("starts-with", "$x-amz-meta-tag", "")
]

let expiresIn = 36.0 * 60.0 * 60.0
var dateComponents = DateComponents()
dateComponents.year = 2015
dateComponents.month = 12
dateComponents.day = 29
dateComponents.timeZone = TimeZone(secondsFromGMT: 0)!

let date = Calendar(identifier: .gregorian).date(from: dateComponents)!

let presignedPost = try await s3.generatePresignedPost(
key: "user/user1/${filename}",
bucket: "sigv4examplebucket",
fields: fields,
conditions: conditions,
expiresIn: expiresIn,
date: date
)

XCTAssertEqual(presignedPost.fields["x-amz-session-token"], "EXAMPLESESSIONTOKEN")
}
}

0 comments on commit abf6805

Please sign in to comment.