|
1 | 1 | #if os(Windows)
|
2 |
| -import Foundation |
3 |
| -import WinSDK |
| 2 | + import Foundation |
| 3 | + import WinSDK |
4 | 4 |
|
5 |
| -enum WinCredLocalStorageError: Error { |
6 |
| - case windows(UInt32) |
7 |
| - case other(Int) |
8 |
| -} |
| 5 | + enum WinCredLocalStorageError: Error { |
| 6 | + case windows(UInt32) |
| 7 | + case other(Int) |
| 8 | + } |
9 | 9 |
|
10 |
| -public struct WinCredLocalStorage: AuthLocalStorage { |
11 |
| - private let service: String |
| 10 | + public struct WinCredLocalStorage: AuthLocalStorage { |
| 11 | + private let service: String |
12 | 12 |
|
13 |
| - private let credentialType: DWORD |
14 |
| - private let credentialPersistence: DWORD |
| 13 | + private let credentialType: DWORD |
| 14 | + private let credentialPersistence: DWORD |
15 | 15 |
|
16 |
| - public init(service: String) { |
17 |
| - self.service = service |
18 |
| - credentialType = DWORD(CRED_TYPE_GENERIC) |
19 |
| - credentialPersistence = DWORD(CRED_PERSIST_LOCAL_MACHINE) |
20 |
| - } |
| 16 | + public init(service: String) { |
| 17 | + self.service = service |
| 18 | + credentialType = DWORD(CRED_TYPE_GENERIC) |
| 19 | + credentialPersistence = DWORD(CRED_PERSIST_LOCAL_MACHINE) |
| 20 | + } |
21 | 21 |
|
22 |
| - public func store(key: String, value: Data) throws { |
23 |
| - var valueData = value |
| 22 | + public func store(key: String, value: Data) throws { |
| 23 | + var valueData = value |
24 | 24 |
|
25 |
| - var credential: CREDENTIALW = .init() |
| 25 | + var credential: CREDENTIALW = .init() |
26 | 26 |
|
27 |
| - credential.Type = credentialType |
28 |
| - credential.Persist = credentialPersistence |
29 |
| - "\(service)\\\(key)".withCString(encodedAs: UTF16.self, { keyName in |
30 |
| - credential.TargetName = UnsafeMutablePointer(mutating: keyName) |
31 |
| - }) |
| 27 | + credential.Type = credentialType |
| 28 | + credential.Persist = credentialPersistence |
| 29 | + "\(service)\\\(key)".withCString(encodedAs: UTF16.self) { keyName in |
| 30 | + credential.TargetName = UnsafeMutablePointer(mutating: keyName) |
| 31 | + } |
32 | 32 |
|
33 |
| - withUnsafeMutableBytes(of: &valueData, { data in |
34 |
| - credential.CredentialBlobSize = DWORD(data.count) |
35 |
| - credential.CredentialBlob = data.baseAddress!.assumingMemoryBound(to: UInt8.self) |
36 |
| - }) |
| 33 | + withUnsafeMutableBytes(of: &valueData) { data in |
| 34 | + credential.CredentialBlobSize = DWORD(data.count) |
| 35 | + credential.CredentialBlob = data.baseAddress!.assumingMemoryBound(to: UInt8.self) |
| 36 | + } |
37 | 37 |
|
38 |
| - if !CredWriteW(&credential, 0) { |
39 |
| - let lastError = GetLastError() |
40 |
| - debugPrint("Unable to save password to credential vault, got error code \(lastError)") |
| 38 | + if !CredWriteW(&credential, 0) { |
| 39 | + let lastError = GetLastError() |
| 40 | + debugPrint("Unable to save password to credential vault, got error code \(lastError)") |
41 | 41 |
|
42 |
| - throw WinCredLocalStorageError.windows(lastError) |
| 42 | + throw WinCredLocalStorageError.windows(lastError) |
| 43 | + } |
43 | 44 | }
|
44 |
| - } |
45 | 45 |
|
46 |
| - public func retrieve(key: String) throws -> Data? { |
47 |
| - var credential: PCREDENTIALW? |
| 46 | + public func retrieve(key: String) throws -> Data? { |
| 47 | + var credential: PCREDENTIALW? |
48 | 48 |
|
49 |
| - let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self, { $0 }) |
| 49 | + let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self) { $0 } |
50 | 50 |
|
51 |
| - if !CredReadW(targetName, credentialType, 0, &credential) { |
52 |
| - let lastError = GetLastError() |
53 |
| - debugPrint("Unable to find entry for key in credential vault, got error code \(lastError)") |
| 51 | + if !CredReadW(targetName, credentialType, 0, &credential) { |
| 52 | + let lastError = GetLastError() |
| 53 | + debugPrint("Unable to find entry for key in credential vault, got error code \(lastError)") |
54 | 54 |
|
55 |
| - throw WinCredLocalStorageError.windows(lastError) |
56 |
| - } |
| 55 | + throw WinCredLocalStorageError.windows(lastError) |
| 56 | + } |
57 | 57 |
|
58 |
| - guard let foundCredential = credential, let blob = foundCredential.pointee.CredentialBlob else { |
59 |
| - throw WinCredLocalStorageError.other(-1) |
60 |
| - } |
| 58 | + guard let foundCredential = credential, |
| 59 | + let blob = foundCredential.pointee.CredentialBlob |
| 60 | + else { |
| 61 | + throw WinCredLocalStorageError.other(-1) |
| 62 | + } |
61 | 63 |
|
62 |
| - let blobSize = Int(foundCredential.pointee.CredentialBlobSize) |
63 |
| - let pointer = blob.withMemoryRebound(to: UInt8.self, capacity: blobSize, { $0 }) |
64 |
| - let data = Data(bytes: pointer, count: blobSize) |
| 64 | + let blobSize = Int(foundCredential.pointee.CredentialBlobSize) |
| 65 | + let pointer = blob.withMemoryRebound(to: UInt8.self, capacity: blobSize) { $0 } |
| 66 | + let data = Data(bytes: pointer, count: blobSize) |
65 | 67 |
|
66 |
| - CredFree(foundCredential) |
| 68 | + CredFree(foundCredential) |
67 | 69 |
|
68 |
| - return data |
69 |
| - } |
| 70 | + return data |
| 71 | + } |
70 | 72 |
|
71 |
| - public func remove(key: String) throws { |
72 |
| - let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self, { $0 }) |
| 73 | + public func remove(key: String) throws { |
| 74 | + let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self) { $0 } |
73 | 75 |
|
74 |
| - if !CredDeleteW(targetName, credentialType, 0) { |
75 |
| - let lastError = GetLastError() |
76 |
| - debugPrint("Unable to remove key from credential vault, got error code \(lastError)") |
| 76 | + if !CredDeleteW(targetName, credentialType, 0) { |
| 77 | + let lastError = GetLastError() |
| 78 | + debugPrint("Unable to remove key from credential vault, got error code \(lastError)") |
77 | 79 |
|
78 |
| - throw WinCredLocalStorageError.windows(lastError) |
| 80 | + throw WinCredLocalStorageError.windows(lastError) |
| 81 | + } |
79 | 82 | }
|
80 | 83 | }
|
81 |
| -} |
82 | 84 | #endif
|
0 commit comments