Skip to content

Commit

Permalink
Add phone auth settings (#12327)
Browse files Browse the repository at this point in the history
  • Loading branch information
pragatimodi authored May 6, 2024
1 parent 13d94d1 commit 8894b50
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array/>
<array>
<string>app-1-1085102361755-ios-2b9d4a82e5df229f</string>
<string>app-1-1085102361755-ios-74df231d8a2072e45dca3e</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ enum SettingsAction: String {
case toggleSecureTokenAPI = "Secure Token"
case toggleActiveApp = "Active App"
case toggleAccessGroup = "Current Access Group"
case toggleAPNSToken = "APNs Token"
case toggleAppCredential = "App Credential"
case setAuthLanugage = "Auth Language"
case useAppLanguage = "Use App Language"
case togglePhoneAppVerification = "Disable App Verification (Phone)"
Expand Down Expand Up @@ -79,6 +81,10 @@ class SettingsViewController: UIViewController, DataSourceProviderDelegate {
setAuthLanguage()
case .useAppLanguage:
auth.useAppLanguage()
case .toggleAPNSToken:
clearAPNSToken()
case .toggleAppCredential:
clearAppCredential()
case .togglePhoneAppVerification:
guard let settings = auth.settings else {
fatalError("Unset auth.settings")
Expand Down Expand Up @@ -118,6 +124,37 @@ class SettingsViewController: UIViewController, DataSourceProviderDelegate {
}
}

func clearAPNSToken() {
guard let token = AppManager.shared.auth().tokenManager.token else {
return
}

let tokenType = token.type == .prod ? "Production" : "Sandbox"
let message = "token: \(token.string)\ntype: \(tokenType)"

let prompt = UIAlertController(title: nil, message: "Clear APNs Token?",
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { action in
AppManager.shared.auth().tokenManager.token = nil
self.updateUI()
}
prompt.addAction(okAction)
present(prompt, animated: true)
}

func clearAppCredential() {
if let credential = AppManager.shared.auth().appCredentialManager.credential {
let prompt = UIAlertController(title: nil, message: "Clear APNs Token?",
preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .default) { action in
AppManager.shared.auth().appCredentialManager.clearCredential()
self.updateUI()
}
prompt.addAction(okAction)
present(prompt, animated: true)
}
}

private func setAuthLanguage() {
let prompt = UIAlertController(title: nil, message: "Enter Language Code For Auth:",
preferredStyle: .alert)
Expand Down Expand Up @@ -160,6 +197,24 @@ class SettingsViewController: UIViewController, DataSourceProviderDelegate {
options: .transitionCrossDissolve,
animations: { tableView.reloadData() })
}

func showPromptWithTitle(_ title: String, message: String, showCancelButton: Bool,
completion: @escaping (Bool, String?) -> Void) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
let userInput = alertController.textFields?.first?.text
completion(true, userInput)
}))

if showCancelButton {
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
completion(false, nil)
}))
}

alertController.addTextField(configurationHandler: nil)
}
}

// MARK: - Extending a `AuthSettings` to conform to `DataSourceProvidable`
Expand Down Expand Up @@ -187,21 +242,42 @@ extension AuthSettings: DataSourceProvidable {
return Section(headerDescription: "Keychain Access Groups", items: items)
}

func truncatedString(string: String, length: Int) -> String {
guard string.count > length else { return string }

let half = (length - 3) / 2
let startIndex = string.startIndex
let midIndex = string.index(startIndex, offsetBy: half) // Ensure correct mid index
let endIndex = string.index(startIndex, offsetBy: string.count - half)

return "\(string[startIndex ..< midIndex])...\(string[endIndex...])"
}

// TODO: Add ability to click and clear both of these fields.
private var phoneAuthSection: Section {
var tokenString = "No Token"
var credentialString = "No Credential"
if let token = AppManager.shared.auth().tokenManager.token {
let tokenType = token.type == .prod ? "Production" : "Sandbox"
tokenString = "token: \(token.string): type: \(tokenType)"
let items = [Item(title: APNSTokenString(), detailTitle: "APNs Token"),
Item(title: appCredentialString(), detailTitle: "App Credential")]
return Section(headerDescription: "Phone Auth", items: items)
}

func APNSTokenString() -> String {
guard let token = AppManager.shared.auth().tokenManager.token else {
return "No APNs token"
}

let truncatedToken = truncatedString(string: token.string, length: 19)
let tokenType = token.type == .prod ? "Production" : "Sandbox"
return "\(truncatedToken)(\(tokenType))"
}

func appCredentialString() -> String {
if let credential = AppManager.shared.auth().appCredentialManager.credential {
// TODO: Maybe use truncatedString like ObjC sample
credentialString = "\(credential.receipt)/\(credential.secret ?? "nil")"
let truncatedReceipt = truncatedString(string: credential.receipt, length: 13)
let truncatedSecret = truncatedString(string: credential.secret ?? "", length: 13)
return "\(truncatedReceipt)/\(truncatedSecret)"
} else {
return "No App Credential"
}
let items = [Item(title: tokenString, detailTitle: "APNs Token"),
Item(title: credentialString, detailTitle: "App Credential")]
return Section(headerDescription: "Phone Auth - TODO toggle off", items: items)
}

private var languageSection: Section {
Expand Down

0 comments on commit 8894b50

Please sign in to comment.