mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
models now handle authentication
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// AddFeedWranglerViewModel.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
|
||||
|
||||
class AddFeedWranglerViewModel: 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 canDismiss: Bool = false
|
||||
|
||||
func authenticateFeedWrangler() {
|
||||
|
||||
isAuthenticating = true
|
||||
let credentials = Credentials(type: .feedWranglerBasic, username: username, secret: password)
|
||||
|
||||
Account.validateCredentials(type: .feedWrangler, credentials: credentials) { result in
|
||||
|
||||
|
||||
self.isAuthenticating = false
|
||||
|
||||
switch result {
|
||||
case .success(let validatedCredentials):
|
||||
|
||||
guard let validatedCredentials = validatedCredentials else {
|
||||
self.accountUpdateError = .invalidUsernamePassword
|
||||
self.showError = true
|
||||
return
|
||||
}
|
||||
|
||||
let account = AccountManager.shared.createAccount(type: .feedWrangler)
|
||||
|
||||
do {
|
||||
try account.removeCredentials(type: .feedWranglerBasic)
|
||||
try account.removeCredentials(type: .feedWranglerToken)
|
||||
try account.storeCredentials(credentials)
|
||||
try account.storeCredentials(validatedCredentials)
|
||||
self.canDismiss = true
|
||||
account.refreshAll(completion: { result in
|
||||
switch result {
|
||||
case .success:
|
||||
break
|
||||
case .failure(let error):
|
||||
self.accountUpdateError = .other(error: error)
|
||||
self.showError = true
|
||||
}
|
||||
})
|
||||
} catch {
|
||||
self.accountUpdateError = .keyChainError
|
||||
self.showError = true
|
||||
}
|
||||
case .failure:
|
||||
self.accountUpdateError = .networkError
|
||||
self.showError = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
//
|
||||
// AddFeedbinViewModel.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
|
||||
|
||||
class AddFeedbinViewModel: 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 canDismiss: Bool = false
|
||||
|
||||
func authenticateFeedbin() {
|
||||
isAuthenticating = true
|
||||
let credentials = Credentials(type: .basic, username: username, secret: password)
|
||||
|
||||
Account.validateCredentials(type: .feedbin, credentials: credentials) { result in
|
||||
self.isAuthenticating = false
|
||||
|
||||
switch result {
|
||||
case .success(let validatedCredentials):
|
||||
|
||||
guard let validatedCredentials = validatedCredentials else {
|
||||
self.accountUpdateError = .invalidUsernamePassword
|
||||
self.showError = true
|
||||
return
|
||||
}
|
||||
|
||||
let account = AccountManager.shared.createAccount(type: .feedbin)
|
||||
|
||||
do {
|
||||
try account.removeCredentials(type: .basic)
|
||||
try account.storeCredentials(validatedCredentials)
|
||||
self.isAuthenticating = false
|
||||
self.canDismiss = true
|
||||
account.refreshAll(completion: { result in
|
||||
switch result {
|
||||
case .success:
|
||||
break
|
||||
case .failure(let error):
|
||||
self.accountUpdateError = .other(error: error)
|
||||
self.showError = true
|
||||
}
|
||||
})
|
||||
|
||||
} catch {
|
||||
self.accountUpdateError = .keyChainError
|
||||
self.showError = true
|
||||
}
|
||||
|
||||
case .failure:
|
||||
self.accountUpdateError = .networkError
|
||||
self.showError = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// AddFeedlyViewModel.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
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// AddNewsBlurViewModel.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
|
||||
|
||||
class AddNewsBlurViewModel: 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 canDismiss: Bool = false
|
||||
|
||||
func authenticateNewsBlur() {
|
||||
isAuthenticating = true
|
||||
let credentials = Credentials(type: .newsBlurBasic, username: username, secret: password)
|
||||
|
||||
Account.validateCredentials(type: .newsBlur, credentials: credentials) { result in
|
||||
|
||||
self.isAuthenticating = false
|
||||
|
||||
switch result {
|
||||
case .success(let validatedCredentials):
|
||||
|
||||
guard let validatedCredentials = validatedCredentials else {
|
||||
self.accountUpdateError = .invalidUsernamePassword
|
||||
self.showError = true
|
||||
return
|
||||
}
|
||||
|
||||
let account = AccountManager.shared.createAccount(type: .newsBlur)
|
||||
|
||||
do {
|
||||
try account.removeCredentials(type: .newsBlurBasic)
|
||||
try account.removeCredentials(type: .newsBlurSessionId)
|
||||
try account.storeCredentials(credentials)
|
||||
try account.storeCredentials(validatedCredentials)
|
||||
self.canDismiss = true
|
||||
account.refreshAll(completion: { result in
|
||||
switch result {
|
||||
case .success:
|
||||
break
|
||||
case .failure(let error):
|
||||
self.accountUpdateError = .other(error: error)
|
||||
self.showError = true
|
||||
}
|
||||
})
|
||||
|
||||
} catch {
|
||||
self.accountUpdateError = .keyChainError
|
||||
self.showError = true
|
||||
}
|
||||
|
||||
case .failure:
|
||||
self.accountUpdateError = .networkError
|
||||
self.showError = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
//
|
||||
// AddReaderAPIViewModel.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
|
||||
|
||||
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 = ""
|
||||
@Published var canDismiss: Bool = false
|
||||
|
||||
func authenticateReaderAccount(_ accountType: AccountType) {
|
||||
isAuthenticating = true
|
||||
|
||||
let credentials = Credentials(type: .readerBasic, username: username, secret: password)
|
||||
|
||||
if accountType == .freshRSS {
|
||||
Account.validateCredentials(type: accountType, credentials: credentials, endpoint: URL(string: apiUrl)!) { result in
|
||||
|
||||
self.isAuthenticating = false
|
||||
|
||||
switch result {
|
||||
case .success(let validatedCredentials):
|
||||
|
||||
guard let validatedCredentials = validatedCredentials else {
|
||||
self.accountUpdateError = .invalidUsernamePassword
|
||||
self.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)
|
||||
self.canDismiss = true
|
||||
account.refreshAll(completion: { result in
|
||||
switch result {
|
||||
case .success:
|
||||
break
|
||||
case .failure(let error):
|
||||
self.accountUpdateError = .other(error: error)
|
||||
self.showError = true
|
||||
}
|
||||
})
|
||||
|
||||
} catch {
|
||||
self.accountUpdateError = .keyChainError
|
||||
self.showError = true
|
||||
}
|
||||
|
||||
case .failure:
|
||||
self.accountUpdateError = .networkError
|
||||
self.showError = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else {
|
||||
|
||||
Account.validateCredentials(type: accountType, credentials: credentials) { result in
|
||||
|
||||
self.isAuthenticating = false
|
||||
|
||||
switch result {
|
||||
case .success(let validatedCredentials):
|
||||
|
||||
guard let validatedCredentials = validatedCredentials else {
|
||||
self.accountUpdateError = .invalidUsernamePassword
|
||||
self.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)
|
||||
self.canDismiss = true
|
||||
account.refreshAll(completion: { result in
|
||||
switch result {
|
||||
case .success:
|
||||
break
|
||||
case .failure(let error):
|
||||
self.accountUpdateError = .other(error: error)
|
||||
self.showError = true
|
||||
}
|
||||
})
|
||||
|
||||
} catch {
|
||||
self.accountUpdateError = .keyChainError
|
||||
self.showError = true
|
||||
}
|
||||
|
||||
case .failure:
|
||||
self.accountUpdateError = .networkError
|
||||
self.showError = true
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user