summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Keychain/KeychainClass.swift
blob: 86e13f2bd663dcbe66caa7c8101e0016d646c7e4 (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
49
50
//
//  KeychainClass.swift
//  MullvadVPN
//
//  Created by pronebird on 24/04/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import Foundation
import Security

extension Keychain {

    enum KeychainClass: RawRepresentable, CaseIterable, KeychainAttributeDecodable, KeychainAttributeEncodable {
        case genericPassword
        case internetPassword

        var rawValue: CFString {
            switch self {
            case .genericPassword:
                return kSecClassGenericPassword
            case .internetPassword:
                return kSecClassInternetPassword
            }
        }

        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[kSecClass] as? String {
                self.init(rawValue: rawValue as CFString)
            } else {
                return nil
            }
        }

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

}