RedditWebAuthHelper.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import Foundation
  2. enum RedditWebAuthHelper {
  3. static let redditHomeURL = URL(string: "https://www.reddit.com")!
  4. static let redditLoginURL = URL(string: "https://www.reddit.com/login/")!
  5. private static let oauthHosts = [
  6. "accounts.google.com",
  7. "appleid.apple.com",
  8. "idmsa.apple.com",
  9. "gsa.apple.com",
  10. "login.microsoftonline.com",
  11. "login.live.com",
  12. "account.live.com",
  13. "login.microsoft.com",
  14. "www.facebook.com",
  15. "m.facebook.com",
  16. "facebook.com",
  17. "auth0.com",
  18. ]
  19. private static let authFlowPathPrefixes = [
  20. "/login",
  21. "/register",
  22. "/password",
  23. "/account/login",
  24. "/account/register",
  25. "/account/signup",
  26. ]
  27. static func isOAuthProviderURL(_ url: URL) -> Bool {
  28. guard let host = url.host?.lowercased() else { return false }
  29. if oauthHosts.contains(where: { host == $0 || host.hasSuffix(".\($0)") }) {
  30. return true
  31. }
  32. if host == "google.com" || host.hasSuffix(".google.com") {
  33. let path = url.path.lowercased()
  34. return path.contains("oauth")
  35. || path.contains("signin")
  36. || path.contains("gsi")
  37. }
  38. if host == "apple.com" || host.hasSuffix(".apple.com") {
  39. let path = url.path.lowercased()
  40. return path.contains("auth")
  41. || path.contains("signin")
  42. || path.contains("sso")
  43. }
  44. if host == "facebook.com" || host.hasSuffix(".facebook.com") {
  45. let path = url.path.lowercased()
  46. return path.contains("oauth") || path.contains("login") || path.contains("dialog")
  47. }
  48. if host.hasSuffix(".auth0.com") {
  49. return true
  50. }
  51. return false
  52. }
  53. static func isRedditHomeURL(_ url: URL) -> Bool {
  54. guard isRedditURL(url) else { return false }
  55. let host = url.host?.lowercased() ?? ""
  56. let isMainSite = host == "reddit.com"
  57. || host == "www.reddit.com"
  58. || host == "new.reddit.com"
  59. || host == "old.reddit.com"
  60. guard isMainSite else { return false }
  61. let path = url.path.lowercased()
  62. return path.isEmpty || path == "/" || path == "/home"
  63. }
  64. static func isRedditURL(_ url: URL) -> Bool {
  65. guard let host = url.host?.lowercased() else { return false }
  66. let redditHosts = [
  67. "reddit.com",
  68. "www.reddit.com",
  69. "old.reddit.com",
  70. "new.reddit.com",
  71. "accounts.reddit.com",
  72. "oauth.reddit.com",
  73. "reddit.app.link",
  74. "redd.it",
  75. ]
  76. return redditHosts.contains(where: { host == $0 || host.hasSuffix(".\($0)") })
  77. }
  78. static func isAuthFlowURL(_ url: URL) -> Bool {
  79. guard isRedditURL(url) else { return false }
  80. let path = url.path.lowercased()
  81. return authFlowPathPrefixes.contains { prefix in
  82. path == prefix || path.hasPrefix("\(prefix)/")
  83. }
  84. }
  85. static func isCredentialLoginPage(_ url: URL) -> Bool {
  86. guard isRedditURL(url) else { return false }
  87. let path = url.path.lowercased()
  88. guard path == "/login" || path.hasPrefix("/login/") else { return false }
  89. let host = url.host?.lowercased() ?? ""
  90. return host == "www.reddit.com" || host == "reddit.com" || host == "new.reddit.com"
  91. }
  92. static func isAccountsRedditHost(_ url: URL) -> Bool {
  93. url.host?.lowercased() == "accounts.reddit.com"
  94. }
  95. /// Auth subframes to accounts.reddit.com are required on the modern login page.
  96. /// Only redirect subframe auth loads when the main frame is not already on login.
  97. static func shouldRedirectAuthSubframe(mainFrameURL: URL?) -> Bool {
  98. guard let mainFrameURL else { return true }
  99. if isCredentialLoginPage(mainFrameURL) { return false }
  100. return !isAuthFlowURL(mainFrameURL)
  101. }
  102. static func looksLikeLoggedInURL(_ url: URL) -> Bool {
  103. guard isRedditURL(url) else { return false }
  104. if isAuthFlowURL(url) { return false }
  105. let path = url.path.lowercased()
  106. if url.host?.lowercased() == "accounts.reddit.com" {
  107. if path.contains("/oauth") || path.contains("/authorize") || path.contains("/login") {
  108. return false
  109. }
  110. return path.isEmpty || path == "/" || path.hasPrefix("/api/")
  111. }
  112. return true
  113. }
  114. /// Returns true when the OAuth completion panel can close and hand off to the main WebView.
  115. static func shouldCompleteOAuthSignIn(for url: URL) -> Bool {
  116. guard isRedditURL(url) else { return false }
  117. if isAuthFlowURL(url) { return false }
  118. let host = url.host?.lowercased() ?? ""
  119. let path = url.path.lowercased()
  120. if host == "accounts.reddit.com" {
  121. if path.contains("/oauth")
  122. || path.contains("/authorize")
  123. || path.contains("/login")
  124. || path.hasPrefix("/api/") {
  125. return false
  126. }
  127. return path.isEmpty || path == "/"
  128. }
  129. if host == "www.reddit.com"
  130. || host == "reddit.com"
  131. || host == "new.reddit.com"
  132. || host == "old.reddit.com" {
  133. return true
  134. }
  135. return looksLikeLoggedInURL(url)
  136. }
  137. static func oauthCallbackHost(for url: URL) -> String {
  138. if let host = parsedRedirectHost(from: url) {
  139. return host
  140. }
  141. return "accounts.reddit.com"
  142. }
  143. private static func parsedRedirectHost(from url: URL) -> String? {
  144. guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
  145. return nil
  146. }
  147. let redirectValue = components.queryItems?.first(where: {
  148. $0.name == "redirect_uri" || $0.name == "redirect_url" || $0.name == "redirect"
  149. })?.value
  150. guard let redirectValue else { return nil }
  151. let decoded = redirectValue.removingPercentEncoding ?? redirectValue
  152. if let host = hostFromRedirectString(decoded) {
  153. return host
  154. }
  155. if !decoded.contains("://"), let host = hostFromRedirectString("https://\(decoded)") {
  156. return host
  157. }
  158. return nil
  159. }
  160. private static func hostFromRedirectString(_ value: String) -> String? {
  161. guard let redirectURL = URL(string: value),
  162. let host = redirectURL.host?.lowercased(),
  163. isRedditURL(redirectURL) else {
  164. return nil
  165. }
  166. return host
  167. }
  168. }