| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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",
- "login.microsoftonline.com",
- "login.live.com",
- "account.live.com",
- "login.microsoft.com",
- "www.facebook.com",
- "m.facebook.com",
- "facebook.com",
- "auth0.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")
- }
- if host == "facebook.com" || host.hasSuffix(".facebook.com") {
- let path = url.path.lowercased()
- return path.contains("oauth") || path.contains("login") || path.contains("dialog")
- }
- if host.hasSuffix(".auth0.com") {
- return true
- }
- 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"
- }
- }
|