[ABANDONED] Mastodon iOS client.
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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // AuthenticateViewController.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 8/26/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import Alamofire
  9. import CoreData
  10. import UIKit
  11. import SafariServices
  12. class AuthenticateViewController: UIViewController {
  13. @IBOutlet var signInButton: UIButton!
  14. @IBOutlet var instanceTextField: UITextField!
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. signInButton.layer.cornerRadius = 10
  18. signInButton.clipsToBounds = true
  19. instanceTextField.attributedPlaceholder = NSAttributedString(string: "mastodon.social", attributes: [NSAttributedString.Key.foregroundColor: UIColor.init(red: 0.9, green: 0.9, blue: 0.9, alpha: 1)])
  20. }
  21. override open var shouldAutorotate: Bool {
  22. return false
  23. }
  24. func authorize(client: ClientMO) {
  25. AuthenticationManager.authenticationState = NSUUID().uuidString
  26. AuthenticationManager.authenticationClient = client
  27. AuthenticationManager.authenticationDelegate = self
  28. let parameters = [
  29. "client_id": client.clientID!,
  30. "response_type": "code",
  31. "redirect_uri": Config.clientRedirectURI,
  32. "scope": "read write follow".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!,
  33. ]
  34. let parameterString = parameters.map { "\($0.key)=\($0.value)" }.joined(separator: "&")
  35. let authorizeURL = URL(string: "https://\(client.host!)/oauth/authorize?\(parameterString)")
  36. let controller = SFSafariViewController(url: authorizeURL!)
  37. controller.delegate = self
  38. controller.dismissButtonStyle = .cancel
  39. controller.preferredControlTintColor = UIColor(named: "Primary")
  40. present(controller, animated: true)
  41. }
  42. @IBAction func signIn(_ sender: Any) {
  43. guard var host = instanceTextField.text, !host.isEmpty else {
  44. return
  45. }
  46. if host.contains("@") {
  47. if let urlPart = host.split(separator: "@").last {
  48. host = String(urlPart)
  49. } else {
  50. return
  51. }
  52. }
  53. if host.starts(with: "https://") {
  54. host = String(host.dropFirst(8))
  55. }
  56. if host.starts(with: "http://") {
  57. host = String(host.dropFirst(7))
  58. }
  59. signInButton.isEnabled = false
  60. defer {
  61. signInButton.isEnabled = true
  62. }
  63. let request = NSFetchRequest<ClientMO>(entityName: "Client")
  64. request.predicate = NSPredicate(format: "host == %@", host)
  65. do {
  66. let response = try CoreDataManager.shared.context.fetch(request)
  67. if let client = response.first {
  68. self.authorize(client: client)
  69. } else {
  70. MastodonAPI.registerApp(serverURL: URL(string: "https://\(host)")!) { data, error in
  71. guard let data = data, error == nil else {
  72. print("\(String(describing: error))")
  73. return
  74. }
  75. let client = MastodonDataManager.saveClient(
  76. id: data["id"] as! String,
  77. clientID: data["client_id"] as! String,
  78. clientSecret: data["client_secret"] as! String,
  79. host: host
  80. )
  81. self.authorize(client: client)
  82. }
  83. }
  84. } catch {
  85. AlertManager.shared.show(message: error.localizedDescription, category: .error)
  86. }
  87. }
  88. }
  89. extension AuthenticateViewController: SFSafariViewControllerDelegate {
  90. func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
  91. controller.dismiss(animated: true)
  92. }
  93. }
  94. extension AuthenticateViewController: AuthenticationDelegate {
  95. func authenticationSuccess(_ sender: Any) {
  96. NotificationCenter.default.post(name: .didAuthenticate, object: nil)
  97. DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
  98. self.dismiss(animated: true)
  99. }
  100. }
  101. func authenticationFailure(_ sender: Any, error: Error?) {
  102. AlertManager.shared.show(message: error?.localizedDescription ?? "Authentication error", category: .error)
  103. }
  104. }