-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
66 lines (54 loc) · 2 KB
/
main.swift
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
// main.swift
// KeyExplorer
//
// Created by Derik Ramirez on 7/2/19.
// Copyright © 2019 Derik Ramirez. All rights reserved.
//
import Security
import Foundation
struct User {
var username: String
var password: String
}
enum KeychainError: Error {
case noPassword
case unexpectedPasswordData
case unexpectedItemData
case unhandledError(status: OSStatus)
}
let server = "https://rderik.com"
let user = User(username: "rderik", password: "pass123")
var query: [String: Any] = [kSecClass as String: kSecClassInternetPassword,
kSecAttrServer as String: server,
kSecAttrAccount as String: user.username,
kSecValueData as String: user.password.data(using: String.Encoding.utf8)!]
puts("Este es el query:\(query)")
var status = SecItemAdd(query as CFDictionary, nil)
if status != errSecSuccess {
let errmsg = SecCopyErrorMessageString(status, nil)
print("Veamos: \(errmsg)")
}
print("Succesful save")
// Get the value from keychain
query = [kSecClass as String: kSecClassInternetPassword,
kSecAttrServer as String: server,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecReturnAttributes as String: true,
kSecReturnData as String: true]
var item: CFTypeRef?
status = SecItemCopyMatching(query as CFDictionary, &item)
guard status != errSecItemNotFound else { throw KeychainError.noPassword }
guard status == errSecSuccess else {
let errmsg = SecCopyErrorMessageString(status, nil)
print("Veamos: \(errmsg ?? "" as CFString)")
throw KeychainError.unhandledError(status: status)
}
guard let existingItem = item as? [String : Any],
let passwordData = existingItem[kSecValueData as String] as? Data,
let password = String(data: passwordData, encoding: String.Encoding.utf8),
let account = existingItem[kSecAttrAccount as String] as? String
else {
throw KeychainError.unexpectedPasswordData
}
print("username: \(account), password: \(password)")