summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Keychain/KeychainMatchLimit.swift
blob: f86b010532dd14cd859180e47fdab1587a552aa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
//  KeychainMatchLimit.swift
//  MullvadVPN
//
//  Created by pronebird on 24/04/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import Foundation
import Security

extension Keychain {
    enum MatchLimit: RawRepresentable, CaseIterable, KeychainAttributeDecodable, KeychainAttributeEncodable {
        case one
        case all

        var rawValue: CFString {
            switch self {
            case .one:
                return kSecMatchLimitOne
            case .all:
                return kSecMatchLimitAll
            }
        }

        init?(rawValue: CFString) {
            let maybeCase = Self.allCases.first { $0.rawValue == rawValue }

            if let maybeCase = maybeCase {
                self = maybeCase
            } else {
                return nil
            }
        }

        init?(attributes: [CFString : Any]) {
            if let rawValue = attributes[kSecMatchLimit] as? String {
                self.init(rawValue: rawValue as CFString)
            } else {
                return nil
            }
        }

        func updateKeychainAttributes(in attributes: inout [CFString : Any]) {
            attributes[kSecMatchLimit] = rawValue
        }
    }
}