WWDC 2018:自動強密碼與驗證碼自動輸入

RocZhang_9673發表於2019-03-04

WWDC18 - Session 204 Automatic Strong Passwords and Security Code AutoFill 筆記

作者:我是 RocZhang,大四在讀,目前在流利說做 iOS 開發

個人作品:Adonis / Mr.Weather / YearTimer

關於我:https://www.roczhang.com/me/

在 iOS 12 中,Apple 將自動建議與使用強唯一密碼的功能帶入了 App 內,通過 QuickType bar 大幅簡化了使用者設定賬戶與登陸的繁瑣操作。本 session 介紹瞭如何優化應用如何適配密碼、安全碼和其他自動填充功能,帶給使用者更安全與無縫的體驗。

cover

Password AutoFill 自動密碼填充

在 iOS 11 中,Apple 引入了自動填充密碼。此功能可以讓使用者通過點選鍵盤上方的 QuickType bar 快速完成使用者名稱與密碼輸入過程。首先是對此功能的概要重述,重點包括:

  1. 設定 Associated domains 關聯域。iCloud Keychain Password Manager 中的密碼是基於 Web 上的 domain 域(如 apple.com)來儲存的。因此,就需要將 App 與 Web 上的域關聯起來。關於 Password AutoFill 的詳細功能,可見 WWDC 17 - Session 206 - Introducing Password AutoFill for Apps
  2. 標記好 Content types。在 iOS 11 中,UITextContentType 中新增了新型別:usernamepassword。只要給輸入框設定好正確的 textContentType 即可。如:
// For user name text field
let userTextField = UITextField()
userTextField.textContentType = .username

// For password text field
let passwordTextField = UITextField() 
passwordTextField.textContentType = .password
複製程式碼

改進

  1. 在 iOS 11.3 中,WKWebView 支援了自動填入密碼,如果登入介面使用 Web 技術實現會很有幫助。
  2. 在 iOS 12 中,從 App Store 中下載的其他密碼管理應用也可以提供資訊實現自動輸入功能。只要開發者適配了 iCloud Keychain Password Manager 的 AutoFill,其他第三方密碼管理應用也同樣能夠得到支援。如果你在開發密碼管理應用,可以瀏覽 WWDC 18 - Session 721 - Implementing AutoFill Credential Provider Extensions

Password Saving 密碼儲存

在 iOS 12 中,Apple 提供了在 App 內新賬戶登入時儲存密碼憑證的功能。從而使得使用者在所有裝置上都能通過 iCloud Keychain 登入你的 App 或網站。儲存密碼的工作原理是:

  1. 推斷登陸場景;
  2. 基於關聯 domain 檢查資質;
  3. 查詢使用者名稱和密碼欄位;
  4. 檢測登陸操作;
  5. 提示使用者儲存或更新密碼。

為確保相容此功能,我們需要檢查的事件有:

  1. 為相關的輸入框標記好 content type 內容型別;
  2. 當登入事件發生時,將使用者名稱與密碼輸入框從 view hierarchy 中移除。這樣 Autofill 便能夠檢測到登入事件正在進行。可以通過 dismiss 掉登入場景的 View Controller 實現;
  3. 確保值在上述移除工作完成之後再清除登入輸入框中的內容,這樣 Autofill 才能讀到資料並將其儲存;
  4. 檢查 Autofill 儲存的密碼關聯的是否為正確的 domain。可以通過儲存後在設定介面中檢視儲存結果,如不正確,可通過 Web credentials associated domain service 覆蓋其儲存的位置。
  5. 如果之前手動通過 SecAddSharedWebCredential() 儲存,現在可能不在需要使用它了.

總結關鍵點在於:

  1. 將 app 關聯 domain;
  2. 為輸入域做好標記;
  3. 確保登陸檢測。

Automatic Strong Passwords 自動強密碼

Automatic String Passwords 提供生成建議使用者名稱、密碼與儲存功能,通過幾次點選便可以完成註冊,將註冊過程變得更加容易與安全。

automaticStrongPasswords

Automatic Strong Passwords 的工作原理與上述的 Password Saving 工作原理大致相似:

  1. 推斷 View Controller 型別;
  2. 基於關聯的 domians 檢查資質;
  3. 檢測相關的登錄檔單元素,如使用者名稱輸入框與密碼輸入框;
  4. 提供建議使用者名稱;
  5. 鍵入強密碼;
  6. 使用者註冊後儲存。

