| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import Foundation
- enum RedditWebAuthHelper {
- static let redditHomeURL = URL(string: "https://www.reddit.com")!
- static func isOAuthProviderURL(_ url: URL) -> Bool {
- guard let host = url.host?.lowercased() else { return false }
- let oauthHosts = [
- "accounts.google.com",
- "appleid.apple.com",
- "idmsa.apple.com",
- ]
- if oauthHosts.contains(where: { host == $0 || host.hasSuffix(".\($0)") }) {
- return true
- }
- if host == "google.com" || host.hasSuffix(".google.com") {
- let path = url.path.lowercased()
- return path.contains("oauth")
- || path.contains("signin")
- || path.contains("gsi")
- }
- return false
- }
- static func isRedditURL(_ url: URL) -> Bool {
- guard let host = url.host?.lowercased() else { return false }
- let redditHosts = [
- "reddit.com",
- "www.reddit.com",
- "old.reddit.com",
- "new.reddit.com",
- "accounts.reddit.com",
- "oauth.reddit.com",
- "reddit.app.link",
- "redd.it",
- ]
- return redditHosts.contains(where: { host == $0 || host.hasSuffix(".\($0)") })
- }
- static func looksLikeLoggedInURL(_ url: URL) -> Bool {
- guard isRedditURL(url) else { return false }
- let path = url.path.lowercased()
- let blockedPaths = [
- "/login",
- "/register",
- "/password",
- "/account/login",
- ]
- if blockedPaths.contains(where: { path.hasPrefix($0) }) {
- return false
- }
- if url.host?.lowercased() == "accounts.reddit.com" {
- return path.isEmpty || path == "/" || path.hasPrefix("/api/")
- }
- return true
- }
- static func oauthCallbackHost(for url: URL) -> String {
- guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
- return "accounts.reddit.com"
- }
- let redirectURI = components.queryItems?.first(where: { $0.name == "redirect_uri" })?.value
- ?? components.queryItems?.first(where: { $0.name == "redirect_url" })?.value
- if let redirectURI,
- let redirectURL = URL(string: redirectURI),
- let host = redirectURL.host?.lowercased(),
- isRedditURL(redirectURL) {
- return host
- }
- return "accounts.reddit.com"
- }
- }
|