-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathBlog+SelfHosted.swift
More file actions
387 lines (337 loc) · 13.9 KB
/
Copy pathBlog+SelfHosted.swift
File metadata and controls
387 lines (337 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import Foundation
import CoreData
import CryptoKit
import WordPressAPI
import WordPressShared
public extension Blog {
enum BlogCredentialsError: Error {
case blogUrlMissing
case blogUrlInvalid
case blogUsernameMissing
case blogPasswordMissing
case blogIdentifierMissing
case invalidCredentialsUrl
case invalidXmlRpcEndpoint
case incorrectCredentials
}
static func createRestApiBlog(
with details: WpApiApplicationPasswordDetails,
restApiRootURL: URL,
xmlrpcEndpointURL: URL,
blogID: TaggedManagedObjectID<Blog>?,
in contextManager: ContextManager,
using keychainImplementation: KeychainAccessible = AppKeychain()
) async throws -> TaggedManagedObjectID<Blog> {
try await contextManager.performAndSave { context in
let blog =
if let blogID {
try context.existingObject(with: blogID)
} else {
Blog.lookup(username: details.userLogin, xmlrpc: xmlrpcEndpointURL.absoluteString, in: context)
?? Blog.createBlankBlog(in: context)
}
blog.url = details.siteUrl
blog.username = details.userLogin
blog.restApiRootURL = restApiRootURL.absoluteString
blog.setXMLRPCEndpoint(to: xmlrpcEndpointURL)
blog.setSiteIdentifier(details.derivedSiteId)
// `url` and `xmlrpc` need to be set before setting the application password.
try blog.setApplicationToken(details.password, using: keychainImplementation)
// We don't overwrite the `Blog.password` with the application password (`details.password`), because we want
// the application continues to function when the application password is revoked.
return TaggedManagedObjectID(blog)
}
}
static func lookupRestApiBlog(with id: SiteIdentifier, in context: NSManagedObjectContext) throws -> Blog? {
try BlogQuery().apiKey(is: id).blog(in: context)
}
static func hasRestApiBlog(with id: SiteIdentifier, in context: NSManagedObjectContext) throws -> Bool {
BlogQuery().apiKey(is: id).count(in: context) != 0
}
@objc(getApplicationTokenWithError:)
func objc_getApplicationToken() throws -> String {
try getApplicationToken()
}
// MARK: Type-safe wrappers
// The underlying `Blog` object has lots of field nullability that doesn't provide guarantees about
// which fields are present. These wrappers will `throw` if the `Blog` is invalid, allowing any dependent
// code can be much simpler.
/// Retrieve Application Tokens
///
func getApplicationToken(using keychainImplementation: KeychainAccessible? = nil) throws -> String {
try (keychainImplementation ?? self.keychain)
.getPassword(for: self.getUsername(), serviceName: self.getUrlString())
}
/// Delete Application Token
///
func deleteApplicationToken(using keychainImplementation: KeychainAccessible? = nil) throws {
try? (keychainImplementation ?? self.keychain)
.setPassword(
for: self.getUsername(),
to: nil,
serviceName: self.getUrlString()
)
}
@available(swift, obsoleted: 1.0)
@objc(deleteApplicationToken)
func objc_deleteApplicationToken() {
_ = try? deleteApplicationToken()
}
/// Store Application Tokens
///
func setApplicationToken(
_ newValue: String,
using keychainImplementation: KeychainAccessible? = nil
) throws {
try (keychainImplementation ?? self.keychain)
.setPassword(
for: self.getUsername(),
to: newValue,
serviceName: self.getUrlString()
)
}
/// A null-safe wrapper for `Blog.username`
func getUsername() throws -> String {
guard let username = self.username else {
throw BlogCredentialsError.blogUsernameMissing
}
return username
}
/// A null-safe replacement for `Blog.password(get)`
func getPassword(using keychainImplementation: KeychainAccessible? = nil) throws -> String {
try (keychainImplementation ?? self.keychain)
.getPassword(
for: self.getUsername(),
serviceName: self.getXMLRPCEndpoint().absoluteString
)
}
/// A null-safe replacement for `Blog.password(set)`
func setPassword(to newValue: String, using keychainImplementation: KeychainAccessible? = nil) throws {
try (keychainImplementation ?? self.keychain)
.setPassword(
for: self.getUsername(),
to: newValue,
serviceName: self.getXMLRPCEndpoint().absoluteString
)
}
func wordPressClientParsedUrl() throws -> ParsedUrl {
try ParsedUrl.parse(input: self.getUrl().absoluteString)
}
/// A null-and-type-safe replacement for `Blog.url(get)`
func getUrl() throws -> URL {
guard let stringUrl = self.url else {
throw BlogCredentialsError.blogUrlMissing
}
guard let url = URL(string: stringUrl) else {
throw BlogCredentialsError.blogUrlInvalid
}
return url
}
/// A null-safe helper for `Blog.url(get)`, when what you really want is a String
func getUrlString() throws -> String {
try getUrl().absoluteString
}
/// A type-safe helper for `Blog.url(set)` that takes a URL directly (instead of a string)
func setUrl(_ newValue: URL) {
self.url = newValue.absoluteString
}
/// A null-and-type-safe replacement for `Blog.xmlrpc(get)`
func getXMLRPCEndpoint() throws -> URL {
guard let urlString = self.xmlrpc, let url = URL(string: urlString) else {
throw BlogCredentialsError.invalidXmlRpcEndpoint
}
return url
}
/// A type-safe helper for `Blog.xmlrpc(set)` that takes a URL directly (instead of a string)
func setXMLRPCEndpoint(to newValue: URL) {
self.xmlrpc = newValue.absoluteString
}
/// There's `dotComId` for WordPress.com blogs, but we don't have a good way to lookup REST API sites with a scalar value.
///
/// This hack fixes that – we should never store API Keys in Core Data anyway, so we can (mis)use that field to add a unique identifier
typealias SiteIdentifier = String
func getSiteIdentifier() throws -> SiteIdentifier {
guard let identifier = self.apiKey else {
throw BlogCredentialsError.blogIdentifierMissing
}
return identifier
}
func setSiteIdentifier(_ newValue: SiteIdentifier) {
self.apiKey = newValue
}
@objc var isSelfHosted: Bool {
self.account == nil
}
@objc var hasDirectCoreRESTAPIAccess: Bool {
guard let site = try? WordPressSite(blog: self) else {
return false
}
return site.applicationPasswordCredentials != nil
}
}
public extension WpApiApplicationPasswordDetails {
var derivedSiteId: String {
SHA256.hash(data: Data(siteUrl.localizedLowercase.utf8))
.compactMap { String(format: "%02x", $0) }
.joined()
}
}
/// Describes a WordPress site's hosting type, authentication credentials,
/// and API capabilities.
///
/// This is a value type constructed from a `Blog` Core Data object. It captures
/// a snapshot of the site's characteristics at construction time.
///
/// `WordPressSite` is not a one-to-one mapping with `Blog`. It represents the
/// subset of `Blog` instances that have access to the WordPress core REST API
/// (wp/v2). A self-hosted site that only has XML-RPC credentials is not
/// representable as a `WordPressSite`.
///
/// - All WordPress.com sites qualify (wp/v2 is accessed via WP.com REST API
/// with OAuth).
/// - Self-hosted sites must have application password credentials.
public struct WordPressSite {
public let blogId: TaggedManagedObjectID<Blog>
public let siteURL: URL
public let flavor: Flavor
public init(blogId: TaggedManagedObjectID<Blog>, siteURL: URL, flavor: Flavor) {
self.blogId = blogId
self.siteURL = siteURL
self.flavor = flavor
}
}
extension WordPressSite {
public enum Flavor {
/// A site hosted on WordPress.com. Always has OAuth access via
/// WPAccount. May also have application password credentials
/// (e.g., Atomic sites).
case dotCom(DotComCredentials)
/// A self-hosted WordPress site with application password credentials.
/// Application password is required for wp/v2 API access.
case selfHosted(ApplicationPasswordCredentials)
}
}
extension WordPressSite {
public struct DotComCredentials: Hashable {
public let siteId: Int
public let oAuthToken: String
/// Non-nil for Atomic sites that also have application password access.
public let applicationPassword: ApplicationPasswordCredentials?
public init(siteId: Int, oAuthToken: String, applicationPassword: ApplicationPasswordCredentials?) {
self.siteId = siteId
self.oAuthToken = oAuthToken
self.applicationPassword = applicationPassword
}
}
public struct ApplicationPasswordCredentials: Hashable {
public let apiRootURL: ParsedUrl
public let username: String
public let token: String
public init(apiRootURL: ParsedUrl, username: String, token: String) {
self.apiRootURL = apiRootURL
self.username = username
self.token = token
}
}
}
extension WordPressSite {
/// Constructs a `WordPressSite` from a `Blog` Core Data object.
///
/// Throws if the blog lacks enough data to determine its hosting type
/// and at least one valid authentication method.
///
/// For self-hosted sites, application password credentials are required.
/// Sites without them cannot be represented as a `WordPressSite`.
public init(blog: Blog, keychain: KeychainAccessible = AppKeychain()) throws {
let siteURL = try blog.getUrl()
self.blogId = TaggedManagedObjectID(blog)
self.siteURL = siteURL
// Build application password credentials if available.
// These are shared across both hosting types — WordPress.com Atomic
// sites can have them too.
let applicationPassword: ApplicationPasswordCredentials?
if let restApiRootURL = blog.restApiRootURL,
let parsedApiRoot = try? ParsedUrl.parse(input: restApiRootURL),
let username = blog.username,
let token = try? blog.getApplicationToken(using: keychain)
{
applicationPassword = ApplicationPasswordCredentials(
apiRootURL: parsedApiRoot,
username: username,
token: token
)
} else {
applicationPassword = nil
}
// Check for WordPress.com account first. This means Atomic sites
// (which have both an account and application password credentials)
// resolve to `.dotCom`.
if let account = blog.account,
let siteId = blog.dotComID?.intValue
{
let authToken =
try account.authToken
?? WPAccount.token(forUsername: account.username)
self.flavor = .dotCom(
DotComCredentials(
siteId: siteId,
oAuthToken: authToken,
applicationPassword: applicationPassword
)
)
} else {
// Self-hosted sites must have application password credentials
// for wp/v2 API access.
guard let applicationPassword else {
throw Blog.BlogCredentialsError.blogPasswordMissing
}
self.flavor = .selfHosted(applicationPassword)
}
}
}
extension WordPressSite {
/// How the app reaches the site's wp/v2 REST API.
///
/// Unlike `flavor`, which describes how the site is presented in the app,
/// `Transport` decides which endpoint and credentials to use for API
/// access. The two don't always line up: a WordPress.com Atomic site
/// presents as `.dotCom` but is accessed directly when application
/// password credentials are available.
public enum Transport {
/// Requests go to the site's own REST API root, authenticated with
/// an application password.
case direct(ApplicationPasswordCredentials)
/// Requests are proxied through the WP.com REST API, authenticated
/// with the account's OAuth token.
case dotComProxy(siteId: Int, oAuthToken: String)
}
/// Direct site access is preferred whenever application password
/// credentials are available, because it does not depend on the WP.com
/// proxy and works for site features the proxy does not expose.
public var transport: Transport {
switch flavor {
case let .dotCom(credentials):
if let applicationPassword = credentials.applicationPassword {
return .direct(applicationPassword)
}
return .dotComProxy(siteId: credentials.siteId, oAuthToken: credentials.oAuthToken)
case let .selfHosted(credentials):
return .direct(credentials)
}
}
/// The application password credentials, if available.
/// Always non-nil for self-hosted sites. Optional for WordPress.com sites
/// (non-nil for Atomic sites).
public var applicationPasswordCredentials: ApplicationPasswordCredentials? {
switch flavor {
case let .dotCom(credentials):
return credentials.applicationPassword
case let .selfHosted(credentials):
return credentials
}
}
/// Look up the `Blog` object in a given Core Data context.
public func blog(in context: NSManagedObjectContext) throws -> Blog {
try context.existingObject(with: blogId)
}
}