ios – Connecting to Apple – System Test Api


I’m attempting to examine if my app was ever put in on a tool utilizing System Test Api by apple. But it surely all the time returns error 400: Lacking or incorrectly formatted payload

NOTE: I’ve already checked present stackoverflow questions like:

Connecting to Apple Retailer Join utilizing Swift (with SwiftJWT) and REST API’s – failing with 401

How do I generate a JWT to make use of in API authentication for Swift app

None of them helped my resolving the problem.

As a substitute of server facet implementation, we are attempting to fetch this data from swift code itself.

Under is the code to create a JWT token utilizing SwiftJWT.

func generateJWT() -> String? {
    let myHeader = Header(child: "*********")
    let myClaims = MyClaims(iss: "*******", iat: Int(Date().timeIntervalSince1970))
    
    var myJWT = JWT(header: myHeader, claims: myClaims)
    
    guard let privateKeyPath = Bundle.foremost.url(forResource: "AuthKey", withExtension: ".p8") else { return nil }
    
    do {
        let privateKey: Information = attempt Information(contentsOf: privateKeyPath, choices: .alwaysMapped)
        let jwtSigner = JWTSigner.es256(privateKey: privateKey)
        let signedJWT = attempt myJWT.signal(utilizing: jwtSigner)
        return signedJWT
    } catch {
        print(error.localizedDescription)
    }
    return nil
}

To Fetch System Token and name apple’s System examine api:

non-public func generateIdentifier(completion: @escaping (_ token: String) -> Void) {
    let system = DCDevice.present
    if system.isSupported {
        system.generateToken { information, error in
            if let token = information?.base64EncodedString() {
                completion(token)
            }
        }
    }
}

func queryDeviceInfo() {
    guard let jwtToken = generateJWT() else { return }
    generateIdentifier { token in
        let router = ServiceRouter<ReferralApi>()
        router.alamofireRequest(.queryBits(deviceToken: token,
                                           jwtToken: jwtToken)) { [weak self] (response) in
            
            
        }
    }
}

Here’s what’s inside .queryBits(deviceToken: token, jwtToken: jwtToken)

 var url: String {
    change self {
    case .queryBits:
        return "https://api.growth.devicecheck.apple.com/v1/query_two_bits"
        default:
            return "(baseURL)(path)"
    }
}

var headers: HTTPHeaders? {
    change self {
    case .queryBits(_, let jwt):
        let dic: HTTPHeaders = [
            "Accept": "application/json",
            "Authorization" : "Bearer (jwt)"
        ]
        return dic
    }
}
var parameters: [String: Any]? {
    change self {
    case .queryBits(let deviceToken, _):
        return [
            "device_token": deviceToken,
            "timestamp": Date().timeIntervalSince1970*1000,
            "transaction_id": UUID().uuidString]
    default:
            return nil
    }
}

Any assist could be greater than appreciated.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles