APP安全机制(二十) —— 基于SwiftUI App的钥匙串服务和生物识别(二)

版本记录

版本号 时间
V1.0 2020.09.07 星期一

前言

在这个信息爆炸的年代,特别是一些敏感的行业,比如金融业和银行卡相关等等,这都对app的安全机制有更高的需求,很多大公司都有安全 部门,用于检测自己产品的安全性,但是及时是这样,安全问题仍然被不断曝出,接下来几篇我们主要说一下app的安全机制。感兴趣的看我上面几篇。
1. APP安全机制(一)—— 几种和安全性有关的情况
2. APP安全机制(二)—— 使用Reveal查看任意APP的UI
3. APP安全机制(三)—— Base64加密
4. APP安全机制(四)—— MD5加密
5. APP安全机制(五)—— 对称加密
6. APP安全机制(六)—— 非对称加密
7. APP安全机制(七)—— SHA加密
8. APP安全机制(八)—— 偏好设置的加密存储
9. APP安全机制(九)—— 基本iOS安全之钥匙链和哈希(一)
10. APP安全机制(十)—— 基本iOS安全之钥匙链和哈希(二)
11. APP安全机制(十一)—— 密码工具:提高用户安全性和体验(一)
12. APP安全机制(十二)—— 密码工具:提高用户安全性和体验(二)
13. APP安全机制(十三)—— 密码工具:提高用户安全性和体验(三)
14. APP安全机制(十四) —— Keychain Services API使用简单示例(一)
15. APP安全机制(十五) —— Keychain Services API使用简单示例(二)
16. APP安全机制(十六) —— Keychain Services API使用简单示例(三)
17. APP安全机制(十七) —— 阻止使用SSL Pinning 和 Alamofire的中间人攻击(一)
18. APP安全机制(十八) —— 阻止使用SSL Pinning 和 Alamofire的中间人攻击(二)
19. APP安全机制(十九) —— 基于SwiftUI App的钥匙串服务和生物识别(一)

开始

1. Swift

首先看下工程组织结构

下面就是源码了

1. TextEditor.swift
import SwiftUI

struct TextEditor: UIViewRepresentable {
  @Binding var text: String

  func makeCoordinator() -> Coordinator {
    Coordinator(self)
  }

  func makeUIView(context: Context) -> UITextView {
    let textView = UITextView()
    textView.delegate = context.coordinator

    textView.font = UIFont.systemFont(ofSize: UIFont.systemFontSize)
    textView.isScrollEnabled = true
    textView.isEditable = true
    textView.isUserInteractionEnabled = true
    textView.backgroundColor = UIColor.white

    return textView
  }

  func updateUIView(_ uiView: UITextView, context: Context) {
    uiView.text = text
  }

  class Coordinator: NSObject, UITextViewDelegate {
    var parent: TextEditor

    init(_ textView: TextEditor) {
      self.parent = textView
    }

    func textView(
      _ textView: UITextView,
      shouldChangeTextIn range: NSRange,
      replacementText text: String
    ) -> Bool {
      return true
    }

    func textViewDidChange(_ textView: UITextView) {
      self.parent.text = textView.text
    }
  }
}

struct TextEditor_Previews: PreviewProvider {
  static var previews: some View {
    TextEditor(text: .constant("This is some text."))
  }
}
2. ContentView.swift
import SwiftUI

func randomText(length: Int) -> String {
  let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ       abcdefghijklmnopqrstuvwxyz      "
  return String((0..<length).map { _ in letters.randomElement() ?? " " })
}

struct ContentView: View {
  @ObservedObject var noteData: NoteData
  @State private var noteLocked = true
  @State private var fillerText = randomText(length: 250)
  @State private var setPasswordModal = false

  var body: some View {
    VStack(alignment: .leading) {
      Text("RW Quick Note")
        .foregroundColor(Color("rw-green"))
        .font(.largeTitle)
      ToolbarView(noteLocked: $noteLocked, noteData: noteData, setPasswordModal: $setPasswordModal)
        .onAppear {
          if self.noteData.isPasswordBlank {
            self.setPasswordModal = true
          }
        }
      Group {
        if noteLocked {
          TextEditor(text: $fillerText)
            .disabled(true)
            .blur(radius: 5.0)
        } else {
          TextEditor(text: $noteData.noteText)
        }
      }
      .border(Color.gray)
    }
    .padding()
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView(noteData: NoteData())
  }
}
3. SetPasswordView.swift
import SwiftUI

