Reader accounts now authenticate

This commit is contained in:
Stuart Breckenridge 2020-12-05 21:09:34 +08:00
parent ca730c9f3d
commit db68b6ac22
No known key found for this signature in database
GPG Key ID: 1F11FD62007DC331
10 changed files with 305 additions and 36 deletions

View File

@ -47,6 +47,8 @@ struct AccountsPreferencesView: View {
AddFeedWranglerAccountView()
case .newsBlur:
AddNewsBlurAccountView()
case .feedly:
AddFeedlyAccountView()
default:
AddReaderAPIAccountView(accountType: type)
}

View File

@ -122,10 +122,9 @@ struct AddAccountView: View {
} else {
Button(action: {
presentationMode.wrappedValue.dismiss()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
accountToAdd = AccountConfigurationSheets.addSelectedAccount(selectedAccount)
})
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Continue")
.frame(width: 80)

View File

@ -55,7 +55,7 @@ struct AddCloudKitAccountView: View {
}
}
.padding()
.frame(width: 384, height: 150)
.frame(minWidth: 400, maxWidth: 400, maxHeight: 150)
}
}

View File

@ -80,7 +80,7 @@ struct AddFeedWranglerAccountView: View {
Button(action: {
authenticateFeedWrangler()
}, label: {
Text("Create")
Text("Sign In")
.frame(width: 60)
})
.keyboardShortcut(.defaultAction)
@ -90,8 +90,11 @@ struct AddFeedWranglerAccountView: View {
}
}
.padding()
.frame(width: 400, height: 220)
.frame(minWidth: 400, maxWidth: 400, minHeight: 230, maxHeight: 260)
.textFieldStyle(RoundedBorderTextFieldStyle())
.alert(isPresented: $model.showError, content: {
Alert(title: Text("Sign In Error"), message: Text(model.accountUpdateError.description), dismissButton: .cancel())
})
}

View File

@ -80,7 +80,7 @@ struct AddFeedbinAccountView: View {
Button(action: {
authenticateFeedbin()
}, label: {
Text("Create")
Text("Sign In")
.frame(width: 60)
})
.keyboardShortcut(.defaultAction)
@ -91,20 +91,10 @@ struct AddFeedbinAccountView: View {
}
}
.padding()
.frame(width: 384, height: 230)
.frame(minWidth: 400, maxWidth: 400, minHeight: 230, maxHeight: 260)
.textFieldStyle(RoundedBorderTextFieldStyle())
.alert(isPresented: $model.showError, content: {
Alert(title: Text("Error"), message: Text(model.accountUpdateError.description), dismissButton: .cancel())
})
.touchBar(content: {
if !model.username.isEmpty && !model.password.isEmpty {
Button(action: {
authenticateFeedbin()
}, label: {
Text("Create")
.frame(width: 60)
})
}
Alert(title: Text("Sign In Error"), message: Text(model.accountUpdateError.description), dismissButton: .cancel())
})
}

View File

