Files
damus/damus/Util/CredentialHandler.swift
William Casarin cebd1f48ca ndb: switch to nostrdb notes
This is a refactor of the codebase to use a more memory-efficient
representation of notes. It should also be much faster at decoding since
we're using a custom C json parser now.

Changelog-Changed: Improved memory usage and performance when processing events
2023-08-03 13:20:36 -07:00

52 lines
1.7 KiB
Swift

//
// CredentialHandler.swift
// damus
//
// Created by Bryan Montz on 4/26/23.
//
import Foundation
import AuthenticationServices
final class CredentialHandler: NSObject, ASAuthorizationControllerDelegate {
func check_credentials() {
let requests: [ASAuthorizationRequest] = [ASAuthorizationPasswordProvider().createRequest()]
let authorizationController = ASAuthorizationController(authorizationRequests: requests)
authorizationController.delegate = self
authorizationController.performRequests()
}
func save_credential(pubkey: Pubkey, privkey: Privkey) {
let pub = pubkey.npub
let priv = privkey.nsec
SecAddSharedWebCredential("damus.io" as CFString, pub as CFString, priv as CFString, { error in
if let error {
print("⚠️ An error occurred while saving credentials: \(error)")
}
})
}
// MARK: - ASAuthorizationControllerDelegate
func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
guard let cred = authorization.credential as? ASPasswordCredential,
let parsedKey = parse_key(cred.password) else {
return
}
Task {
switch parsedKey {
case .pub, .priv:
try? await process_login(parsedKey, is_pubkey: false)
default:
break
}
}
}
func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
print("⚠️ Warning: authentication failed with error: \(error)")
}
}