Skip to content

Commit a72255e

Browse files
committed
Add method to authenticate with WP.org credentials
1 parent 94242c6 commit a72255e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

WooCommerce/Classes/Extensions/WKWebView+Authenticated.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
import Alamofire
22
import Foundation
33
import WebKit
4+
import WordPressAuthenticator
45
import struct Yosemite.Credentials
56
import class Networking.UserAgent
67

78
/// An extension to authenticate WPCom automatically
89
///
910
extension WKWebView {
11+
static let wporgNoncePath = "/admin-ajax.php?action=rest-nonce"
12+
13+
func authenticateForWPOrg(with credentials: WordPressOrgCredentials) throws -> URLRequest {
14+
var request = try URLRequest(url: credentials.loginURL.asURL(), method: .post)
15+
request.httpShouldHandleCookies = true
16+
17+
let redirectLink = (credentials.adminURL + Self.wporgNoncePath)
18+
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
19+
20+
let parameters = ["log": credentials.username,
21+
"pwd": credentials.password,
22+
"redirect_to": redirectLink ?? ""]
23+
24+
return try URLEncoding.default.encode(request, with: parameters)
25+
}
26+
1027
func authenticateForWPComAndRedirect(to url: URL, credentials: Credentials?) {
1128
customUserAgent = UserAgent.defaultUserAgent
1229
do {

0 commit comments

Comments
 (0)