iOS์์ Facebook์ผ๋ก ์ฌ์ฉ์ ๋ก๊ทธ์ธ
Facebook ๋ก๊ทธ์ธ ๋๋ Facebook Limited ๋ก๊ทธ์ธ์ ์ฑ์ ํตํฉํ์ฌ ์ฌ์ฉ์๊ฐ Facebook ๊ณ์ ์ ์ฌ์ฉํ์ฌ Identity Platform์ผ๋ก ์ธ์ฆํ๋๋ก ํ ์ ์์ต๋๋ค. ์ด ํ์ด์ง์์๋ Identity Platform์ ์ฌ์ฉํ์ฌ iOS ์ฑ์ Facebook์ผ๋ก ๋ก๊ทธ์ธ์ ์ถ๊ฐํ๋ ๋ฐฉ๋ฒ์ ๋ณด์ฌ์ค๋๋ค.
์์ํ๊ธฐ ์ ์
Podfile
์ ๋ค์ ํฌ๋๋ฅผ ์ถ๊ฐํฉ๋๋ค.pod 'FirebaseAuth'
Google Cloud ์ฝ์์์ ID ๊ณต๊ธ์ ์ฒด ํ์ด์ง๋ก ์ด๋ํฉ๋๋ค.
๊ณต๊ธ์ ์ฒด ์ถ๊ฐ๋ฅผ ํด๋ฆญํฉ๋๋ค.
๋ชฉ๋ก์์ Facebook์ ์ ํํฉ๋๋ค.
Facebook ์ฑ ID์ ์ฑ ๋ณด์ ๋น๋ฐ์ ์ ๋ ฅํฉ๋๋ค. ์์ง ID์ ๋ณด์ ๋น๋ฐ์ด ์์ผ๋ฉด Facebook for Developers ํ์ด์ง์์ ์ป์ ์ ์์ต๋๋ค.
Facebook ๊ตฌ์ฑ ์๋์ ๋์ด๋ URI๋ฅผ ํธ์ํฐ ์ฑ์ ์ ํจํ OAuth ๋ฆฌ๋๋ ์ URI๋ก ๊ตฌ์ฑํฉ๋๋ค. Identity Platform์์ ์ปค์คํ ๋๋ฉ์ธ์ ๊ตฌ์ฑํ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๋๋ฉ์ธ ๋์ ์ปค์คํ ๋๋ฉ์ธ์ ์ฌ์ฉํ๋๋ก Facebook ์ฑ ๊ตฌ์ฑ์ ๋ฆฌ๋๋ ์ URI๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค. ์๋ฅผ ๋ค์ด
https://myproject.firebaseapp.com/__/auth/handler
๋ฅผhttps://auth.myownpersonaldomain.com/__/auth/handler
๋ก ๋ณ๊ฒฝํฉ๋๋ค.์น์ธ๋ ๋๋ฉ์ธ ์๋์ ๋๋ฉ์ธ ์ถ๊ฐ๋ฅผ ํด๋ฆญํ์ฌ ์ฑ์ ๋๋ฉ์ธ์ ๋ฑ๋กํฉ๋๋ค. ๊ฐ๋ฐ ์ฉ๋๋ก๋
localhost
๊ฐ ์ด๋ฏธ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฌ์ฉ ์ค์ ๋์ด ์์ต๋๋ค.์ ํ๋ฆฌ์ผ์ด์ ๊ตฌ์ฑ์์ ์ค์ ์ธ๋ถ์ ๋ณด๋ฅผ ํด๋ฆญํฉ๋๋ค. ์ค๋ํซ์ ์ฑ ์ฝ๋์ ๋ณต์ฌํ์ฌ Identity Platform ํด๋ผ์ด์ธํธ SDK๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
์ ์ฅ์ ํด๋ฆญํฉ๋๋ค.
Facebook ๋ก๊ทธ์ธ ๊ตฌํ
'๊ธฐ๋ณธ' Facebook ๋ก๊ทธ์ธ์ ์ฌ์ฉํ๋ ค๋ฉด ๋ค์ ๋จ๊ณ๋ฅผ ์๋ฃํ์ธ์. ๋๋ ๋ค์ ์น์ ์์์ ๊ฐ์ด Facebook Limited ๋ก๊ทธ์ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- ๊ฐ๋ฐ์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํด ์ฑ์ Facebook ์ธ์ฆ์ ํตํฉํฉ๋๋ค.
FBSDKLoginButton
๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ ๋ ๋ก๊ทธ์ธ ๋ฐ ๋ก๊ทธ์์ ์ด๋ฒคํธ๋ฅผ ๋ฐ์ ๋๋ฆฌ์๋ฅผ ์ค์ ํฉ๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.Swift
let loginButton = FBSDKLoginButton() loginButton.delegate = self
Objective-C
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init]; loginButton.delegate = self;
didCompleteWithResult:error:
๋ฅผ ๊ตฌํํฉ๋๋ค.Swift
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { if let error = error { print(error.localizedDescription) return } // ... }
Objective-C
- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error { if (error == nil) { // ... } else { NSLog(error.localizedDescription); } }
UIApplicationDelegate
์์ Firebase ๋ชจ๋์ ๊ฐ์ ธ์ต๋๋ค.Swift
import FirebaseCore import FirebaseAuth
Objective-C
@import FirebaseCore; @import FirebaseAuth;
- ์ผ๋ฐ์ ์ผ๋ก ์ฑ์
application:didFinishLaunchingWithOptions:
๋ฉ์๋์์FirebaseApp
๊ณต์ ์ธ์คํด์ค๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.Swift
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- ์ฌ์ฉ์๊ฐ ์ฑ๊ณต์ ์ผ๋ก ๋ก๊ทธ์ธํ๋ฉด
didCompleteWithResult:error:
๊ตฌํ์์ ๋ก๊ทธ์ธํ ์ฌ์ฉ์์ ์ก์ธ์ค ํ ํฐ์ ๊ฐ์ ธ์์ Identity Platform ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด๋ก ๊ตํํฉ๋๋ค.Swift
let credential = FacebookAuthProvider .credential(withAccessToken: AccessToken.current!.tokenString)
Objective-C
FIRAuthCredential *credential = [FIRFacebookAuthProvider credentialWithAccessToken:[FBSDKAccessToken currentAccessToken].tokenString];
Facebook Limited ๋ก๊ทธ์ธ ๊ตฌํ
'๊ธฐ๋ณธ' Facebook ๋ก๊ทธ์ธ ๋์ Facebook Limited ๋ก๊ทธ์ธ์ ์ฌ์ฉํ๋ ค๋ฉด ๋ค์ ๋จ๊ณ๋ฅผ ์๋ฃํ์ธ์.
- ๊ฐ๋ฐ์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํด ์ฑ์ Facebook Limited ๋ก๊ทธ์ธ์ ํตํฉํฉ๋๋ค.
- ๋ก๊ทธ์ธ ์์ฒญ๋ง๋ค ๊ณ ์ ํ ์์ ๋ฌธ์์ด์ธ 'nonce'๊ฐ ์์ฑ๋๋ฉฐ ์ด ๋ฌธ์์ด์ ์ฑ์ ์ธ์ฆ ์์ฒญ์ ๋ํ ์๋ต์ผ๋ก ID ํ ํฐ์ด ๋ช
์์ ์ผ๋ก ๋ถ์ฌ๋์๋์ง ํ์ธํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ๋ฆด๋ ์ด ๊ณต๊ฒฉ์ ๋ฐฉ์งํ๋ ค๋ฉด ์ด ๋จ๊ณ๊ฐ ํ์ํฉ๋๋ค.
๋ค์ ์์์์์ ๊ฐ์ด iOS์์
SecRandomCopyBytes(_:_:_)
๋ฅผ ์ฌ์ฉํ์ฌ ์ํธ๋ก ๋ณดํธ๋ nonce๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.Swift
private func randomNonceString(length: Int = 32) -> String { precondition(length > 0) var randomBytes = [UInt8](repeating: 0, count: length) let errorCode = SecRandomCopyBytes(kSecRandomDefault, randomBytes.count, &randomBytes) if errorCode != errSecSuccess { fatalError( "Unable to generate nonce. SecRandomCopyBytes failed with OSStatus \(errorCode)" ) } let charset: [Character] = Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._") let nonce = randomBytes.map { byte in // Pick a random character from the set, wrapping around if needed. charset[Int(byte) % charset.count] } return String(nonce) }
Objective-C
// Adapted from https://auth0.com/docs/api-auth/tutorials/nonce#generate-a-cryptographically-random-nonce - (NSString *)randomNonce:(NSInteger)length { NSAssert(length > 0, @"Expected nonce to have positive length"); NSString *characterSet = @"0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._"; NSMutableString *result = [NSMutableString string]; NSInteger remainingLength = length; while (remainingLength > 0) { NSMutableArray *randoms = [NSMutableArray arrayWithCapacity:16]; for (NSInteger i = 0; i < 16; i++) { uint8_t random = 0; int errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random); NSAssert(errorCode == errSecSuccess, @"Unable to generate nonce: OSStatus %i", errorCode); [randoms addObject:@(random)]; } for (NSNumber *random in randoms) { if (remainingLength == 0) { break; } if (random.unsignedIntValue < characterSet.length) { unichar character = [characterSet characterAtIndex:random.unsignedIntValue]; [result appendFormat:@"%C", character]; remainingLength--; } } } return [result copy]; }
Swift
@available(iOS 13, *) private func sha256(_ input: String) -> String { let inputData = Data(input.utf8) let hashedData = SHA256.hash(data: inputData) let hashString = hashedData.compactMap { String(format: "%02x", $0) }.joined() return hashString }
Objective-C
- (NSString *)stringBySha256HashingString:(NSString *)input { const char *string = [input UTF8String]; unsigned char result[CC_SHA256_DIGEST_LENGTH]; CC_SHA256(string, (CC_LONG)strlen(string), result); NSMutableString *hashed = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2]; for (NSInteger i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) { [hashed appendFormat:@"%02x", result[i]]; } return hashed; }
FBSDKLoginButton
์ ์ค์ ํ ๋ ๋ก๊ทธ์ธ ๋ฐ ๋ก๊ทธ์์ ์ด๋ฒคํธ๋ฅผ ๋ฐ์ ๋๋ฆฌ์ธ์ ์ค์ ํ๊ณ ์ถ์ ๋ชจ๋๋ฅผFBSDKLoginTrackingLimited
๋ก ์ค์ ํ ๋ค์ nonce๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.Swift
func setupLoginButton() { let nonce = randomNonceString() currentNonce = nonce loginButton.delegate = self loginButton.loginTracking = .limited loginButton.nonce = sha256(nonce) }
Objective-C
- (void)setupLoginButton { NSString *nonce = [self randomNonce:32]; self.currentNonce = nonce; self.loginButton.delegate = self; self.loginButton.loginTracking = FBSDKLoginTrackingLimited self.loginButton.nonce = [self stringBySha256HashingString:nonce]; }
didCompleteWithResult:error:
๋ฅผ ๊ตฌํํฉ๋๋ค.Swift
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) { if let error = error { print(error.localizedDescription) return } // ... }
Objective-C
- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result error:(NSError *)error { if (error == nil) { // ... } else { NSLog(error.localizedDescription); } }
UIApplicationDelegate
์์ Firebase ๋ชจ๋์ ๊ฐ์ ธ์ต๋๋ค.Swift
import Firebase
Objective-C
@import Firebase;
- ์ผ๋ฐ์ ์ผ๋ก ์ฑ์
application:didFinishLaunchingWithOptions:
๋ฉ์๋์์FirebaseApp
๊ณต์ ์ธ์คํด์ค๋ฅผ ๊ตฌ์ฑํฉ๋๋ค.Swift
// Use Firebase library to configure APIs FirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs [FIRApp configure];
- ์ฌ์ฉ์๊ฐ ์ฑ๊ณต์ ์ผ๋ก ๋ก๊ทธ์ธํ๋ฉด
didCompleteWithResult:error:
๊ตฌํ์์ ํด์๋์ง ์์ nonce๊ฐ ํฌํจ๋ Facebook์ ์๋ต์์ ID ํ ํฐ์ ์ฌ์ฉํ์ฌ Identity Platform ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ต๋๋ค.Swift
// Initialize an Identity Platform credential. let idTokenString = AuthenticationToken.current?.tokenString let nonce = currentNonce let credential = OAuthProvider.credential(withProviderID: "facebook.com", IDToken: idTokenString, rawNonce: nonce)
Objective-C
// Initialize an Identity Platform credential. NSString *idTokenString = FBSDKAuthenticationToken.currentAuthenticationToken.tokenString; NSString *rawNonce = self.currentNonce; FIROAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:@"facebook.com" IDToken:idTokenString rawNonce:rawNonce];
Identity Platform์ผ๋ก ์ธ์ฆ
๋ง์ง๋ง์ผ๋ก Identity Platform ์ฌ์ฉ์ ์ธ์ฆ ์ ๋ณด๋ฅผ ์ฌ์ฉํ์ฌ Identity Platform์ผ๋ก ์ธ์ฆํฉ๋๋ค.
Swift
Auth.auth().signIn(with: credential) { authResult, error in if let error = error { let authError = error as NSError if isMFAEnabled, authError.code == AuthErrorCode.secondFactorRequired.rawValue { // The user is a multi-factor user. Second factor challenge is required. let resolver = authError .userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver var displayNameString = "" for tmpFactorInfo in resolver.hints { displayNameString += tmpFactorInfo.displayName ?? "" displayNameString += " " } self.showTextInputPrompt( withMessage: "Select factor to sign in\n\(displayNameString)", completionBlock: { userPressedOK, displayName in var selectedHint: PhoneMultiFactorInfo? for tmpFactorInfo in resolver.hints { if displayName == tmpFactorInfo.displayName { selectedHint = tmpFactorInfo as? PhoneMultiFactorInfo } } PhoneAuthProvider.provider() .verifyPhoneNumber(with: selectedHint!, uiDelegate: nil, multiFactorSession: resolver .session) { verificationID, error in if error != nil { print( "Multi factor start sign in failed. Error: \(error.debugDescription)" ) } else { self.showTextInputPrompt( withMessage: "Verification code for \(selectedHint?.displayName ?? "")", completionBlock: { userPressedOK, verificationCode in let credential: PhoneAuthCredential? = PhoneAuthProvider.provider() .credential(withVerificationID: verificationID!, verificationCode: verificationCode!) let assertion: MultiFactorAssertion? = PhoneMultiFactorGenerator .assertion(with: credential!) resolver.resolveSignIn(with: assertion!) { authResult, error in if error != nil { print( "Multi factor finanlize sign in failed. Error: \(error.debugDescription)" ) } else { self.navigationController?.popViewController(animated: true) } } } ) } } } ) } else { self.showMessagePrompt(error.localizedDescription) return } // ... return } // User is signed in // ... }
Objective-C
[[FIRAuth auth] signInWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (isMFAEnabled && error && error.code == FIRAuthErrorCodeSecondFactorRequired) { FIRMultiFactorResolver *resolver = error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey]; NSMutableString *displayNameString = [NSMutableString string]; for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) { [displayNameString appendString:tmpFactorInfo.displayName]; [displayNameString appendString:@" "]; } [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Select factor to sign in\n%@", displayNameString] completionBlock:^(BOOL userPressedOK, NSString *_Nullable displayName) { FIRPhoneMultiFactorInfo* selectedHint; for (FIRMultiFactorInfo *tmpFactorInfo in resolver.hints) { if ([displayName isEqualToString:tmpFactorInfo.displayName]) { selectedHint = (FIRPhoneMultiFactorInfo *)tmpFactorInfo; } } [FIRPhoneAuthProvider.provider verifyPhoneNumberWithMultiFactorInfo:selectedHint UIDelegate:nil multiFactorSession:resolver.session completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) { if (error) { [self showMessagePrompt:error.localizedDescription]; } else { [self showTextInputPromptWithMessage:[NSString stringWithFormat:@"Verification code for %@", selectedHint.displayName] completionBlock:^(BOOL userPressedOK, NSString *_Nullable verificationCode) { FIRPhoneAuthCredential *credential = [[FIRPhoneAuthProvider provider] credentialWithVerificationID:verificationID verificationCode:verificationCode]; FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential]; [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { [self showMessagePrompt:error.localizedDescription]; } else { NSLog(@"Multi factor finanlize sign in succeeded."); } }]; }]; } }]; }]; } else if (error) { // ... return; } // User successfully signed in. Get user data from the FIRUser object if (authResult == nil) { return; } FIRUser *user = authResult.user; // ... }];
๋ค์ ๋จ๊ณ
- Identity Platform ์ฌ์ฉ์์ ๋ํด ์์ธํ ์์๋ด ๋๋ค.
- ๋ค๋ฅธ ID ๊ณต๊ธ์ ์ฒด๋ก ์ฌ์ฉ์๋ฅผ ๋ก๊ทธ์ธ ์ฒ๋ฆฌํฉ๋๋ค.