LCOV - code coverage report
Current view: top level - SwiftySimpleKeychain/Extensions/KeychainOps - SwiftySimpleKeychain+Delete.swift (source / functions) Hit Total Coverage
Test: lcov.info Lines: 34 35 97.1 %
Date: 2022-03-29 11:59:53 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //
       2             : // SwiftySimpleKeychain+Delete.swift
       3             : // SwiftySimpleKeychain
       4             : //
       5             : // Copyright (c) 2022 Ezequiel (Kimi) Aceto (https://eaceto.dev). Based on Auth0's SimpleKeychain
       6             : //
       7             : // Permission is hereby granted, free of charge, to any person obtaining a copy
       8             : // of this software and associated documentation files (the "Software"), to deal
       9             : // in the Software without restriction, including without limitation the rights
      10             : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      11             : // copies of the Software, and to permit persons to whom the Software is
      12             : // furnished to do so, subject to the following conditions:
      13             : //
      14             : // The above copyright notice and this permission notice shall be included in
      15             : // all copies or substantial portions of the Software.
      16             : //
      17             : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      18             : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      19             : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      20             : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      21             : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      22             : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      23             : // THE SOFTWARE.
      24             : 
      25             : #if canImport(Foundation)
      26             : import Foundation
      27             : #endif
      28             : 
      29             : public extension SwiftySimpleKeychain {
      30             :     
      31             :     /**
      32             :      * Deletes an entry in the Keychain with the given **key**
      33             :      *
      34             :      * - Parameter key: Identifier of the entry
      35             :      * - Returns **true** if the key exists and it could be deleted
      36             :      */
      37           2 :     func deleteEntry(for key: String) -> Bool {
      38           2 :         if key.isEmpty { return false }
      39           2 :         let deleteQuery = queryFind(by: key)
      40           2 :         let status = SecItemDelete(deleteQuery as CFDictionary)
      41           2 :         return status == errSecSuccess
      42           2 :     }
      43             : 
      44             :     /**
      45             :      * Deletes all entries in the Keychain
      46             :      * Aborts if there is an error deleting one item. 
      47             :      */
      48           1 :     func clearAll() {
      49           1 :         #if os(iOS)
      50           1 :         let query = queryFindAll()
      51           1 :         var result: CFTypeRef?
      52           1 :         let status = SecItemCopyMatching(query as CFDictionary, &result)
      53           1 :         if status == errSecSuccess || status == errSecItemNotFound {
      54           1 :             guard let result = result, let items = result as? [[String: Any]] else {
      55           1 :                 return
      56           1 :             }
      57           1 :             for item in items {
      58           1 :                 var queryDelete = item
      59           1 :                 queryDelete[kSecClass as String] = kSecClassGenericPassword
      60           1 :                 let status = SecItemDelete(queryDelete as CFDictionary)
      61           1 :                 if status != errSecSuccess {
      62           1 :                     debugPrint("Could not delete item: \(item)")
      63           1 :                     return
      64           1 :                 }
      65           1 :             }
      66           1 :         }
      67           1 :         #else
      68           1 :         var queryDelete = baseQuery()
      69           1 :         queryDelete[kSecClass as String] = kSecClassGenericPassword
      70           1 :         queryDelete[kSecMatchLimit as String] = kSecMatchLimitAll
      71           1 :         let status = SecItemDelete(queryDelete as CFDictionary)
      72           1 :         if status != errSecSuccess {
      73           0 :             debugPrint("Could not delete: \(queryDelete)")
      74           1 :         }
      75           1 :         #endif
      76           1 :     }
      77             : }

Generated by: LCOV version 1.15