struct SetPasswordView: View {
  var title: String
  var subTitle: String
  @State var password1 = ""
  @State var password2 = ""
  @Binding var noteLocked: Bool
  @Binding var showModal: Bool
  @ObservedObject var noteData: NoteData

  var passwordValid: Bool {
    passwordsMatch && !password1.isEmpty
  }

  var passwordsMatch: Bool {
    password1 == password2
  }

  var body: some View {
    VStack(alignment: .leading) {
      Text(title)
        .font(.title)
      Text(subTitle)
        .font(.subheadline)
      SecureField("Password", text: $password1)
        .modifier(PasswordField(error: !passwordsMatch))
      SecureField("Verify Password", text: $password2)
        .modifier(PasswordField(error: !passwordsMatch))
      HStack {
        if password1 != password2 {
          Text("Passwords Do Not Match")
            .padding(.leading)
            .foregroundColor(.red)
        }
        Spacer()
        Button("Set Password") {
          if self.passwordValid {
            self.noteData.updateStoredPassword(self.password1)
            self.noteLocked = false
            self.showModal = false
          }
        }.disabled(!passwordValid)
        .padding()
      }
    }.padding()
  }
}

struct SetPasswordView_Previews: PreviewProvider {
  static var previews: some View {
    SetPasswordView(
      title: "Test",
      subTitle: "This is a test",
      noteLocked: .constant(true),
      showModal: .constant(true),
      noteData: NoteData()
    )
  }
}
4. ToolbarView.swift
import SwiftUI
import LocalAuthentication

func getBiometricType() -> String {
  let context = LAContext()

  _ = context.canEvaluatePolicy(
    .deviceOwnerAuthenticationWithBiometrics,
    error: nil)
  switch context.biometryType {
  case .faceID:
    return "faceid"
  case .touchID:
    // In iOS 14 and later, you can use "touchid" here
    return "lock"
  case .none:
    return "lock"
  @unknown default:
    return "lock"
  }
}

// swiftlint:disable multiple_closures_with_trailing_closure
struct ToolbarView: View {
  @Binding var noteLocked: Bool
  @ObservedObject var noteData: NoteData
  @Binding var setPasswordModal: Bool
  @State private var showUnlockModal: Bool = false
  @State private var changePasswordModal: Bool = false

  func tryBiometricAuthentication() {
    // 1
    let context = LAContext()
    var error: NSError?

    // 2
    if context.canEvaluatePolicy(
      .deviceOwnerAuthenticationWithBiometrics,
      error: &error) {
      // 3
      let reason = "Authenticate to unlock your note."
      context.evaluatePolicy(
        .deviceOwnerAuthentication,
        localizedReason: reason) { authenticated, error in
        // 4
        DispatchQueue.main.async {
          if authenticated {
            // 5
            self.noteLocked = false
          } else {
            // 6
            if let errorString = error?.localizedDescription {
              print("Error in biometric policy evaluation: \(errorString)")
            }
            self.showUnlockModal = true
          }
        }
      }
    } else {
      // 7
      if let errorString = error?.localizedDescription {
        print("Error in biometric policy evaluation: \(errorString)")
      }
      showUnlockModal = true
    }
  }

  var body: some View {
    HStack {
      #if DEBUG
      Button(
        action: {
          print("App reset.")
          self.noteData.noteText = ""
          self.noteData.updateStoredPassword("")
        }, label: {
          Image(systemName: "trash")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .frame(width: 25.0, height: 25.0)
        })
      #endif

      Color.clear
        .sheet(isPresented: $setPasswordModal) {
          SetPasswordView(
            title: "Set Note Password",
            subTitle: "Enter a password to protect this note.",
            noteLocked: self.$noteLocked,
            showModal: self.$setPasswordModal,
            noteData: self.noteData
          )
        }

      Spacer()

      Button(
        action: {
          self.changePasswordModal = true
        }) {
        Image(systemName: "arrow.right.arrow.left")
          .resizable()
          .aspectRatio(contentMode: .fit)
          .frame(width: 25.0, height: 25.0)
      }
      .disabled(noteLocked || noteData.isPasswordBlank)
      .sheet(isPresented: $changePasswordModal) {
        SetPasswordView(
          title: "Change Password",
          subTitle: "Enter new password",
          noteLocked: self.$noteLocked,
          showModal: self.$changePasswordModal,
          noteData: self.noteData)
      }

      Button(
        action: {
          if self.noteLocked {
            // Biometric Authentication Point
            self.tryBiometricAuthentication()
          } else {
            self.noteLocked = true
          }
        }) {
        // Lock Icon
        Image(systemName: noteLocked ? getBiometricType() : "lock.open")
          .resizable()
          .aspectRatio(contentMode: .fit)
          .frame(width: 25.0, height: 25.0)
      }
      .sheet(isPresented: $showUnlockModal) {
        if self.noteData.isPasswordBlank {
          SetPasswordView(
            title: "Enter Password",
            subTitle: "Enter a password to protect your notes",
            noteLocked: self.$noteLocked,
            showModal: self.$changePasswordModal,
            noteData: self.noteData)
        } else {
          UnlockView(noteLocked: self.$noteLocked, showModal: self.$showUnlockModal, noteData: self.noteData)
        }
      }
    }
    .frame(height: 64)
  }
}

