You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
4.3 KiB
128 lines
4.3 KiB
//
|
|
// AuthenticateViewController.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 8/26/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import Alamofire
|
|
import CoreData
|
|
import UIKit
|
|
import SafariServices
|
|
|
|
class AuthenticateViewController: UIViewController {
|
|
@IBOutlet var signInButton: UIButton!
|
|
@IBOutlet var instanceTextField: UITextField!
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
signInButton.layer.cornerRadius = 10
|
|
signInButton.clipsToBounds = true
|
|
|
|
instanceTextField.attributedPlaceholder = NSAttributedString(string: "mastodon.social", attributes: [NSAttributedString.Key.foregroundColor: UIColor.init(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)])
|
|
}
|
|
|
|
override open var shouldAutorotate: Bool {
|
|
return false
|
|
}
|
|
|
|
func authorize(client: ClientMO) {
|
|
AuthenticationManager.authenticationState = NSUUID().uuidString
|
|
AuthenticationManager.authenticationClient = client
|
|
AuthenticationManager.authenticationDelegate = self
|
|
|
|
let parameters = [
|
|
"client_id": client.clientID!,
|
|
"response_type": "code",
|
|
"redirect_uri": Config.clientRedirectURI,
|
|
"scope": "read write follow".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!,
|
|
]
|
|
|
|
let parameterString = parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
|
|
let authorizeURL = URL(string: "https://\(client.host!)/oauth/authorize?\(parameterString)")
|
|
|
|
let controller = SFSafariViewController(url: authorizeURL!)
|
|
controller.delegate = self
|
|
controller.dismissButtonStyle = .cancel
|
|
controller.preferredControlTintColor = UIColor(named: "Primary")
|
|
|
|
present(controller, animated: true)
|
|
}
|
|
|
|
@IBAction func signIn(_ sender: Any) {
|
|
guard var host = instanceTextField.text, !host.isEmpty else {
|
|
return
|
|
}
|
|
|
|
if host.contains("@") {
|
|
if let urlPart = host.split(separator: "@").last {
|
|
host = String(urlPart)
|
|
} else {
|
|
return
|
|
}
|
|
}
|
|
|
|
if host.starts(with: "https://") {
|
|
host = String(host.dropFirst(8))
|
|
}
|
|
|
|
if host.starts(with: "http://") {
|
|
host = String(host.dropFirst(7))
|
|
}
|
|
|
|
signInButton.isEnabled = false
|
|
defer {
|
|
signInButton.isEnabled = true
|
|
}
|
|
|
|
let request = NSFetchRequest<ClientMO>(entityName: "Client")
|
|
request.predicate = NSPredicate(format: "host == %@", host)
|
|
|
|
do {
|
|
let response = try CoreDataManager.shared.context.fetch(request)
|
|
if let client = response.first {
|
|
self.authorize(client: client)
|
|
} else {
|
|
MastodonAPI.registerApp(serverURL: URL(string: "https://\(host)")!) { data, error in
|
|
guard let data = data, error == nil else {
|
|
print("\(String(describing: error))")
|
|
return
|
|
}
|
|
|
|
let client = MastodonDataManager.saveClient(
|
|
id: data["id"] as! String,
|
|
clientID: data["client_id"] as! String,
|
|
clientSecret: data["client_secret"] as! String,
|
|
host: host
|
|
)
|
|
|
|
self.authorize(client: client)
|
|
}
|
|
}
|
|
} catch {
|
|
AlertManager.shared.show(message: error.localizedDescription, category: .error)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension AuthenticateViewController: SFSafariViewControllerDelegate {
|
|
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
|
|
controller.dismiss(animated: true)
|
|
}
|
|
}
|
|
|
|
extension AuthenticateViewController: AuthenticationDelegate {
|
|
func authenticationSuccess(_ sender: Any) {
|
|
NotificationCenter.default.post(name: .didAuthenticate, object: nil)
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
|
|
self.dismiss(animated: true)
|
|
}
|
|
}
|
|
|
|
func authenticationFailure(_ sender: Any, error: Error?) {
|
|
AlertManager.shared.show(message: error?.localizedDescription ?? "Authentication error", category: .error)
|
|
}
|
|
}
|