對此功能的相容性檢查表也與上述的 Password Saving 類似。 為配合此功能,在 iOS 12 中,UITextContentType 新增了 .newPassword 型別,我們需要標記好自己 App 中的新密碼輸入框與密碼確認輸入框為 .newPassword。

對修改密碼錶單的注意事項:

  1. 使用者名稱與新密碼文字框應該在同一屏上;
  2. 使用者名稱文字框可以是隻讀的;
  3. 在註冊中的最佳實踐同樣適用於此。

預設生成密碼的格式

  1. 長度為20個字元;
  2. 包括大字母、小寫字幕、數字與連字元;
  3. 超過 71 位的熵;
  4. 設計上旨在與大多數服務相容。

自動生成的密碼例子:funrus-Hommez-kajzp7

當然,考慮到不同的後端規則,也可以自定義自動生成強密碼的格式。可以通過密碼規則語言來定義規則,如:

let newPasswordTextField = UITextField()
...
let rulesDescriptor = "allowed: upper, lower, digit; required: [$];" newPasswordTextField.passwordRules = UITextInputPasswordRules(descriptor: rulesDescriptor)
複製程式碼

還可以使用新的密碼規則驗證工具 - Password Rules Validation Tool

Security Code AutoFill 驗證碼自動填充

對於 iOS 12 之前使用者來說,收到簡訊驗證碼都需要人肉記憶,再手動輸入到文字框中。iOS 12 與 macOS Mojave 中終於帶來了自動輸入驗證碼功能。同樣在 UITextContentType 中新增了一種 .oneTimeCode 的型別。但由於依賴系統鍵盤 QuickType 輸入,所以對於哪些取代系統鍵盤使用自定義介面輸入驗證碼的場景無法使用。同時,在所以已支援的語言環境中都可用。驗證碼自動填充功能同樣能夠在 Safari Web 頁面中使用。非常棒的一點是如果使用者嘗試在 Mac 上通過 Safari 登入,iPhone 上收到的驗證碼資訊將會被自動安全的傳遞到 Mac 上,以實現在 Mac 上同樣能夠輕點一下自動填充驗證碼。

此次自動輸入的功能在 Safari 中同樣有效,具體屬性符對應如下:

Attribute iOS 12 (UITextContentType) Safari (input autocomplete="value")
UserName .username username
Existing Password .password current-password
New Password (for Automatic Strong Passwords) .newPassword new-password
One Time Code (for Security Code AutoFill) .oneTimeCode one-time-code

Federated authentication 聯合身份驗證

對於要支援使用第三方服務(如社交媒體賬號)登入的情況,iOS 12 中引入了新的 API: ASWebAuthenticationSession。聯合身份驗證過程如下圖所示:

federatedAuthentication
使用 ASWebAuthenticationSession 的好處在於:更快的登陸流程、支援密碼自動輸入與驗證碼自動輸入以及簡單明瞭的基於 block 形式的 API。具體用法如下所示:

import AuthenticationServices
guard let oauthURL = URL(string: "https://www.example.com/oauth/...") else {
return
}
self.authenticationSession = ASWebAuthenticationSession(url: oauthURL, callbackURLScheme:
nil) { (callbackURL, error) in
guard error == nil, let callbackURL = callbackURL else {
}
// Process error.
return
// Process token.
}
self.authenticationSession.start()
複製程式碼

New password management features 密碼管理新功能

iCloud Keychain Password Manager 中帶了了一些與密碼管理有關的新功能。如:通過詢問 Siri 密碼自動跳轉到密碼檢視頁以快速檢視、密碼內容支援通過 AirDrop 分享給他人、iOS 12 與 macOS Mojave 中的密碼檢視列表介面也經過了重新的設計、以及對在多個網站中使用相同密碼的情況給予使用者警告、tvOS 應用可以通過附近的 iOS 裝置完成自動密碼輸入。

Summary 總結

在 iOS 12 與 macOS Mojave 中提供的這些密碼相關功能非常強大,儘管許多功能可能能夠自動適配工作,我們仍需要測試自己的 app 來保證良好相容。 同時,聯想到近兩天再次頻繁爆出的國內大網站被脫褲的訊息,開發者對安全問題都應該更加重視。適配 Automatic Strong Passwords 相比其他 feature 來說適配工作量並不會非常多,但能夠非常明顯的提升使用者操作的連貫體驗與安全性。

相關文章