-
Notifications
You must be signed in to change notification settings - Fork 3
Support additional SSL certificate alternative names #1049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ import FoundationNetworking | |
| @Suite("Login Tests", .enabled(if: !isLinux())) | ||
| class LoginTests { | ||
|
|
||
| let client = WordPressLoginClient(urlSession: .shared) | ||
| let client = WordPressLoginClient(urlSession: .init(configuration: .ephemeral)) | ||
|
|
||
| @Test("Login Spec Example 1: Valid URL") | ||
| func testValidURL() async throws { | ||
|
|
@@ -172,7 +172,7 @@ class LoginTests { | |
|
|
||
| await #expect(performing: { | ||
| _ = try await WordPressLoginClient( | ||
| urlSession: .shared, | ||
| urlSession: .init(configuration: .ephemeral), | ||
| middleware: MiddlewarePipeline(middlewares: invalid) | ||
| ).findLoginUrl(forSite: "https://basic-auth.wpmt.co") | ||
| }, throws: { error in | ||
|
|
@@ -196,7 +196,7 @@ class LoginTests { | |
| let valid = ApiDiscoveryAuthenticationMiddleware(username: "[email protected]", password: "str0ngp4ssw0rd!") | ||
|
|
||
| let parsedUrl = try await WordPressLoginClient( | ||
| urlSession: .shared, | ||
| urlSession: .init(configuration: .ephemeral), | ||
| middleware: MiddlewarePipeline(middlewares: valid) | ||
| ).findLoginUrl(forSite: "https://basic-auth.wpmt.co") | ||
|
|
||
|
|
@@ -285,44 +285,19 @@ class LoginTests { | |
| } | ||
|
|
||
| /// This test is unavailable in Linux until https://github.com/swiftlang/swift-corelibs-foundation/pull/4937 lands | ||
| @Test("Login Spec Example 17: Invalid SSL Certificate with explicit exception", .enabled(if: !isLinux())) | ||
| @Test("Login Spec Example 18: Invalid SSL Certificate with explicit exception", .enabled(if: !isLinux())) | ||
| func testInvalidHttpsWithExceptionWorks() async throws { | ||
| let session = URLSession( | ||
| configuration: .default, | ||
| delegate: HTTPSSessionDelegate( | ||
| allowedDomains: ["wordpress-1315525-4803651.cloudwaysapps.com"] | ||
| ), | ||
| delegateQueue: nil | ||
| ) | ||
| let client = WordPressLoginClient(urlSession: session) | ||
| let executor = WpRequestExecutor(urlSession: .init(configuration: .ephemeral)) | ||
| executor.allowSSL(altNames: ["wordpress-1315525-4803651.cloudwaysapps.com"], forCommonName: "vanilla.wpmt.co") | ||
| let client = WordPressLoginClient(requestExecutor: executor) | ||
| _ = try await client.findLoginUrl(forSite: "https://wordpress-1315525-4803651.cloudwaysapps.com") | ||
| } | ||
|
|
||
| final class HTTPSSessionDelegate: NSObject, URLSessionDelegate { | ||
|
|
||
| let allowedDomains: [String] | ||
|
|
||
| init(allowedDomains: [String] = []) { | ||
| self.allowedDomains = allowedDomains | ||
| } | ||
|
|
||
| #if !os(Linux) | ||
| // There's no ability to support self-signed (or otherwise invalid) SSL certificates in Linux until | ||
| // https://github.com/swiftlang/swift-corelibs-foundation/pull/4937 lands. | ||
| func urlSession( | ||
| _ session: URLSession, | ||
| didReceive challenge: URLAuthenticationChallenge, | ||
| completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void | ||
| ) { | ||
| guard allowedDomains.contains(challenge.protectionSpace.host), | ||
| let trust = challenge.protectionSpace.serverTrust else { | ||
| completionHandler(.useCredential, nil) | ||
| return | ||
| } | ||
|
|
||
| completionHandler(.useCredential, URLCredential(trust: trust)) | ||
| } | ||
| #endif | ||
| /// This test is unavailable in Linux until https://github.com/swiftlang/swift-corelibs-foundation/pull/4937 lands | ||
| @Test("Login Spec Example 19: Alternative name in SSL Certificate", .enabled(if: !isLinux())) | ||
| func testAlternameWorks() async throws { | ||
| // "vanilla1.wpmt.co" is one of the alternative names in vanilla.wpmt.co certificate. | ||
| _ = try await self.client.findLoginUrl(forSite: "https://vanilla1.wpmt.co") | ||
| } | ||
|
|
||
| private func getApplicationPasswordsNotSupportedReason( | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you manually trust a certificate, it's trusted for the entire lifetime of the URLSession, which means that using the
.sharedURLSession would cause unit tests to interfere with each other.