2020-03-23 00:52:48 +01:00
|
|
|
//
|
|
|
|
// AccountsNewsBlurWindowController.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Anh Quang Do on 2020-03-22.
|
|
|
|
// Copyright (c) 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import AppKit
|
|
|
|
import Account
|
2024-04-02 04:31:57 +02:00
|
|
|
import Web
|
2020-04-10 04:07:56 +02:00
|
|
|
import Secrets
|
2020-03-23 00:52:48 +01:00
|
|
|
|
|
|
|
class AccountsNewsBlurWindowController: NSWindowController {
|
2020-11-06 12:07:28 +01:00
|
|
|
|
|
|
|
@IBOutlet weak var signInTextField: NSTextField!
|
|
|
|
@IBOutlet weak var noAccountTextField: NSTextField!
|
|
|
|
@IBOutlet weak var createNewAccountButton: NSButton!
|
2020-03-23 00:52:48 +01:00
|
|
|
@IBOutlet weak var progressIndicator: NSProgressIndicator!
|
|
|
|
@IBOutlet weak var usernameTextField: NSTextField!
|
|
|
|
@IBOutlet weak var passwordTextField: NSSecureTextField!
|
|
|
|
@IBOutlet weak var errorMessageLabel: NSTextField!
|
|
|
|
@IBOutlet weak var actionButton: NSButton!
|
|
|
|
|
|
|
|
var account: Account?
|
|
|
|
|
|
|
|
private weak var hostWindow: NSWindow?
|
|
|
|
|
|
|
|
convenience init() {
|
|
|
|
self.init(windowNibName: NSNib.Name("AccountsNewsBlur"))
|
|
|
|
}
|
|
|
|
|
|
|
|
override func windowDidLoad() {
|
|
|
|
if let account = account, let credentials = try? account.retrieveCredentials(type: .newsBlurBasic) {
|
|
|
|
usernameTextField.stringValue = credentials.username
|
|
|
|
actionButton.title = NSLocalizedString("Update", comment: "Update")
|
2020-11-06 12:07:28 +01:00
|
|
|
signInTextField.stringValue = NSLocalizedString("Update your NewsBlur account credentials.", comment: "SignIn")
|
|
|
|
noAccountTextField.isHidden = true
|
|
|
|
createNewAccountButton.isHidden = true
|
2020-03-23 00:52:48 +01:00
|
|
|
} else {
|
|
|
|
actionButton.title = NSLocalizedString("Create", comment: "Create")
|
2020-11-06 12:07:28 +01:00
|
|
|
signInTextField.stringValue = NSLocalizedString("Sign in to your NewsBlur account.", comment: "SignIn")
|
2020-03-23 00:52:48 +01:00
|
|
|
}
|
2021-06-17 06:38:53 +02:00
|
|
|
enableAutofill()
|
2020-11-07 02:42:20 +01:00
|
|
|
usernameTextField.becomeFirstResponder()
|
2020-03-23 00:52:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
|
|
func runSheetOnWindow(_ hostWindow: NSWindow, completion: ((NSApplication.ModalResponse) -> Void)? = nil) {
|
|
|
|
self.hostWindow = hostWindow
|
|
|
|
hostWindow.beginSheet(window!, completionHandler: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction func cancel(_ sender: Any) {
|
|
|
|
hostWindow!.endSheet(window!, returnCode: NSApplication.ModalResponse.cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func action(_ sender: Any) {
|
|
|
|
self.errorMessageLabel.stringValue = ""
|
|
|
|
|
|
|
|
guard !usernameTextField.stringValue.isEmpty else {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Username required.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
2020-09-25 03:20:01 +02:00
|
|
|
|
2020-10-29 20:05:55 +01:00
|
|
|
guard account != nil || !AccountManager.shared.duplicateServiceAccount(type: .newsBlur, username: usernameTextField.stringValue) else {
|
2020-09-25 03:20:01 +02:00
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("There is already a NewsBlur account with that username created.", comment: "Duplicate Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-23 00:52:48 +01:00
|
|
|
actionButton.isEnabled = false
|
|
|
|
progressIndicator.isHidden = false
|
|
|
|
progressIndicator.startAnimation(self)
|
|
|
|
|
|
|
|
let credentials = Credentials(type: .newsBlurBasic, username: usernameTextField.stringValue, secret: passwordTextField.stringValue)
|
|
|
|
|
2024-04-04 06:15:13 +02:00
|
|
|
Task { @MainActor in
|
|
|
|
|
|
|
|
var validationDidThrow = false
|
|
|
|
var validatedCredentials: Credentials?
|
|
|
|
|
|
|
|
do {
|
|
|
|
validatedCredentials = try await Account.validateCredentials(type: .newsBlur, credentials: credentials, secretsProvider: Secrets())
|
|
|
|
} catch {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Network error. Try again later.", comment: "Credentials Error")
|
|
|
|
validationDidThrow = true
|
|
|
|
}
|
2020-03-23 00:52:48 +01:00
|
|
|
|
|
|
|
self.actionButton.isEnabled = true
|
|
|
|
self.progressIndicator.isHidden = true
|
|
|
|
self.progressIndicator.stopAnimation(self)
|
|
|
|
|
2024-04-04 06:15:13 +02:00
|
|
|
if validationDidThrow {
|
|
|
|
return
|
|
|
|
}
|
2020-03-23 00:52:48 +01:00
|
|
|
|
2024-04-04 06:15:13 +02:00
|
|
|
guard let validatedCredentials else {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Invalid email/password combination.", comment: "Credentials Error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.account == nil {
|
|
|
|
self.account = AccountManager.shared.createAccount(type: .newsBlur)
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
try self.account?.removeCredentials(type: .newsBlurBasic)
|
|
|
|
try self.account?.removeCredentials(type: .newsBlurSessionId)
|
|
|
|
try self.account?.storeCredentials(credentials)
|
|
|
|
try self.account?.storeCredentials(validatedCredentials)
|
2020-03-23 00:52:48 +01:00
|
|
|
|
2024-04-04 06:15:13 +02:00
|
|
|
self.refreshAll()
|
|
|
|
|
|
|
|
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
|
|
|
|
} catch {
|
|
|
|
self.errorMessageLabel.stringValue = NSLocalizedString("Keychain error while storing credentials.", comment: "Credentials Error")
|
2020-03-23 00:52:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 12:07:28 +01:00
|
|
|
|
2024-04-04 06:15:13 +02:00
|
|
|
private func refreshAll() {
|
|
|
|
|
|
|
|
Task { @MainActor in
|
|
|
|
do {
|
|
|
|
try await self.account?.refreshAll()
|
|
|
|
} catch {
|
|
|
|
NSApplication.shared.presentError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 12:07:28 +01:00
|
|
|
@IBAction func createAccountWithProvider(_ sender: Any) {
|
|
|
|
NSWorkspace.shared.open(URL(string: "https://newsblur.com")!)
|
|
|
|
}
|
|
|
|
|
2021-06-17 06:38:53 +02:00
|
|
|
// MARK: Autofill
|
2024-02-28 05:06:57 +01:00
|
|
|
|
2021-06-17 06:38:53 +02:00
|
|
|
func enableAutofill() {
|
2024-02-28 05:06:57 +01:00
|
|
|
|
|
|
|
usernameTextField.contentType = .username
|
|
|
|
passwordTextField.contentType = .password
|
2021-06-17 06:38:53 +02:00
|
|
|
}
|
2020-03-23 00:52:48 +01:00
|
|
|
}
|