struct ToolbarView_Previews: PreviewProvider {
  static var previews: some View {
    ToolbarView(noteLocked: .constant(true), noteData: NoteData(), setPasswordModal: .constant(false))
  }
}
5. UnlockView.swift
import SwiftUI

// swiftlint:disable multiple_closures_with_trailing_closure
struct UnlockView: View {
  @State var password = ""
  @State var passwordError = false
  @State var showPassword = false
  @Binding var noteLocked: Bool
  @Binding var showModal: Bool
  @ObservedObject var noteData: NoteData

  var body: some View {
    VStack(alignment: .leading) {
      Text("Enter Password")
        .font(.title)
      Text("Enter password to unlock note")
        .font(.subheadline)
      HStack {
        Group {
          if showPassword {
            TextField("Password", text: $password)
          } else {
            SecureField("Password", text: $password)
          }
        }
        Button(
          action: {
            self.showPassword.toggle()
          }) {
          if showPassword {
            Image(systemName: "eye.slash")
          } else {
            Image(systemName: "eye")
              .padding(.trailing, 5.0)
          }
        }
      }.modifier(PasswordField(error: passwordError))
      HStack {
        if passwordError {
          Text("Incorrect Password")
            .padding(.leading)
            .foregroundColor(.red)
        }
        Spacer()
        Button("Unlock") {
          if !self.noteData.validatePassword(self.password) {
            self.passwordError = true
          } else {
            self.noteLocked = false
            self.showModal = false
          }
        }.padding()
      }
    }.padding()
  }
}

struct ToggleLock_Previews: PreviewProvider {
  static var previews: some View {
    UnlockView(noteLocked: .constant(false), showModal: .constant(true), noteData: NoteData())
  }
}
6. ViewModifiers.swift
import SwiftUI

struct PasswordField: ViewModifier {
  var error: Bool

  func body(content: Content) -> some View {
    content
      .textFieldStyle(RoundedBorderTextFieldStyle())
      .border(error ? Color.red : Color.gray)
  }
}
7. NoteData.swift
import SwiftUI

class NoteData: ObservableObject {
  let textKey = "StoredText"

  @Published var noteText: String {
    didSet {
      UserDefaults.standard.set(noteText, forKey: textKey)
    }
  }

  var isPasswordBlank: Bool {
    getStoredPassword() == ""
  }

  func getStoredPassword() -> String {
    let kcw = KeychainWrapper()
    if let password = try? kcw.getGenericPasswordFor(
      account: "RWQuickNote",
      service: "unlockPassword") {
      return password
    }

    return ""
  }

  func updateStoredPassword(_ password: String) {
    let kcw = KeychainWrapper()
    do {
      try kcw.storeGenericPasswordFor(
        account: "RWQuickNote",
        service: "unlockPassword",
        password: password)
    } catch let error as KeychainWrapperError {
      print("Exception setting password: \(error.message ?? "no message")")
    } catch {
      print("An error occurred setting the password.")
    }
  }

  func validatePassword(_ password: String) -> Bool {
    let currentPassword = getStoredPassword()
    return password == currentPassword
  }

  func changePassword(currentPassword: String, newPassword: String) -> Bool {
    guard validatePassword(currentPassword) == true else { return false }
    updateStoredPassword(newPassword)
    return true
  }

  init() {
    noteText = UserDefaults.standard.string(forKey: textKey) ?? ""
  }
}
8. KeychainServices.swift
import Foundation

