func authenticationProvider()

in SasquatchMac/SasquatchMac/ViewControllers/AuthenticationViewController.swift [132:180]


    func authenticationProvider(_ authenticationProvider: AnalyticsAuthenticationProvider!, acquireTokenWithCompletionHandler completionHandler: AnalyticsAuthenticationProviderCompletionBlock!) {
        if let refreshUrl = URL(string: self.baseUrl + self.tokenEndpoint) {
            let config = URLSessionConfiguration.default
            let session = URLSession(configuration: config)
            let request = NSMutableURLRequest(url: refreshUrl)
            request.httpMethod = "POST"
            request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
            let bodyString = redirectParam + clientIdParam + refreshParam + refreshToken + scopeParam
            let data: Data = bodyString.data(using: String.Encoding.utf8)!
            NSLog("Started refresh process")
            session.uploadTask(with: request as URLRequest, from: data) { (data, response, error) in
                defer {
                    self.window?.performClose(nil)
                }
                do {
                    guard let data = data else {

                        // Call the completion handler in the error case to send anonymous logs.
                        completionHandler(nil, nil)
                        throw JSONError.NoData
                    }
                    guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary else {

                        // Call the completion handler in the error case to send anonymous logs.
                        completionHandler(nil, nil)
                        throw JSONError.ConversionFailed
                    }
                    if let error = json["error"] as? String, let errorDescription = json["error_description"] as? String {
                        NSLog("Refresh token error: \"\(error)\": \(errorDescription)")

                        // Call the completion handler in the error case to send anonymous logs.
                        completionHandler(nil, nil)
                        return
                    }
                    let token = json["access_token"]! as! String
                    let expiresIn = json["expires_in"]! as! Int64
                    let userId = json["user_id"]! as! String
                    NSLog("Successfully refreshed token for user: %@", userId)

                    // Call the completion handler and pass in the updated token and expiryDate.
                    completionHandler(token, Date().addingTimeInterval(Double(expiresIn)))
                } catch let error as JSONError {
                    NSLog("Error while preforming refresh request: %@", error.rawValue)
                } catch let error as NSError {
                    NSLog("Error while preforming refresh request: %@", error.localizedDescription)
                }
            }.resume()
        }
    }