@ -0,0 +1,129 @@
//
// AddFeedlyAccountView.swift
// Multiplatform macOS
//
// Created by Stuart Breckenridge on 05/12/2020.
// Copyright © 2020 Ranchero Software. All rights reserved.
//
import SwiftUI
import Account
import RSCore
import RSWeb
import Secrets
fileprivate class AddFeedlyViewModel: ObservableObject, OAuthAccountAuthorizationOperationDelegate {
@Published var isAuthenticating: Bool = false
@Published var accountUpdateError: AccountUpdateErrors = .none
@Published var showError: Bool = false
@Published var username: String = ""
@Published var password: String = ""
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didCreate account: Account) {
isAuthenticating = false
// macOS only: `ASWebAuthenticationSession` leaves the browser in the foreground.
// Ensure the app is in the foreground so the user can see their Feedly account load.
#if os(macOS)
NSApplication.shared.activate(ignoringOtherApps: true)
#endif
account.refreshAll { [weak self] result in
switch result {
case .success:
break
case .failure(let error):
self?.accountUpdateError = .other(error: error)
self?.showError = true
}
}
}
func oauthAccountAuthorizationOperation(_ operation: OAuthAccountAuthorizationOperation, didFailWith error: Error) {
isAuthenticating = false
// macOS only: `ASWebAuthenticationSession` leaves the browser in the foreground.
// Ensure the app is in the foreground so the user can see the error.
#if os(macOS)
NSApplication.shared.activate(ignoringOtherApps: true)
#endif
accountUpdateError = .other(error: error)
showError = true
}
}
struct AddFeedlyAccountView: View {
@Environment (\.presentationMode) var presentationMode
@StateObject private var model = AddFeedlyViewModel()
var body: some View {
VStack {
HStack(spacing: 16) {
VStack(alignment: .leading) {
AccountType.feedly.image()
.resizable()
.frame(width: 50, height: 50)
Spacer()
}
VStack(alignment: .leading, spacing: 8) {
Text("Sign in to your Feedly account.")
.font(.headline)
HStack {
Text("Don't have a Feedly account?")
.font(.callout)
Button(action: {
NSWorkspace.shared.open(URL(string: "https://feedly.com")!)
}, label: {
Text("Sign up here.").font(.callout)
}).buttonStyle(LinkButtonStyle())
}
Spacer()
HStack(spacing: 8) {
Spacer()
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Cancel")
.frame(width: 60)
}).keyboardShortcut(.cancelAction)
Button(action: {
authenticateFeedly()
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Sign In")
.frame(width: 60)
})
.keyboardShortcut(.defaultAction)
.disabled(AccountManager.shared.activeAccounts.filter({ $0.type == .cloudKit }).count > 0)
}
}
}
}
.padding()
.frame(minWidth: 400, maxWidth: 400, maxHeight: 150)
.alert(isPresented: $model.showError, content: {
Alert(title: Text("Sign In Error"), message: Text(model.accountUpdateError.description), dismissButton: .cancel())
})
}
private func authenticateFeedly() {
model.isAuthenticating = true
let addAccount = OAuthAccountAuthorizationOperation(accountType: .feedly)
addAccount.delegate = model
#if os(macOS)
addAccount.presentationAnchor = NSApplication.shared.windows.last
#endif
MainThreadOperationQueue.shared.add(addAccount)
}
}
struct AddFeedlyAccountView_Previews: PreviewProvider {
static var previews: some View {
AddFeedlyAccountView()
}
}

View File

@ -57,7 +57,7 @@ struct AddLocalAccountView: View {
}
}
.padding()
.frame(width: 384, height: 230)
.frame(minWidth: 400, maxWidth: 400, minHeight: 230, maxHeight: 260)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}

View File

