Skip to content

Commit

Permalink
fetch app certs
Browse files Browse the repository at this point in the history
  • Loading branch information
daytime-em committed May 1, 2024
1 parent dd2e913 commit 45ea7bc
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions Sources/MuxPlayerSwift/Fairplay/FairplaySessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,58 @@ class FairplaySessionManager {

/// Requests the App Certificate for a playback id
func requestCertificate(
fromDomain domain: String,
fromDomain rootDomain: String,
playbackID: String,
drmToken: String,
completion: (Result<Data, Error>) -> Void
completion requestCompletion: @escaping (Result<Data, Error>) -> Void
) {
// todo - request app certficate from the backend
let tempCert = ProcessInfo.processInfo.environment["APP_CERT_BASE64"]
//print("CERTIFICATE :: temp app cert is \(tempCert)")
let url = makeAppCertificateURL(
playbackId: playbackID,
drmToken: drmToken,
licenseDomain: makeLicenseDomain(rootDomain)
)
var request = URLRequest(url: url)
request.httpMethod = "GET"

guard let tempCert = tempCert else {
completion(Result.failure(CancellationError())) // todo - a real Error type
return
print("Getting app cert from \(url)")
let dataTask = urlSession.dataTask(with: request) { [requestCompletion] data, response, error in
print("!--! APP CERT RESPONSE")
var responseCode: Int? = nil
if let httpResponse = response as? HTTPURLResponse {
responseCode = httpResponse.statusCode
print("Cert response code: \(httpResponse.statusCode)")
print("Cert response headers: ", httpResponse.allHeaderFields)
if let errorBody = data {
let errorUtf = String(data: errorBody, encoding: .utf8)
print("Cert Error: \(errorUtf)")

Check warning on line 55 in Sources/MuxPlayerSwift/Fairplay/FairplaySessionManager.swift

View workflow job for this annotation

GitHub Actions / Run Unit Tests

string interpolation produces a debug description for an optional value; did you mean to make this explicit?
}

}
// error case: I/O finished with non-successful response
guard responseCode == 200 else {
print("Cert request failed: \(responseCode)")

Check warning on line 61 in Sources/MuxPlayerSwift/Fairplay/FairplaySessionManager.swift

View workflow job for this annotation

GitHub Actions / Run Unit Tests

string interpolation produces a debug description for an optional value; did you mean to make this explicit?
requestCompletion(Result.failure(TempError()))
return
}
// error case: I/O failed
if let error = error {
print("Cert Request Failed: \(error.localizedDescription)")
requestCompletion(Result.failure(error)) // todo - real Error type
return
}
// strange edge case: 200 with no response body
guard let data = data else {
print("No cert data despite server returning success")
requestCompletion(Result.failure(TempError())) // todo - real Error type
return
}

print(">> App Cert Response data:\(data.base64EncodedString())")

requestCompletion(Result.success(data))
}

let certData = Data(base64Encoded: tempCert, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)!
print("CERTIFICATE :: Delivering App Certificate")
completion(Result.success(certData))
dataTask.resume()
}

/// Requests a license to play based on the given SPC data
Expand All @@ -59,9 +94,6 @@ class FairplaySessionManager {
offline _: Bool,
completion licenseRequestComplete: @escaping (Result<Data, Error>) -> Void
) {
// TODO: Need to calculate license Domain from input playbackDomain
// ie, stream.mux.com -> license.mux.com or custom.domain.com -> TODO: ????
//let licenseDomain = "license.gcp-us-west1-vos1.staging.mux.com"
let url = makeLicenseURL(
playbackId: playbackID,
drmToken: drmToken,
Expand Down

0 comments on commit 45ea7bc

Please sign in to comment.