Token generation/encoding not working properly

So I’m using DeviceCheck to create a unique token per iOS device and send it to my server. The problem is, that the token I’m receiving seems to be much longer than expected (200+ characters).

 

Here’s the code:

 

@IBAction func sendInfo(_ sender: Any) {
        
        generateToken()
        
        //not actual server
        let url = URL(string: "http://myserver.com/test.asp?")
        var request = URLRequest(url: url!)
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.httpMethod = "POST"
        let postString = "v1=(prefix.text!)&v2=(email.text!)&v3=(verification)"
        request.httpBody = postString.data(using: .utf8)

        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {
                print("error=(String(describing: error))")
                return
            }

            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
                print("statusCode should be 200, but is (httpStatus.statusCode)")
                print("response = (String(describing: response))")
            }

            let responseString = String(data: data, encoding: .utf8)
            print("responseString = (String(describing: responseString))")
        }
        task.resume()
        
    }

func generateToken(){
        
        if #available(iOS 11.0, *) {
            let currentDevice = DCDevice.current
            
            if currentDevice.isSupported{
                currentDevice.generateToken(completionHandler: { (data, error) in
                    DispatchQueue.main.async {
                        if let tokenData = data?.base64EncodedString() {
                            
                            self.verification = tokenData
                            
                            let alert = UIAlertController(title: "Encoded Device ID", message: "(self.verification)", preferredStyle: .alert)
                            alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
                            self.present(alert, animated: true)
                        } else {
                            self.verification = error?.localizedDescription ?? "Something Wrong!!!"
                            
                            let alert = UIAlertController(title: "Error", message: "(self.verification)", preferredStyle: .alert)
                            alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
                            self.present(alert, animated: true)
                        }
                    }
                })
            } else {
                self.verification = "Device not supported."
                
                let alert = UIAlertController(title: "Error", message: "(verification)", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
                self.present(alert, animated: true)
            }
        } else {
            // Fallback on earlier versions
        }
    }

 

What it does, is it sends 2 text inputs and an encoded token to the server with a click of a send button. Not sure why the token is so long (of if its supposed to be that long)..

 

Any help would be greatly appreciated.

 

Edit:

 

Edited the code, breaking down the generateToken function for clarity purposes. Still functions the same as the unedited version.

Powered by WPeMatico

About

You may also like...

Comments are closed.