-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
303 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
import Foundation | ||
|
||
// Borrowed from the Vapor project, https://github.com/vapor/vapor/blob/main/Sources/Vapor/Utilities/Array%2BRandom.swift#L14 | ||
// Borrowed from the Vapor project, | ||
// https://github.com/vapor/vapor/blob/main/Sources/Vapor/Utilities/Array%2BRandom.swift#L14 | ||
extension FixedWidthInteger { | ||
internal static func random() -> Self { | ||
return Self.random(in: .min ... .max) | ||
} | ||
static func random() -> Self { | ||
random(in: .min ... .max) | ||
} | ||
|
||
internal static func random<T>(using generator: inout T) -> Self | ||
where T : RandomNumberGenerator | ||
{ | ||
return Self.random(in: .min ... .max, using: &generator) | ||
} | ||
static func random(using generator: inout some RandomNumberGenerator) -> Self { | ||
random(in: .min ... .max, using: &generator) | ||
} | ||
} | ||
|
||
extension Array where Element: FixedWidthInteger { | ||
internal static func random(count: Int) -> [Element] { | ||
var array: [Element] = .init(repeating: 0, count: count) | ||
(0..<count).forEach { array[$0] = Element.random() } | ||
return array | ||
} | ||
static func random(count: Int) -> [Element] { | ||
var array: [Element] = .init(repeating: 0, count: count) | ||
(0 ..< count).forEach { array[$0] = Element.random() } | ||
return array | ||
} | ||
|
||
internal static func random<T>(count: Int, using generator: inout T) -> [Element] | ||
where T: RandomNumberGenerator | ||
{ | ||
var array: [Element] = .init(repeating: 0, count: count) | ||
(0..<count).forEach { array[$0] = Element.random(using: &generator) } | ||
return array | ||
} | ||
static func random(count: Int, using generator: inout some RandomNumberGenerator) -> [Element] { | ||
var array: [Element] = .init(repeating: 0, count: count) | ||
(0 ..< count).forEach { array[$0] = Element.random(using: &generator) } | ||
return array | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
#if !os(Windows) && !os(Linux) | ||
import Foundation | ||
@preconcurrency import KeychainAccess | ||
import Foundation | ||
@preconcurrency import KeychainAccess | ||
|
||
public struct KeychainLocalStorage: AuthLocalStorage { | ||
private let keychain: Keychain | ||
public struct KeychainLocalStorage: AuthLocalStorage { | ||
private let keychain: Keychain | ||
|
||
public init(service: String, accessGroup: String?) { | ||
if let accessGroup { | ||
keychain = Keychain(service: service, accessGroup: accessGroup) | ||
} else { | ||
keychain = Keychain(service: service) | ||
public init(service: String, accessGroup: String?) { | ||
if let accessGroup { | ||
keychain = Keychain(service: service, accessGroup: accessGroup) | ||
} else { | ||
keychain = Keychain(service: service) | ||
} | ||
} | ||
} | ||
|
||
public func store(key: String, value: Data) throws { | ||
try keychain.set(value, key: key) | ||
} | ||
public func store(key: String, value: Data) throws { | ||
try keychain.set(value, key: key) | ||
} | ||
|
||
public func retrieve(key: String) throws -> Data? { | ||
try keychain.getData(key) | ||
} | ||
public func retrieve(key: String) throws -> Data? { | ||
try keychain.getData(key) | ||
} | ||
|
||
public func remove(key: String) throws { | ||
try keychain.remove(key) | ||
public func remove(key: String) throws { | ||
try keychain.remove(key) | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,84 @@ | ||
#if os(Windows) | ||
import Foundation | ||
import WinSDK | ||
import Foundation | ||
import WinSDK | ||
|
||
enum WinCredLocalStorageError: Error { | ||
case windows(UInt32) | ||
case other(Int) | ||
} | ||
enum WinCredLocalStorageError: Error { | ||
case windows(UInt32) | ||
case other(Int) | ||
} | ||
|
||
public struct WinCredLocalStorage: AuthLocalStorage { | ||
private let service: String | ||
public struct WinCredLocalStorage: AuthLocalStorage { | ||
private let service: String | ||
|
||
private let credentialType: DWORD | ||
private let credentialPersistence: DWORD | ||
private let credentialType: DWORD | ||
private let credentialPersistence: DWORD | ||
|
||
public init(service: String) { | ||
self.service = service | ||
credentialType = DWORD(CRED_TYPE_GENERIC) | ||
credentialPersistence = DWORD(CRED_PERSIST_LOCAL_MACHINE) | ||
} | ||
public init(service: String) { | ||
self.service = service | ||
credentialType = DWORD(CRED_TYPE_GENERIC) | ||
credentialPersistence = DWORD(CRED_PERSIST_LOCAL_MACHINE) | ||
} | ||
|
||
public func store(key: String, value: Data) throws { | ||
var valueData = value | ||
public func store(key: String, value: Data) throws { | ||
var valueData = value | ||
|
||
var credential: CREDENTIALW = .init() | ||
var credential: CREDENTIALW = .init() | ||
|
||
credential.Type = credentialType | ||
credential.Persist = credentialPersistence | ||
"\(service)\\\(key)".withCString(encodedAs: UTF16.self, { keyName in | ||
credential.TargetName = UnsafeMutablePointer(mutating: keyName) | ||
}) | ||
credential.Type = credentialType | ||
credential.Persist = credentialPersistence | ||
"\(service)\\\(key)".withCString(encodedAs: UTF16.self) { keyName in | ||
credential.TargetName = UnsafeMutablePointer(mutating: keyName) | ||
} | ||
|
||
withUnsafeMutableBytes(of: &valueData, { data in | ||
credential.CredentialBlobSize = DWORD(data.count) | ||
credential.CredentialBlob = data.baseAddress!.assumingMemoryBound(to: UInt8.self) | ||
}) | ||
withUnsafeMutableBytes(of: &valueData) { data in | ||
credential.CredentialBlobSize = DWORD(data.count) | ||
credential.CredentialBlob = data.baseAddress!.assumingMemoryBound(to: UInt8.self) | ||
} | ||
|
||
if !CredWriteW(&credential, 0) { | ||
let lastError = GetLastError() | ||
debugPrint("Unable to save password to credential vault, got error code \(lastError)") | ||
if !CredWriteW(&credential, 0) { | ||
let lastError = GetLastError() | ||
debugPrint("Unable to save password to credential vault, got error code \(lastError)") | ||
|
||
throw WinCredLocalStorageError.windows(lastError) | ||
throw WinCredLocalStorageError.windows(lastError) | ||
} | ||
} | ||
} | ||
|
||
public func retrieve(key: String) throws -> Data? { | ||
var credential: PCREDENTIALW? | ||
public func retrieve(key: String) throws -> Data? { | ||
var credential: PCREDENTIALW? | ||
|
||
let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self, { $0 }) | ||
let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self) { $0 } | ||
|
||
if !CredReadW(targetName, credentialType, 0, &credential) { | ||
let lastError = GetLastError() | ||
debugPrint("Unable to find entry for key in credential vault, got error code \(lastError)") | ||
if !CredReadW(targetName, credentialType, 0, &credential) { | ||
let lastError = GetLastError() | ||
debugPrint("Unable to find entry for key in credential vault, got error code \(lastError)") | ||
|
||
throw WinCredLocalStorageError.windows(lastError) | ||
} | ||
throw WinCredLocalStorageError.windows(lastError) | ||
} | ||
|
||
guard let foundCredential = credential, let blob = foundCredential.pointee.CredentialBlob else { | ||
throw WinCredLocalStorageError.other(-1) | ||
} | ||
guard let foundCredential = credential, | ||
let blob = foundCredential.pointee.CredentialBlob | ||
else { | ||
throw WinCredLocalStorageError.other(-1) | ||
} | ||
|
||
let blobSize = Int(foundCredential.pointee.CredentialBlobSize) | ||
let pointer = blob.withMemoryRebound(to: UInt8.self, capacity: blobSize, { $0 }) | ||
let data = Data(bytes: pointer, count: blobSize) | ||
let blobSize = Int(foundCredential.pointee.CredentialBlobSize) | ||
let pointer = blob.withMemoryRebound(to: UInt8.self, capacity: blobSize) { $0 } | ||
let data = Data(bytes: pointer, count: blobSize) | ||
|
||
CredFree(foundCredential) | ||
CredFree(foundCredential) | ||
|
||
return data | ||
} | ||
return data | ||
} | ||
|
||
public func remove(key: String) throws { | ||
let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self, { $0 }) | ||
public func remove(key: String) throws { | ||
let targetName = "\(service)\\\(key))".withCString(encodedAs: UTF16.self) { $0 } | ||
|
||
if !CredDeleteW(targetName, credentialType, 0) { | ||
let lastError = GetLastError() | ||
debugPrint("Unable to remove key from credential vault, got error code \(lastError)") | ||
if !CredDeleteW(targetName, credentialType, 0) { | ||
let lastError = GetLastError() | ||
debugPrint("Unable to remove key from credential vault, got error code \(lastError)") | ||
|
||
throw WinCredLocalStorageError.windows(lastError) | ||
throw WinCredLocalStorageError.windows(lastError) | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.