damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

CredentialHandler.swift (1709B)


      1 //
      2 //  CredentialHandler.swift
      3 //  damus
      4 //
      5 //  Created by Bryan Montz on 4/26/23.
      6 //
      7 
      8 import Foundation
      9 import AuthenticationServices
     10 
     11 final class CredentialHandler: NSObject, ASAuthorizationControllerDelegate {
     12     
     13     func check_credentials() {
     14         let requests: [ASAuthorizationRequest] = [ASAuthorizationPasswordProvider().createRequest()]
     15         let authorizationController = ASAuthorizationController(authorizationRequests: requests)
     16         authorizationController.delegate = self
     17         authorizationController.performRequests()
     18     }
     19     
     20     func save_credential(pubkey: Pubkey, privkey: Privkey) {
     21         let pub = pubkey.npub
     22         let priv = privkey.nsec
     23 
     24         SecAddSharedWebCredential("damus.io" as CFString, pub as CFString, priv as CFString, { error in
     25             if let error {
     26                 print("⚠️ An error occurred while saving credentials: \(error)")
     27             }
     28         })
     29     }
     30     
     31     // MARK: - ASAuthorizationControllerDelegate
     32     func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
     33         guard let cred = authorization.credential as? ASPasswordCredential,
     34               let parsedKey = parse_key(cred.password) else {
     35             return
     36         }
     37         
     38         Task {
     39             switch parsedKey {
     40             case .pub, .priv:
     41                 try? await process_login(parsedKey, is_pubkey: false)
     42             default:
     43                 break
     44             }
     45         }
     46     }
     47     
     48     func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
     49         print("⚠️ Warning: authentication failed with error: \(error)")
     50     }
     51 }