Swift में ऑथेंटिकेशन और ऑथराइजेशन (iOS/macOS)
ऑथेंटिकेशन और ऑथराइजेशन किसी भी आधुनिक ऐप के मुख्य घटक हैं। Apple इकोसिस्टम (iOS/macOS) में डेवलपर्स के पास शक्तिशाली फ्रेमवर्क और लाइब्रेरी हैं जो इन प्रक्रियाओं को आसान बनाते हैं। इस लेख में हम मुख्य दृष्टिकोण, लोकप्रिय लाइब्रेरी और Swift में व्यावहारिक उदाहरण देखेंगे।
बुनियादी अवधारणाएँ
कोड पर जाने से पहले, दो शब्दों को समझना ज़रूरी है:
- ऑथेंटिकेशन — उपयोगकर्ता की पहचान सत्यापित करना (जैसे, लॉगिन + पासवर्ड, Face ID, Touch ID)।
- ऑथराइजेशन — ऑथेंटिकेशन के बाद एक्सेस अधिकार निर्धारित करना (जैसे, क्या उपयोगकर्ता कुछ डेटा देख सकता है)।
Apple की अंतर्निहित सुविधाएँ
1. LocalAuthentication (Face ID / Touch ID)
LocalAuthentication फ्रेमवर्क आपको डिवाइस के बायोमेट्रिक डेटा का उपयोग करके ऑथेंटिकेशन करने देता है।
import LocalAuthentication
func authenticateWithBiometrics() {
let context = LAContext()
var error: NSError?
// जाँचें कि बायोमेट्रिक्स उपलब्ध है या नहीं
guard context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) else {
print("बायोमेट्रिक्स उपलब्ध नहीं: \(error?.localizedDescription ?? "त्रुटि")")
return
}
// ऑथेंटिकेशन का अनुरोध करें
context.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: "ऐप एक्सेस करने के लिए लॉगिन करें") { success, authError in
DispatchQueue.main.async {
if success {
print("ऑथेंटिकेशन सफल")
} else {
print("त्रुटि: \(authError?.localizedDescription ?? "अज्ञात त्रुटि")")
}
}
}
}
2. Keychain Services
Keychain पासवर्ड और टोकन के लिए एक सुरक्षित स्टोरेज है। ऑथेंटिकेशन के बाद क्रेडेंशियल स्टोर करने के लिए इसका उपयोग करें।
import Security
func saveToKeychain(service: String, account: String, password: String) {
guard let passwordData = password.data(using: .utf8) else { return }
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
kSecValueData as String: passwordData
]
// पुराना डेटा हटाएँ, यदि है
SecItemDelete(query as CFDictionary)
// नया डेटा जोड़ें
let status = SecItemAdd(query as CFDictionary, nil)
if status == errSecSuccess {
print("डेटा Keychain में सेव हो गया")
} else {
print("सेव करने में त्रुटि: \(status)")
}
}
func readFromKeychain(service: String, account: String) -> String? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: account,
kSecReturnData as String: true,
kSecMatchLimit as String: kSecMatchLimitOne
]
var item: CFTypeRef?
let status = SecItemCopyMatching(query as CFDictionary, &item)
guard status == errSecSuccess, let data = item as? Data else {
return nil
}
return String(data: data, encoding: .utf8)
}
ऑथराइजेशन के लिए लोकप्रिय लाइब्रेरी
1. Firebase Authentication
Firebase ईमेल/पासवर्ड, Google, Apple, Facebook और अन्य प्रदाताओं के माध्यम से ऑथेंटिकेशन का तैयार समाधान प्रदान करता है।
import FirebaseAuth
func signInWithEmail(email: String, password: String) {
Auth.auth().signIn(withEmail: email, password: password) { result, error in
if let error = error {
print("लॉगिन में त्रुटि: \(error.localizedDescription)")
return
}
print("उपयोगकर्ता लॉग इन: \(result?.user.uid ?? "कोई नहीं")")
}
}
func signUpWithEmail(email: String, password: String) {
Auth.auth().createUser(withEmail: email, password: password) { result, error in
if let error = error {
print("रजिस्ट्रेशन में त्रुटि: \(error.localizedDescription)")
return
}
print("उपयोगकर्ता बनाया गया: \(result?.user.uid ?? "कोई नहीं")")
}
}