This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
/
UrpService.swift
174 lines (146 loc) · 6.38 KB
/
UrpService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import SafariServices
import Shared
import BraveShared
import CertificateUtilities
import SwiftyJSON
import os.log
import Combine
import BraveCore
enum UrpError {
case networkError, downloadIdNotFound, ipNotFound, endpointError
}
/// Api endpoints for user referral program.
struct UrpService {
private struct ParamKeys {
static let api = "api_key"
static let referralCode = "referral_code"
static let platform = "platform"
static let downLoadId = "download_id"
}
private let host: String
private let adServicesURL: String
private let apiKey: String
private let sessionManager: URLSession
private let certificateEvaluator: URPCertificatePinningService
init?(host: String, apiKey: String, adServicesURL: String) {
self.host = host
self.apiKey = apiKey
self.adServicesURL = adServicesURL
// Certificate pinning
certificateEvaluator = URPCertificatePinningService()
sessionManager = URLSession(configuration: .default, delegate: certificateEvaluator, delegateQueue: .main)
}
func referralCodeLookup(refCode: String?, completion: @escaping (ReferralData?, UrpError?) -> Void) {
guard var endPoint = URL(string: host) else {
completion(nil, .endpointError)
UrpLog.log("Host not a url: \(host)")
return
}
var params = [UrpService.ParamKeys.api: apiKey]
var lastPathComponent = "ua"
if let refCode = refCode {
params[UrpService.ParamKeys.referralCode] = refCode
params[UrpService.ParamKeys.platform] = "ios"
lastPathComponent = "nonua"
}
endPoint.append(pathComponents: "promo", "initialize", lastPathComponent)
sessionManager.urpApiRequest(endPoint: endPoint, params: params) { response in
switch response {
case .success(let data):
if let data = data as? Data {
Logger.module.debug("Referral code lookup response: \(String(data: data, encoding: .utf8) ?? "nil")")
}
UrpLog.log("Referral code lookup response: \(data)")
let json = JSON(data)
let referral = ReferralData(json: json)
completion(referral, nil)
case .failure(let error):
Logger.module.error("Referral code lookup response: \(error.localizedDescription)")
UrpLog.log("Referral code lookup response: \(error.localizedDescription)")
completion(nil, .endpointError)
}
}
}
@MainActor func adCampaignTokenLookupQueue(adAttributionToken: String) async throws -> (AdAttributionData?) {
guard let endPoint = URL(string: adServicesURL) else {
Logger.module.error("AdServicesURLString can not be resolved: \(adServicesURL)")
throw URLError(.badURL)
}
let attributionDataToken = adAttributionToken.data(using: .utf8)
do {
let (result, _) = try await sessionManager.adServicesAttributionApiRequest(endPoint: endPoint, rawData: attributionDataToken)
UrpLog.log("Ad Attribution response: \(result)")
if let resultData = result as? Data {
let jsonResponse = try JSONSerialization.jsonObject(with: resultData, options: []) as? [String: Any]
let adAttributionData = try AdAttributionData(json: jsonResponse)
return adAttributionData
}
} catch {
throw error
}
return (nil)
}
func checkIfAuthorizedForGrant(with downloadId: String, completion: @escaping (Bool?, UrpError?) -> Void) {
guard var endPoint = URL(string: host) else {
completion(nil, .endpointError)
return
}
endPoint.append(pathComponents: "promo", "activity")
let params = [
UrpService.ParamKeys.api: apiKey,
UrpService.ParamKeys.downLoadId: downloadId,
]
sessionManager.urpApiRequest(endPoint: endPoint, params: params) { response in
switch response {
case .success(let data):
if let data = data as? Data {
Logger.module.debug("Check if authorized for grant response: \(String(data: data, encoding: .utf8) ?? "nil")")
}
let json = JSON(data)
completion(json["finalized"].boolValue, nil)
case .failure(let error):
Logger.module.error("Check if authorized for grant response: \(error.localizedDescription)")
completion(nil, .endpointError)
}
}
}
}
extension URLSession {
/// All requests to referral api use PUT method, accept and receive json.
func urpApiRequest(endPoint: URL, params: [String: String], completion: @escaping (Result<Any, Error>) -> Void) {
request(endPoint, method: .put, parameters: params, encoding: .json) { response in
completion(response)
}
}
// Apple ad service attricution request requires plain text encoding with post method and passing token as rawdata
func adServicesAttributionApiRequest(endPoint: URL, rawData: Data?) async throws -> (Any, URLResponse) {
// According to attributiontoken API docs
// An error reponse can occur API call is done too quickly after receiving a valid token.
// A best practice is to initiate retries at intervals of 5 seconds, with a maximum of three attempts.
return try await Task.retry(retryCount: 3, retryDelay: 5) {
return try await self.request(endPoint, method: .post, rawData: rawData, encoding: .textPlain)
}.value
}
}
class URPCertificatePinningService: NSObject, URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge) async -> (URLSession.AuthChallengeDisposition, URLCredential?) {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
if let serverTrust = challenge.protectionSpace.serverTrust {
let result = BraveCertificateUtility.verifyTrust(serverTrust, host: challenge.protectionSpace.host, port: challenge.protectionSpace.port)
// Cert is valid and should be pinned
if result == 0 {
return (.useCredential, URLCredential(trust: serverTrust))
}
// Cert is valid and should not be pinned
// Let the system handle it and we'll show an error if the system cannot validate it
if result == Int32.min {
return (.performDefaultHandling, nil)
}
}
return (.cancelAuthenticationChallenge, nil)
}
return (.performDefaultHandling, nil)
}
}