struct KeychainWrapperError: Error {
  var message: String?
  var type: KeychainErrorType

  enum KeychainErrorType {
    case badData
    case servicesError
    case itemNotFound
    case unableToConvertToString
  }

  init(status: OSStatus, type: KeychainErrorType) {
    self.type = type
    if let errorMessage = SecCopyErrorMessageString(status, nil) {
      self.message = String(errorMessage)
    } else {
      self.message = "Status Code: \(status)"
    }
  }

  init(type: KeychainErrorType) {
    self.type = type
  }

  init(message: String, type: KeychainErrorType) {
    self.message = message
    self.type = type
  }
}

class KeychainWrapper {
  func storeGenericPasswordFor(
    account: String,
    service: String,
    password: String
  ) throws {
    if password.isEmpty {
      try deleteGenericPasswordFor(account: account, service: service)
      return
    }
    guard let passwordData = password.data(using: .utf8) else {
      print("Error converting value to data.")
      throw KeychainWrapperError(type: .badData)
    }

    // 1
    let query: [String: Any] = [
      // 2
      kSecClass as String: kSecClassGenericPassword,
      // 3
      kSecAttrAccount as String: account,
      // 4
      kSecAttrService as String: service,
      // 5
      kSecValueData as String: passwordData
    ]

    // 1
    let status = SecItemAdd(query as CFDictionary, nil)
    switch status {
    // 2
    case errSecSuccess:
      break
    case errSecDuplicateItem:
      try updateGenericPasswordFor(
        account: account,
        service: service,
        password: password)
    // 3
    default:
      throw KeychainWrapperError(status: status, type: .servicesError)
    }
  }

  func getGenericPasswordFor(account: String, service: String) throws -> String {
    let query: [String: Any] = [
      // 1
      kSecClass as String: kSecClassGenericPassword,
      kSecAttrAccount as String: account,
      kSecAttrService as String: service,
      // 2
      kSecMatchLimit as String: kSecMatchLimitOne,
      kSecReturnAttributes as String: true,
      // 3
      kSecReturnData as String: true
    ]

    var item: CFTypeRef?
    let status = SecItemCopyMatching(query as CFDictionary, &item)
    guard status != errSecItemNotFound else {
      throw KeychainWrapperError(type: .itemNotFound)
    }
    guard status == errSecSuccess else {
      throw KeychainWrapperError(status: status, type: .servicesError)
    }

    guard let existingItem = item as? [String: Any],
      // 2
      let valueData = existingItem[kSecValueData as String] as? Data,
      // 3
      let value = String(data: valueData, encoding: .utf8)
      else {
        // 4
        throw KeychainWrapperError(type: .unableToConvertToString)
    }

    //5
    return value
  }

  func updateGenericPasswordFor(
    account: String,
    service: String,
    password: String
  ) throws {
    guard let passwordData = password.data(using: .utf8) else {
      print("Error converting value to data.")
      return
    }
    // 1
    let query: [String: Any] = [
      kSecClass as String: kSecClassGenericPassword,
      kSecAttrAccount as String: account,
      kSecAttrService as String: service
    ]

    // 2
    let attributes: [String: Any] = [
      kSecValueData as String: passwordData
    ]

    // 3
    let status = SecItemUpdate(query as CFDictionary, attributes as CFDictionary)
    guard status != errSecItemNotFound else {
      throw KeychainWrapperError(message: "Matching Item Not Found", type: .itemNotFound)
    }
    guard status == errSecSuccess else {
      throw KeychainWrapperError(status: status, type: .servicesError)
    }
  }

  func deleteGenericPasswordFor(account: String, service: String) throws {
    // 1
    let query: [String: Any] = [
      kSecClass as String: kSecClassGenericPassword,
      kSecAttrAccount as String: account,
      kSecAttrService as String: service
    ]

    // 2
    let status = SecItemDelete(query as CFDictionary)
    guard status == errSecSuccess || status == errSecItemNotFound else {
      throw KeychainWrapperError(status: status, type: .servicesError)
    }
  }
}

后记

本篇主要讲述了SwiftUI的钥匙串服务和生物识别,感兴趣的给个赞或者关注~~~

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,142评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,298评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,068评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,081评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,099评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,071评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,990评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,832评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,274评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,488评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,649评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,378评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,979评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,625评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,643评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,545评论 2 352