Swift (iOS/macOS) के लिए OAuth क्लाइंट लाइब्रेरी
OAuth (Open Authorization) एक ओपन स्टैंडर्ड प्रोटोकॉल है जो एप्लिकेशन को यूज़र का यूज़रनेम और पासवर्ड लिए बिना उनके संसाधनों तक सीमित एक्सेस देता है। Apple इकोसिस्टम (iOS, macOS, watchOS, tvOS) में OAuth 2.0 का इस्तेमाल Google, Facebook, GitHub, Twitter जैसी सर्विसेज के साथ इंटीग्रेशन के लिए मानक है। Swift डेवलपर्स के लिए कई अच्छी लाइब्रेरीज़ मौजूद हैं जो ऑथेंटिकेशन और टोकन मैनेजमेंट को आसान बनाती हैं।
इस आर्टिकल में हम सबसे लोकप्रिय और फ्लेक्सिबल लाइब्रेरी OAuthSwift के बारे में जानेंगे, साथ ही Google द्वारा अनुशंसित AppAuth-iOS पर भी चर्चा करेंगे। आप सीखेंगे कि लाइब्रेरी कैसे जोड़ें, ऑथराइज़ेशन कैसे करें, access token और refresh token कैसे प्राप्त करें और उन्हें सुरक्षित रूप से कैसे स्टोर करें।
OAuthSwift की मुख्य विशेषताएं
- OAuth 1.0 और OAuth 2.0 का सपोर्ट
- WebView (SFSafariViewController, WKWebView) के साथ बिल्ट-इन सपोर्ट
- Access token को ऑटोमैटिकली प्राप्त और रिफ्रेश करना
- विभिन्न ग्रांट टाइप्स का सपोर्ट (Authorization Code, Implicit, Client Credentials, Resource Owner Password)
- Alamofire, URLSession और अन्य नेटवर्किंग फ्रेमवर्क के साथ आसान इंटीग्रेशन
- Async कॉलबैक (completion handlers) और Combine सपोर्ट (एक्सटेंशन के माध्यम से)
- Keychain के ज़रिए टोकन की सुरक्षित स्टोरेज
OAuthSwift इंस्टॉल करना
यह लाइब्रेरी Swift Package Manager (SPM) या CocoaPods के ज़रिए इंस्टॉल की जा सकती है। SPM का उदाहरण:
// Xcode में: File → Add Packages...
// URL: https://github.com/OAuthSwift/OAuthSwift.git
// वर्जन: 2.2.0 या उससे ऊपर
या Package.swift के ज़रिए:
dependencies: [
.package(url: "https://github.com/OAuthSwift/OAuthSwift.git", from: "2.2.0")
]
OAuth 2.0 (Google) के साथ इंटीग्रेशन का उदाहरण
एक सामान्य सीनारियो देखते हैं — Google के ज़रिए ऑथराइज़ेशन और access token प्राप्त करना। इसके लिए पहले Google Cloud Console में अपना ऐप रजिस्टर करें और client_id और client_secret प्राप्त करें।
स्टेप 1. URL scheme सेट करना
Info.plist फ़ाइल में कॉलबैक के लिए स्कीम जोड़ें:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.YOUR_CLIENT_ID</string>
</array>
</dict>
</array>
स्टेप 2. ऑथराइज़ेशन कोड
import OAuthSwift
let oauthswift = OAuth2Swift(
consumerKey: "YOUR_CLIENT_ID",
consumerSecret: "YOUR_CLIENT_SECRET",
authorizeUrl: "https://accounts.google.com/o/oauth2/auth",
accessTokenUrl: "https://oauth2.googleapis.com/token",
responseType: "code"
)
// WebView दिखाने के लिए कंट्रोलर का रेफरेंस सेव करें
let handler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)
oauthswift.authorizeURLHandler = handler
let state = generateState(withLength: 20)
oauthswift.authorize(
withCallbackURL: URL(string: "com.googleusercontent.apps.YOUR_CLIENT_ID:/oauth2callback")!,
scope: "https://www.googleapis.com/auth/userinfo.email",
state: state,
parameters: [:]) { result in
switch result {
case .success(let (credential, response, parameters)):
print("Access Token: \(credential.oauthToken)")
print("Refresh Token: \(credential.oauthRefreshToken)")
// टोकन को Keychain में सेव करें
KeychainWrapper.standard.set(credential.oauthToken, forKey: "access_token")
KeychainWrapper.standard.set(credential.oauthRefreshToken, forKey: "refresh_token")
case .failure(let error):
print("ऑथराइज़ेशन में त्रुटि: \(error.localizedDescription)")
}
}
स्टेप 3. टोकन रिफ्रेश करना
को