You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.4 KiB

5 years ago
  1. //
  2. // KeyChain.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon Devik Kim on 08/04/2019.
  6. // Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import KeychainAccess
  10. final class KeyChain {
  11. enum KeyType: String {
  12. case password
  13. case id
  14. case biometricAuth
  15. case login
  16. case temporaryID
  17. case temporaryPW
  18. }
  19. static let shared = KeyChain()
  20. private var keychain: Keychain
  21. private init() {
  22. self.keychain = Keychain(service: "com.gme.gmeremit")
  23. // There is no userId, it's logout or remove app all and reinstall app.
  24. if GMEDB.shared.user.string(.userId) == nil {
  25. try? keychain.removeAll()
  26. }
  27. }
  28. func save(data: String, key: KeyType) {
  29. do {
  30. try self.keychain.set(data, key: key.rawValue)
  31. } catch let error {
  32. print("keychain save error: \(error.localizedDescription)")
  33. }
  34. }
  35. func get(key: KeyType) -> String? {
  36. guard let pw = ((try? self.keychain.get(key.rawValue)) as String??) else {
  37. return nil
  38. }
  39. return pw
  40. }
  41. func remove(key: KeyType) {
  42. do {
  43. try self.keychain.remove(key.rawValue)
  44. } catch let error {
  45. print("keychain remove error: \(error.localizedDescription)")
  46. }
  47. }
  48. func removeAll() {
  49. do {
  50. try self.keychain.removeAll()
  51. } catch let error {
  52. print("keychain remove error: \(error.localizedDescription)")
  53. }
  54. }
  55. }