@ -80,7 +80,7 @@ struct AddNewsBlurAccountView: View {
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
Text("Create")
Text("Sign In")
.frame(width: 60)
})
.keyboardShortcut(.defaultAction)
@ -90,8 +90,11 @@ struct AddNewsBlurAccountView: View {
}
}
.padding()
.frame(width: 400, height: 230)
.frame(minWidth: 400, maxWidth: 400, minHeight: 230, maxHeight: 260)
.textFieldStyle(RoundedBorderTextFieldStyle())
.alert(isPresented: $model.showError, content: {
Alert(title: Text("Sign In Error"), message: Text(model.accountUpdateError.description), dismissButton: .cancel())
})
}
private func authenticateNewsBlur() {

View File

@ -8,12 +8,23 @@
import SwiftUI
import Account
import RSCore
import RSWeb
import Secrets
fileprivate class AddReaderAPIViewModel: ObservableObject {
@Published var isAuthenticating: Bool = false
@Published var accountUpdateError: AccountUpdateErrors = .none
@Published var showError: Bool = false
@Published var username: String = ""
@Published var password: String = ""
@Published var apiUrl: String = ""
}
struct AddReaderAPIAccountView: View {
@Environment (\.presentationMode) var presentationMode
@State private var username: String = ""
@State private var password: String = ""
@StateObject private var model = AddReaderAPIViewModel()
public var accountType: AccountType
var body: some View {
@ -49,10 +60,16 @@ struct AddReaderAPIAccountView: View {
VStack(alignment: .trailing, spacing: 14) {
Text("Email")
Text("Password")
if accountType == .freshRSS {
Text("API URL")
}
}
VStack(spacing: 8) {
TextField("me@email.com", text: $username)
SecureField("•••••••••••", text: $password)
TextField("me@email.com", text: $model.username)
SecureField("•••••••••••", text: $model.password)
if accountType == .freshRSS {
TextField("https://myfreshrss.rocks", text: $model.apiUrl)
}
}
}
@ -65,7 +82,9 @@ struct AddReaderAPIAccountView: View {
Spacer()
HStack(spacing: 8) {
Spacer()
ProgressView().scaleEffect(CGSize(width: 0.5, height: 0.5))
ProgressView()
.scaleEffect(CGSize(width: 0.5, height: 0.5))
.hidden(!model.isAuthenticating)
Button(action: {
presentationMode.wrappedValue.dismiss()
}, label: {
@ -74,38 +93,158 @@ struct AddReaderAPIAccountView: View {
}).keyboardShortcut(.cancelAction)
Button(action: {
presentationMode.wrappedValue.dismiss()
authenticateReaderAccount()
}, label: {
Text("Create")
Text("Sign In")
.frame(width: 60)
})
.keyboardShortcut(.defaultAction)
.disabled(username.isEmpty && password.isEmpty)
.disabled(createDisabled())
}
}
}
}
.padding()
.frame(width: 400, height: 230)
.frame(width: 400, height: height())
.textFieldStyle(RoundedBorderTextFieldStyle())
.alert(isPresented: $model.showError, content: {
Alert(title: Text("Sign In Error"), message: Text(model.accountUpdateError.description), dismissButton: .cancel())
})
}
func createDisabled() -> Bool {
if accountType == .freshRSS {
return model.username.isEmpty || model.password.isEmpty || !model.apiUrl.mayBeURL
}
return model.username.isEmpty || model.password.isEmpty
}
func height() -> CGFloat {
if accountType == .freshRSS {
return 260
}
return 230
}
private func signUp() {
switch accountType {
case .freshRSS:
#if os(macOS)
NSWorkspace.shared.open(URL(string: "https://freshrss.org")!)
#endif
case .inoreader:
#if os(macOS)
NSWorkspace.shared.open(URL(string: "https://www.inoreader.com")!)
#endif
case .bazQux:
#if os(macOS)
NSWorkspace.shared.open(URL(string: "https://bazqux.com")!)
#endif
case .theOldReader:
#if os(macOS)
NSWorkspace.shared.open(URL(string: "https://theoldreader.com")!)
#endif
default:
return
}
}
private func authenticateReaderAccount() {
model.isAuthenticating = true
let credentials = Credentials(type: .readerBasic, username: model.username, secret: model.password)
if accountType == .freshRSS {
Account.validateCredentials(type: accountType, credentials: credentials, endpoint: URL(string: model.apiUrl)!) { result in
self.model.isAuthenticating = false
switch result {
case .success(let validatedCredentials):
guard let validatedCredentials = validatedCredentials else {
self.model.accountUpdateError = .invalidUsernamePassword
self.model.showError = true
return
}
let account = AccountManager.shared.createAccount(type: .freshRSS)
do {
try account.removeCredentials(type: .readerBasic)
try account.removeCredentials(type: .readerAPIKey)
try account.storeCredentials(credentials)
try account.storeCredentials(validatedCredentials)
account.refreshAll(completion: { result in
switch result {
case .success:
self.presentationMode.wrappedValue.dismiss()
case .failure(let error):
self.model.accountUpdateError = .other(error: error)
self.model.showError = true
}
})
} catch {
self.model.accountUpdateError = .keyChainError
self.model.showError = true
}
case .failure:
self.model.accountUpdateError = .networkError
self.model.showError = true
}
}
}
else {
Account.validateCredentials(type: accountType, credentials: credentials) { result in
self.model.isAuthenticating = false
switch result {
case .success(let validatedCredentials):
guard let validatedCredentials = validatedCredentials else {
self.model.accountUpdateError = .invalidUsernamePassword
self.model.showError = true
return
}
let account = AccountManager.shared.createAccount(type: .freshRSS)
do {
try account.removeCredentials(type: .readerBasic)
try account.removeCredentials(type: .readerAPIKey)
try account.storeCredentials(credentials)
try account.storeCredentials(validatedCredentials)
account.refreshAll(completion: { result in
switch result {
case .success:
self.presentationMode.wrappedValue.dismiss()
case .failure(let error):
self.model.accountUpdateError = .other(error: error)
self.model.showError = true
}
})
} catch {
self.model.accountUpdateError = .keyChainError
self.model.showError = true
}
case .failure:
self.model.accountUpdateError = .networkError
self.model.showError = true
}
}
}
}
}
struct AddReaderAPIAccountView_Previews: PreviewProvider {

View File

@ -28,6 +28,7 @@
172199C924AB228900A31D04 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 172199C824AB228900A31D04 /* SettingsView.swift */; };
172199ED24AB2E0100A31D04 /* SafariView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 172199EC24AB2E0100A31D04 /* SafariView.swift */; };
172199F124AB716900A31D04 /* SidebarToolbarModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 172199F024AB716900A31D04 /* SidebarToolbarModifier.swift */; };
17241249257B8A8A00ACCEBC /* AddFeedlyAccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17241248257B8A8A00ACCEBC /* AddFeedlyAccountView.swift */; };
1727B39924C1368D00A4DBDC /* LayoutPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1727B39824C1368D00A4DBDC /* LayoutPreferencesView.swift */; };
1729529324AA1CAA00D65E66 /* AccountsPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529024AA1CAA00D65E66 /* AccountsPreferencesView.swift */; };
1729529424AA1CAA00D65E66 /* AdvancedPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1729529124AA1CAA00D65E66 /* AdvancedPreferencesView.swift */; };
@ -1353,6 +1354,7 @@
172199C824AB228900A31D04 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
172199EC24AB2E0100A31D04 /* SafariView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafariView.swift; sourceTree = "<group>"; };
172199F024AB716900A31D04 /* SidebarToolbarModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SidebarToolbarModifier.swift; sourceTree = "<group>"; };
17241248257B8A8A00ACCEBC /* AddFeedlyAccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddFeedlyAccountView.swift; sourceTree = "<group>"; };
1727B39824C1368D00A4DBDC /* LayoutPreferencesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutPreferencesView.swift; sourceTree = "<group>"; };
1729529024AA1CAA00D65E66 /* AccountsPreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccountsPreferencesView.swift; sourceTree = "<group>"; };
1729529124AA1CAA00D65E66 /* AdvancedPreferencesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AdvancedPreferencesView.swift; sourceTree = "<group>"; };
@ -2144,6 +2146,7 @@
17386B792577C4BF0014C8B2 /* AddLocalAccountView.swift */,
DF98E2992578A73A00F18944 /* AddCloudKitAccountView.swift */,
17386BC32577CC600014C8B2 /* AddFeedbinAccountView.swift */,
17241248257B8A8A00ACCEBC /* AddFeedlyAccountView.swift */,
DF98E2AF2578AA5C00F18944 /* AddFeedWranglerAccountView.swift */,
DF98E2BD2578AC0000F18944 /* AddNewsBlurAccountView.swift */,
DF98E2C52578AD1B00F18944 /* AddReaderAPIAccountView.swift */,
@ -4743,6 +4746,7 @@
1729529424AA1CAA00D65E66 /* AdvancedPreferencesView.swift in Sources */,
5177470424B2657F00EB0F74 /* TimelineToolbarModifier.swift in Sources */,
51A8001324CA0FC700F41F1D /* Sink.swift in Sources */,
17241249257B8A8A00ACCEBC /* AddFeedlyAccountView.swift in Sources */,
51E4992D24A8676300B667CB /* FetchRequestOperation.swift in Sources */,
51E4992424A8098400B667CB /* SmartFeedPasteboardWriter.swift in Sources */,
51E4991424A808FF00B667CB /* ArticleStringFormatter.swift in Sources */,