Skip to content

Commit 2b3ed3f

Browse files
authored
Cookies+nonce authentication (#994)
* Implement getting REST nonce using account credentials * Add Swift wrapper for cookies+nonce authentication * Add comments about WpAuthentication::Nonce * Assert the URLSession instance stores cookies * Try username & password authentication on any nonce errors * Merge the Swift docker env into the existing wordpress image * Implement using username & password to make API calls * Less console output during building docker image * Update the installing swift script * Move Swift Linux test job to the end-to-end test group * Fix swiftlint issues * Default to test against WordPress v6.8.1 swiftly does not support debian 13 yet, which is the platform of newer WordPress docker images.
1 parent a0e17a9 commit 2b3ed3f

File tree

29 files changed

+559
-90
lines changed

29 files changed

+559
-90
lines changed

.buildkite/pipeline.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ steps:
100100
plugins: [$CI_TOOLKIT]
101101
agents:
102102
queue: mac
103-
- label: ":swift: :linux: Build and Test"
104-
command: ".buildkite/commands/run-swift-tests-linux.sh"
105103
- label: ":swift: Lint"
106104
command: |
107105
.buildkite/download-xcframework.sh
@@ -254,6 +252,12 @@ steps:
254252
artifact_paths:
255253
- "native/kotlin/api/kotlin/build/test-results/integrationTest/*.xml"
256254

255+
- label: ":wordpress: :swift: WordPress {{matrix}}"
256+
command: ".buildkite/commands/run-swift-tests-linux.sh"
257+
env:
258+
WORDPRESS_VERSION: "{{matrix}}"
259+
matrix: *wordpress_version_matrix
260+
257261
- label: ":rocket: Publish Swift release $NEW_VERSION"
258262
command: .buildkite/release.sh $NEW_VERSION
259263
depends_on: swift

Cargo.lock

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ semver = "1.0"
6363
serde = "1.0"
6464
serde_json = "1.0"
6565
serde_repr = "0.1"
66+
serde_urlencoded = "0.7"
6667
serial_test = "3.2"
6768
strum = "0.27"
6869
strum_macros = "0.27"

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ test-swift:
160160
$(MAKE) test-swift-$(uname)
161161

162162
test-swift-linux:
163-
docker compose run --rm swift make test-swift-linux-in-docker
163+
docker exec -w /app -i wordpress make test-swift-linux-in-docker
164164

165165
test-swift-linux-in-docker: swift-linux-library
166-
swift test -Xlinker -Ltarget/release/libwordpressFFI-linux -Xlinker -lwp_mobile
166+
swift test -Xlinker -Ltarget/release/libwordpressFFI-linux -Xlinker -lwp_mobile --no-parallel
167167

168168
test-swift-darwin: xcframework
169169
swift test

Package.resolved

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docker-compose.yml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ services:
44
context: .
55
dockerfile: wordpress.Dockerfile
66
args:
7-
WORDPRESS_VERSION: ${WORDPRESS_VERSION:-latest}
7+
WORDPRESS_VERSION: ${WORDPRESS_VERSION:-6.8.1}
88
container_name: 'wordpress'
99
ports:
1010
- '80:80'
@@ -30,17 +30,6 @@ services:
3030
timeout: 1s
3131
retries: 30
3232

33-
swift:
34-
build:
35-
context: .
36-
dockerfile: swift.Dockerfile
37-
volumes:
38-
- ./:/app
39-
working_dir: /app
40-
environment:
41-
- TEST_ALL_PLUGINS
42-
- CARGO_HOME=/app/.cargo
43-
4433
database:
4534
image: 'public.ecr.aws/docker/library/mariadb:11.2'
4635
ports:

integration_test_credentials/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub struct TestCredentials {
77
pub admin_username: &'static str,
88
pub admin_password: &'static str,
99
pub admin_password_uuid: &'static str,
10+
pub admin_account_password: &'static str,
1011
pub subscriber_username: &'static str,
1112
pub subscriber_password: &'static str,
1213
pub subscriber_password_uuid: &'static str,

native/swift/Sources/wordpress-api/WordPressLoginClient.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ public final class WordPressLoginClient: @unchecked Sendable {
99

1010
private let requestExecutor: SafeRequestExecutor
1111
private let client: UniffiWpLoginClient
12+
private let middleware: MiddlewarePipeline
1213

1314
public convenience init(
1415
urlSession: URLSession,
1516
middleware: MiddlewarePipeline = .default
1617
) {
18+
precondition(urlSession.configuration.httpCookieStorage != nil)
19+
precondition(urlSession.configuration.httpShouldSetCookies == true)
20+
precondition(urlSession.configuration.httpCookieAcceptPolicy != .never)
21+
1722
self.init(requestExecutor: WpRequestExecutor(urlSession: urlSession), middleware: middleware)
1823
}
1924

@@ -22,6 +27,7 @@ public final class WordPressLoginClient: @unchecked Sendable {
2227
middleware: MiddlewarePipeline = .default
2328
) {
2429
self.requestExecutor = requestExecutor
30+
self.middleware = middleware
2531
self.client = UniffiWpLoginClient(requestExecutor: requestExecutor, middlewarePipeline: middleware)
2632
}
2733

@@ -60,6 +66,22 @@ public final class WordPressLoginClient: @unchecked Sendable {
6066
public func credentials(from callbackUrl: URL) throws -> WpApiApplicationPasswordDetails {
6167
try extractLoginDetailsFromUrl(url: callbackUrl.absoluteString)
6268
}
69+
70+
public func authenticateTemporarily(
71+
username: String,
72+
password: String,
73+
details: AutoDiscoveryAttemptSuccess
74+
) async throws -> WordPressAPI {
75+
let nonceRetrieval = WpRestNonceRetrieval(details: details, requestExecutor: requestExecutor)
76+
let nonce = try await nonceRetrieval.getNonce(username: username, password: password)
77+
return WordPressAPI(
78+
apiUrlResolver: WpOrgSiteApiUrlResolver(apiRootUrl: details.apiRootUrl),
79+
authenticationProvider: .staticWithAuth(auth: .nonce(nonce: nonce)),
80+
executor: requestExecutor,
81+
middlewarePipeline: middleware,
82+
appNotifier: nil
83+
)
84+
}
6385
}
6486

6587
extension AutoDiscoveryAttemptSuccess {

native/swift/Tests/integration-tests/BlockSettingsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22
import WordPressAPI
33
import Testing
44

5-
@Suite
5+
@Suite(.serialized)
66
struct BlockSettingsTests {
77
let api = WordPressAPI.admin()
88

native/swift/Tests/integration-tests/CancellationTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Testing
66

77
#if os(macOS)
88

9+
@Suite(.serialized)
910
struct CancellationTests {
1011
let api = WordPressAPI.admin()
1112

0 commit comments

Comments
 (0)