2022-12-04 16:09:32 +05:30
|
|
|
import Alamofire
|
2021-09-25 13:48:22 +05:30
|
|
|
import Foundation
|
|
|
|
import Siesta
|
|
|
|
import SwiftUI
|
|
|
|
|
2021-09-27 01:42:43 +05:30
|
|
|
final class AccountValidator: Service {
|
2022-07-02 02:58:32 +05:30
|
|
|
let app: Binding<VideosApp?>
|
2021-09-25 13:48:22 +05:30
|
|
|
let url: String
|
2021-11-15 04:36:01 +05:30
|
|
|
let account: Account!
|
2021-09-25 13:48:22 +05:30
|
|
|
|
|
|
|
var formObjectID: Binding<String>
|
2021-09-29 02:03:12 +05:30
|
|
|
var isValid: Binding<Bool>
|
|
|
|
var isValidated: Binding<Bool>
|
|
|
|
var isValidating: Binding<Bool>
|
2021-09-25 13:48:22 +05:30
|
|
|
var error: Binding<String?>?
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
private var appsToValidateInstance = VideosApp.allCases
|
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
init(
|
2022-07-02 02:58:32 +05:30
|
|
|
app: Binding<VideosApp?>,
|
2021-09-25 13:48:22 +05:30
|
|
|
url: String,
|
2021-10-21 03:51:50 +05:30
|
|
|
account: Account? = nil,
|
2021-09-27 01:42:43 +05:30
|
|
|
id: Binding<String>,
|
2021-09-29 02:03:12 +05:30
|
|
|
isValid: Binding<Bool>,
|
|
|
|
isValidated: Binding<Bool>,
|
|
|
|
isValidating: Binding<Bool>,
|
2021-09-25 13:48:22 +05:30
|
|
|
error: Binding<String?>? = nil
|
|
|
|
) {
|
2021-10-17 04:18:58 +05:30
|
|
|
self.app = app
|
2021-09-25 13:48:22 +05:30
|
|
|
self.url = url
|
|
|
|
self.account = account
|
2021-09-27 01:42:43 +05:30
|
|
|
formObjectID = id
|
2021-09-29 02:03:12 +05:30
|
|
|
self.isValid = isValid
|
|
|
|
self.isValidated = isValidated
|
|
|
|
self.isValidating = isValidating
|
2021-09-25 13:48:22 +05:30
|
|
|
self.error = error
|
|
|
|
|
|
|
|
super.init(baseURL: url)
|
|
|
|
configure()
|
|
|
|
}
|
|
|
|
|
|
|
|
func configure() {
|
2021-10-17 04:18:58 +05:30
|
|
|
configure {
|
|
|
|
$0.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
|
|
|
|
}
|
|
|
|
|
2021-11-15 04:36:01 +05:30
|
|
|
configure("/login", requestMethods: [.post]) {
|
|
|
|
$0.headers["Content-Type"] = "application/json"
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
func instanceValidationResource(_ app: VideosApp) -> Resource {
|
|
|
|
switch app {
|
|
|
|
case .invidious:
|
|
|
|
return resource("/api/v1/videos/dQw4w9WgXcQ")
|
|
|
|
|
|
|
|
case .piped:
|
|
|
|
return resource("/streams/dQw4w9WgXcQ")
|
2022-12-09 05:45:19 +05:30
|
|
|
|
|
|
|
case .peerTube:
|
|
|
|
// TODO: fixme
|
|
|
|
return resource("")
|
|
|
|
|
|
|
|
case .local:
|
|
|
|
return resource("")
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
func validateInstance() {
|
|
|
|
reset()
|
|
|
|
|
2022-12-09 05:45:19 +05:30
|
|
|
app.wrappedValue = .peerTube
|
|
|
|
setValidationResult(true)
|
|
|
|
return
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
guard let app = appsToValidateInstance.popLast() else { return }
|
|
|
|
tryValidatingUsing(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tryValidatingUsing(_ app: VideosApp) {
|
|
|
|
instanceValidationResource(app)
|
2021-09-25 13:48:22 +05:30
|
|
|
.load()
|
2021-10-17 04:18:58 +05:30
|
|
|
.onSuccess { response in
|
2021-09-25 13:48:22 +05:30
|
|
|
guard self.url == self.formObjectID.wrappedValue else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
guard !response.json.isEmpty else {
|
|
|
|
guard let app = self.appsToValidateInstance.popLast() else {
|
|
|
|
self.isValid.wrappedValue = false
|
|
|
|
self.isValidated.wrappedValue = true
|
|
|
|
self.isValidating.wrappedValue = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.tryValidatingUsing(app)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-08 03:09:28 +05:30
|
|
|
let json = response.json.dictionaryValue
|
2022-07-02 02:58:32 +05:30
|
|
|
let author = app == .invidious ? json["author"] : json["uploader"]
|
2021-11-08 03:09:28 +05:30
|
|
|
|
|
|
|
if author == "Rick Astley" {
|
2022-07-02 02:58:32 +05:30
|
|
|
self.app.wrappedValue = app
|
2021-10-17 04:18:58 +05:30
|
|
|
self.isValid.wrappedValue = true
|
|
|
|
self.error?.wrappedValue = nil
|
|
|
|
} else {
|
|
|
|
self.isValid.wrappedValue = false
|
|
|
|
}
|
2022-07-02 02:58:32 +05:30
|
|
|
self.isValidated.wrappedValue = true
|
|
|
|
self.isValidating.wrappedValue = false
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
.onFailure { error in
|
|
|
|
guard self.url == self.formObjectID.wrappedValue else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
if self.appsToValidateInstance.isEmpty {
|
|
|
|
self.isValidating.wrappedValue = false
|
|
|
|
self.isValidated.wrappedValue = true
|
|
|
|
self.isValid.wrappedValue = false
|
|
|
|
self.error?.wrappedValue = error.userMessage
|
|
|
|
} else {
|
|
|
|
guard let app = self.appsToValidateInstance.popLast() else { return }
|
|
|
|
self.tryValidatingUsing(app)
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 04:36:01 +05:30
|
|
|
func validateAccount() {
|
2021-09-25 13:48:22 +05:30
|
|
|
reset()
|
|
|
|
|
2022-12-04 16:09:32 +05:30
|
|
|
switch app.wrappedValue {
|
|
|
|
case .invidious:
|
|
|
|
invidiousValidation()
|
|
|
|
case .piped:
|
|
|
|
pipedValidation()
|
|
|
|
default:
|
|
|
|
setValidationResult(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func invidiousValidation() {
|
|
|
|
guard let username = account?.username,
|
|
|
|
let password = account?.password
|
|
|
|
else {
|
|
|
|
setValidationResult(false)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
AF
|
|
|
|
.request(login.url, method: .post, parameters: ["email": username, "password": password], encoding: URLEncoding.default)
|
|
|
|
.redirect(using: .doNotFollow)
|
|
|
|
.response { response in
|
|
|
|
guard let headers = response.response?.headers,
|
|
|
|
let cookies = headers["Set-Cookie"]
|
|
|
|
else {
|
|
|
|
self.setValidationResult(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let sidRegex = #"SID=(?<sid>[^;]*);"#
|
|
|
|
guard let sidRegex = try? NSRegularExpression(pattern: sidRegex),
|
|
|
|
let match = sidRegex.matches(in: cookies, range: NSRange(cookies.startIndex..., in: cookies)).first
|
|
|
|
else {
|
|
|
|
self.setValidationResult(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let matchRange = match.range(withName: "sid")
|
|
|
|
|
|
|
|
if let substringRange = Range(matchRange, in: cookies) {
|
|
|
|
let sid = String(cookies[substringRange])
|
|
|
|
if !sid.isEmpty {
|
|
|
|
self.setValidationResult(true)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.setValidationResult(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pipedValidation() {
|
2022-07-02 02:58:32 +05:30
|
|
|
guard let request = accountRequest else {
|
2022-12-04 16:09:32 +05:30
|
|
|
setValidationResult(false)
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
request.onSuccess { response in
|
|
|
|
guard self.account!.username == self.formObjectID.wrappedValue else {
|
|
|
|
return
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
2022-07-02 02:58:32 +05:30
|
|
|
switch self.app.wrappedValue {
|
|
|
|
case .invidious:
|
|
|
|
self.isValid.wrappedValue = true
|
|
|
|
case .piped:
|
|
|
|
let error = response.json.dictionaryValue["error"]?.string
|
|
|
|
let token = response.json.dictionaryValue["token"]?.string
|
|
|
|
self.isValid.wrappedValue = error?.isEmpty ?? !(token?.isEmpty ?? true)
|
|
|
|
self.error!.wrappedValue = error
|
|
|
|
default:
|
|
|
|
return
|
2021-09-29 02:03:12 +05:30
|
|
|
}
|
2022-07-02 02:58:32 +05:30
|
|
|
}
|
|
|
|
.onFailure { _ in
|
|
|
|
guard self.account!.username == self.formObjectID.wrappedValue else {
|
|
|
|
return
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
2022-07-02 02:58:32 +05:30
|
|
|
|
|
|
|
self.isValid.wrappedValue = false
|
|
|
|
}
|
|
|
|
.onCompletion { _ in
|
|
|
|
self.isValidated.wrappedValue = true
|
|
|
|
self.isValidating.wrappedValue = false
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
2022-12-04 16:09:32 +05:30
|
|
|
func setValidationResult(_ result: Bool) {
|
|
|
|
isValid.wrappedValue = result
|
|
|
|
isValidated.wrappedValue = true
|
|
|
|
isValidating.wrappedValue = false
|
|
|
|
}
|
|
|
|
|
|
|
|
var accountRequest: Siesta.Request? {
|
2021-11-15 04:36:01 +05:30
|
|
|
switch app.wrappedValue {
|
|
|
|
case .invidious:
|
2022-08-26 05:06:46 +05:30
|
|
|
guard let password = account.password else { return nil }
|
|
|
|
return login.request(.post, urlEncoded: ["email": account.username, "password": password])
|
2021-11-15 04:36:01 +05:30
|
|
|
case .piped:
|
|
|
|
return login.request(.post, json: ["username": account.username, "password": account.password])
|
2022-07-02 02:58:32 +05:30
|
|
|
default:
|
|
|
|
return nil
|
2021-11-15 04:36:01 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
func reset() {
|
2022-07-02 02:58:32 +05:30
|
|
|
appsToValidateInstance = VideosApp.allCases
|
|
|
|
app.wrappedValue = nil
|
2021-09-29 02:03:12 +05:30
|
|
|
isValid.wrappedValue = false
|
|
|
|
isValidated.wrappedValue = false
|
|
|
|
isValidating.wrappedValue = false
|
2021-09-25 13:48:22 +05:30
|
|
|
error?.wrappedValue = nil
|
|
|
|
}
|
|
|
|
|
2021-11-15 04:36:01 +05:30
|
|
|
var login: Resource {
|
|
|
|
resource("/login")
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
2021-11-08 03:09:28 +05:30
|
|
|
var videoResourceBasePath: String {
|
|
|
|
app.wrappedValue == .invidious ? "/api/v1/videos" : "/streams"
|
|
|
|
}
|
|
|
|
|
|
|
|
var neverGonnaGiveYouUp: Resource {
|
|
|
|
resource("\(videoResourceBasePath)/dQw4w9WgXcQ")
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|