Home > Software engineering >  How to make Apple Sign In Revoke Token POST Request?
How to make Apple Sign In Revoke Token POST Request?

Time:06-04

How do I structure a swift POST request to satisfy the upcoming June 30th requirements?

I am not sure what form-data, client_id, client_secret, token, or token_type_hint are supposed to be. I was able to implement Sign in With Apple to create a user, but very lost on the deletion part of this.

I am looking to perform this client-side with Swift, as my application does not have an option to use Server Side for its use case.

enter image description here

CodePudding user response:

Here is some unstructured solution to give you an idea on how to implement this. In the end you are going to call AppleAuth.revokeTokens() when you need to.

import SwiftUI

struct AppleAuth {
    // IRL Use keychain for this instead
    @AppStorage("JWT_client_secret") static var clientSecret: String = ""
    @AppStorage("AppleToken") static var appleToken: String = ""
    static func revokeTokens(completion: (([String: Any]?, Error?) -> Void)? = nil) {
        
        let paramString: [String : Any] = [
            "client_id": Bundle.main.bundleIdentifier!, //"com.MyCompany.Name",
            "client_secret": clientSecret,
            "token": appleToken,
            "token_type_hint": "access_token"
        ]
        let url = URL(string: "https://appleid.apple.com/auth/revoke")!
        
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        
        do {
            request.httpBody = try JSONSerialization.data(withJSONObject:paramString, options: .prettyPrinted)
        } catch let error {
            print(error.localizedDescription)
            completion?(nil, error)
        }
        
        request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        
        let task =  URLSession.shared.dataTask(with: request as URLRequest)  { (data, response, error) in
            guard
                let response = response as? HTTPURLResponse,
                error == nil
            else {                                                               
                print("error", error ?? URLError(.badServerResponse))
                return
            }
            
            guard (200 ... 299) ~= response.statusCode else {                 
                print("statusCode should be 2xx, but is \(response.statusCode)")
                print("response = \(response)")
                return
            }
            
            
            if let error = error {
                print(error)
            }else{
                print("deleted accont")
            }
        }
        task.resume()
    }

}
  • Related