หากอัปเกรดเป็น Firebase Authentication with Identity Platform แล้ว คุณจะเพิ่มการตรวจสอบสิทธิ์แบบหลายปัจจัยทาง SMS ลงในแอป iOS ได้
การตรวจสอบสิทธิ์แบบหลายปัจจัยช่วยเพิ่มความปลอดภัยให้แอปของคุณ แม้ว่าผู้โจมตีมักจะละเมิดรหัสผ่านที่ถูกละเมิดและบัญชีโซเชียล แต่การดักจับข้อความ SMS นั้นทำได้ยากกว่า
ก่อนเริ่มต้น
เปิดใช้ผู้ให้บริการอย่างน้อย 1 รายที่รองรับการตรวจสอบสิทธิ์แบบหลายปัจจัย ผู้ให้บริการทุกรายรองรับ MFA ยกเว้นการตรวจสอบสิทธิ์ทางโทรศัพท์ การตรวจสอบสิทธิ์แบบไม่ระบุตัวตน และ Apple Game Center
ตรวจสอบว่าแอปของคุณยืนยันอีเมลของผู้ใช้ MFA ต้องมีการยืนยันอีเมล ซึ่งจะป้องกันไม่ให้ผู้ไม่ประสงค์ดีลงทะเบียนใช้บริการด้วยอีเมลที่ตนไม่ได้เป็นเจ้าของ แล้วล็อกไม่ให้เจ้าของที่แท้จริงเข้าถึงโดยการเพิ่มปัจจัยที่ 2
การเปิดใช้การตรวจสอบสิทธิ์แบบหลายปัจจัย
ในคอนโซล Firebase ให้ไปที่ความปลอดภัย > การตรวจสอบสิทธิ์
ในแท็บวิธีการลงชื่อเข้าใช้ ให้เปิดใช้การตรวจสอบสิทธิ์แบบหลายปัจจัยผ่าน SMS ในส่วนขั้นสูง
นอกจากนี้ คุณควรป้อนหมายเลขโทรศัพท์ที่จะใช้ทดสอบแอปด้วย แม้ว่าจะไม่บังคับ แต่เราขอแนะนำอย่างยิ่งให้ลงทะเบียนหมายเลขโทรศัพท์ทดสอบเพื่อหลีกเลี่ยงการควบคุมปริมาณในระหว่างการพัฒนา
หากยังไม่ได้ดำเนินการ ให้ให้สิทธิ์โดเมนของแอปโดยทำดังนี้
ในคอนโซล Firebase ให้ไปที่แท็บความปลอดภัย > การตรวจสอบสิทธิ์ > การตั้งค่า
ในส่วนโดเมนที่ได้รับอนุญาต ให้คลิกเพิ่มโดเมน แล้วเพิ่มโดเมนของคุณ
กำลังยืนยันแอปของคุณ
Authentication ต้องยืนยันว่าคำขอ SMS มาจากแอปของคุณ ซึ่งทำได้ 2 วิธีดังนี้
การแจ้งเตือน APNs แบบไม่มีเสียง: เมื่อคุณลงชื่อเข้าใช้ผู้ใช้เป็นครั้งแรก Authentication จะส่งข้อความ Push แบบไม่มีเสียงไปยังอุปกรณ์ของผู้ใช้ได้ การตรวจสอบสิทธิ์จะดำเนินการต่อได้หากแอปได้รับการแจ้งเตือน โปรดทราบว่าตั้งแต่ iOS 8.0 เป็นต้นไป คุณไม่จำเป็นต้องขอให้ผู้ใช้อนุญาตข้อความ Push เพื่อใช้วิธีนี้
การยืนยัน reCAPTCHA: หากส่งการแจ้งเตือนแบบไม่แสดงข้อความไม่ได้ (เช่น เนื่องจากผู้ใช้ปิดใช้การรีเฟรชเบื้องหลัง หรือคุณกำลังทดสอบแอปใน iOS Simulator) คุณสามารถใช้ reCAPTCHA ได้ ในหลายกรณี reCAPTCHA จะแก้ปัญหาเองโดยอัตโนมัติโดยไม่ต้องมีการโต้ตอบของผู้ใช้
การใช้การแจ้งเตือนแบบไม่มีเสียง
วิธีเปิดใช้การแจ้งเตือน APNs เพื่อใช้กับ Authentication
ใน Xcode ให้เปิดใช้ข้อความ Push สำหรับโปรเจ็กต์
อัปโหลดคีย์การตรวจสอบสิทธิ์ APNs โดยใช้คอนโซล Firebase (การเปลี่ยนแปลงจะส่งต่อไปยัง Google Cloud Authentication โดยอัตโนมัติ) หากยังไม่มีคีย์การตรวจสอบสิทธิ์ APNs โปรดดูวิธีรับคีย์ที่หัวข้อการกำหนดค่า APNs ด้วย FCM
ในคอนโซล Firebase ให้ไปที่
การตั้งค่า > ทั่วไป จากนั้นคลิกแท็บ Cloud Messagingในส่วนการกำหนดค่าแอป iOS ใต้คีย์การตรวจสอบสิทธิ์ APN ให้คลิกอัปโหลดเพื่ออัปโหลดคีย์การตรวจสอบสิทธิ์การพัฒนา คีย์การตรวจสอบสิทธิ์เวอร์ชันที่ใช้งานจริง หรือทั้ง 2 อย่าง ทั้งนี้คุณต้องระบุอย่างน้อย 1 รายการ
เลือกคีย์
เพิ่มรหัสคีย์สำหรับคีย์ คุณดูรหัสคีย์ได้ในส่วน Certificates, Identifiers & Profiles ใน Apple Developer Member Center
คลิกอัปโหลด
หากมีใบรับรอง APNs อยู่แล้ว คุณสามารถอัปโหลดใบรับรองแทนได้
การใช้การยืนยัน reCAPTCHA
วิธีเปิดใช้ SDK ของไคลเอ็นต์เพื่อใช้ reCAPTCHA
เปิดการกำหนดค่าโปรเจ็กต์ใน Xcode
ดับเบิลคลิกชื่อโปรเจ็กต์ในมุมมองแบบต้นไม้ด้านซ้าย
เลือกแอปจากส่วนเป้าหมาย
เลือกแท็บข้อมูล
ขยายส่วนประเภท URL
คลิกปุ่ม +
ป้อนรหัสไคลเอ็นต์แบบย้อนกลับในช่อง URL Schemes คุณจะเห็นค่านี้แสดงอยู่ในไฟล์การกำหนดค่า
GoogleService-Info.plistเป็นREVERSED_CLIENT_ID
เมื่อเสร็จแล้ว การกำหนดค่าควรมีลักษณะคล้ายกับตัวอย่างต่อไปนี้

คุณสามารถปรับแต่งวิธีที่แอปแสดง SFSafariViewController หรือ UIWebView เมื่อแสดง reCAPTCHA ได้ (ไม่บังคับ) โดยสร้างคลาสที่กำหนดเองซึ่งสอดคล้องกับโปรโตคอล FIRAuthUIDelegate แล้วส่งไปยัง verifyPhoneNumber:UIDelegate:completion:
การเลือกรูปแบบการลงทะเบียน
คุณเลือกได้ว่าแอปของคุณต้องใช้การตรวจสอบสิทธิ์แบบหลายปัจจัยหรือไม่ รวมถึงวิธีและเวลาในการลงทะเบียนผู้ใช้ รูปแบบที่พบบ่อยมีดังนี้
ลงทะเบียนปัจจัยที่ 2 ของผู้ใช้เป็นส่วนหนึ่งของการลงทะเบียน ใช้วิธีนี้หากแอปของคุณกำหนดให้มีการตรวจสอบสิทธิ์แบบหลายปัจจัยสำหรับผู้ใช้ทุกคน โปรดทราบว่าบัญชีต้องมีอีเมลที่ยืนยันแล้วจึงจะลงทะเบียนปัจจัยที่ 2 ได้ ดังนั้นขั้นตอนการลงทะเบียนของคุณจะต้องรองรับข้อกำหนดนี้
เสนอตัวเลือกที่ข้ามได้เพื่อลงทะเบียนปัจจัยที่ 2 ในระหว่างการลงทะเบียน แอปที่ต้องการส่งเสริมแต่ไม่ได้บังคับให้ใช้การตรวจสอบสิทธิ์แบบหลายปัจจัยอาจเลือกใช้วิธีนี้
ให้ความสามารถในการเพิ่มปัจจัยที่ 2 จากหน้าการจัดการบัญชีหรือโปรไฟล์ของผู้ใช้ แทนที่จะเป็นหน้าจอลงชื่อสมัครใช้ วิธีนี้จะช่วยลดความยุ่งยากในระหว่างกระบวนการลงทะเบียน ขณะเดียวกันก็ยังคงให้ผู้ใช้ที่ให้ความสำคัญกับความปลอดภัยสามารถใช้การตรวจสอบสิทธิ์แบบหลายปัจจัยได้
กำหนดให้เพิ่มปัจจัยที่ 2 ทีละขั้นเมื่อผู้ใช้ต้องการเข้าถึงฟีเจอร์ที่มีข้อกำหนดด้านความปลอดภัยที่เข้มงวดขึ้น
การลงทะเบียนปัจจัยที่ 2
วิธีลงทะเบียนปัจจัยรองใหม่สำหรับผู้ใช้
ตรวจสอบสิทธิ์ผู้ใช้อีกครั้ง
ขอให้ผู้ใช้ป้อนหมายเลขโทรศัพท์
รับเซสชันแบบหลายปัจจัยสำหรับผู้ใช้โดยทำดังนี้
Swift
authResult.user.multiFactor.getSessionWithCompletion() { (session, error) in // ... }Objective-C
[authResult.user.multiFactor getSessionWithCompletion:^(FIRMultiFactorSession * _Nullable session, NSError * _Nullable error) { // ... }];ส่งข้อความยืนยันไปยังโทรศัพท์ของผู้ใช้ ตรวจสอบว่าหมายเลขโทรศัพท์ขึ้นต้นด้วย
+และไม่มีเครื่องหมายวรรคตอนหรือเว้นวรรคอื่นๆ (เช่น+15105551234)Swift
// Send SMS verification code. PhoneAuthProvider.provider().verifyPhoneNumber( phoneNumber, uiDelegate: nil, multiFactorSession: session) { (verificationId, error) in // verificationId will be needed for enrollment completion. }Objective-C
// Send SMS verification code. [FIRPhoneAuthProvider.provider verifyPhoneNumber:phoneNumber UIDelegate:nil multiFactorSession:session completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) { // verificationId will be needed for enrollment completion. }];แม้ว่าจะไม่บังคับ แต่แนวทางปฏิบัติแนะนำคือการแจ้งให้ผู้ใช้ทราบล่วงหน้าว่าผู้ใช้จะได้รับข้อความ SMS และมีค่าบริการตามอัตรามาตรฐาน
เมธอด
verifyPhoneNumber()จะเริ่มกระบวนการตรวจสอบแอปในเบื้องหลังโดยใช้ข้อความ Push แบบไม่มีเสียง หากข้อความ Push แบบไม่มีเสียงไม่พร้อมใช้งาน ระบบจะแสดงภาพทดสอบ reCAPTCHA แทนเมื่อส่งรหัส SMS แล้ว ให้ขอให้ผู้ใช้ยืนยันรหัส จากนั้นใช้คำตอบของลูกค้าเพื่อสร้าง
PhoneAuthCredentialดังนี้Swift
// Ask user for the verification code. Then: let credential = PhoneAuthProvider.provider().credential( withVerificationID: verificationId, verificationCode: verificationCode)Objective-C
// Ask user for the SMS verification code. Then: FIRPhoneAuthCredential *credential = [FIRPhoneAuthProvider.provider credentialWithVerificationID:verificationID verificationCode:kPhoneSecondFactorVerificationCode];เริ่มต้นออบเจ็กต์การยืนยัน
Swift
let assertion = PhoneMultiFactorGenerator.assertion(with: credential)Objective-C
FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];ลงทะเบียนให้เสร็จสมบูรณ์ คุณสามารถระบุชื่อที่แสดงสำหรับปัจจัยที่ 2 ได้ (ไม่บังคับ) ซึ่งจะเป็นประโยชน์สำหรับผู้ใช้ที่มีปัจจัยที่สองหลายรายการ เนื่องจากหมายเลขโทรศัพท์จะถูกมาสก์ในระหว่างขั้นตอนการตรวจสอบสิทธิ์ (เช่น +1******1234)
Swift
// Complete enrollment. This will update the underlying tokens // and trigger ID token change listener. user.multiFactor.enroll(with: assertion, displayName: displayName) { (error) in // ... }Objective-C
// Complete enrollment. This will update the underlying tokens // and trigger ID token change listener. [authResult.user.multiFactor enrollWithAssertion:assertion displayName:nil completion:^(NSError * _Nullable error) { // ... }];
โค้ดด้านล่างแสดงตัวอย่างที่เสร็จสมบูรณ์ของการลงทะเบียนปัจจัยที่ 2
Swift
let user = Auth.auth().currentUser
user?.multiFactor.getSessionWithCompletion({ (session, error) in
// Send SMS verification code.
PhoneAuthProvider.provider().verifyPhoneNumber(
phoneNumber,
uiDelegate: nil,
multiFactorSession: session
) { (verificationId, error) in
// verificationId will be needed for enrollment completion.
// Ask user for the verification code.
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationId!,
verificationCode: phoneSecondFactorVerificationCode)
let assertion = PhoneMultiFactorGenerator.assertion(with: credential)
// Complete enrollment. This will update the underlying tokens
// and trigger ID token change listener.
user?.multiFactor.enroll(with: assertion, displayName: displayName) { (error) in
// ...
}
}
})
Objective-C
FIRUser *user = FIRAuth.auth.currentUser;
[user.multiFactor getSessionWithCompletion:^(FIRMultiFactorSession * _Nullable session,
NSError * _Nullable error) {
// Send SMS verification code.
[FIRPhoneAuthProvider.provider
verifyPhoneNumber:phoneNumber
UIDelegate:nil
multiFactorSession:session
completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) {
// verificationId will be needed for enrollment completion.
// Ask user for the verification code.
// ...
// Then:
FIRPhoneAuthCredential *credential =
[FIRPhoneAuthProvider.provider credentialWithVerificationID:verificationID
verificationCode:kPhoneSecondFactorVerificationCode];
FIRMultiFactorAssertion *assertion =
[FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
// Complete enrollment. This will update the underlying tokens
// and trigger ID token change listener.
[user.multiFactor enrollWithAssertion:assertion
displayName:displayName
completion:^(NSError * _Nullable error) {
// ...
}];
}];
}];
ยินดีด้วย คุณลงทะเบียนปัจจัยการตรวจสอบสิทธิ์ที่ 2 ให้กับผู้ใช้เรียบร้อยแล้ว
การลงชื่อเข้าใช้ของผู้ใช้ด้วยปัจจัยที่ 2
วิธีลงชื่อเข้าใช้ให้ผู้ใช้ด้วยการยืนยันผ่าน SMS แบบ 2 ปัจจัย
ลงชื่อเข้าใช้ผู้ใช้ด้วยปัจจัยแรก จากนั้นตรวจหาข้อผิดพลาดที่ระบุว่าต้องมีการตรวจสอบสิทธิ์แบบหลายปัจจัย ข้อผิดพลาดนี้ประกอบด้วย Resolver, คำแนะนำเกี่ยวกับปัจจัยที่สองที่ลงทะเบียน และเซสชันพื้นฐานที่พิสูจน์ว่าผู้ใช้ตรวจสอบสิทธิ์ด้วยปัจจัยแรกสำเร็จ
ตัวอย่างเช่น หากปัจจัยแรกของผู้ใช้คืออีเมลและรหัสผ่าน
Swift
Auth.auth().signIn( withEmail: email, password: password ) { (result, error) in let authError = error as NSError if authError?.code == AuthErrorCode.secondFactorRequired.rawValue { // The user is a multi-factor user. Second factor challenge is required. let resolver = authError!.userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver // ... } else { // Handle other errors such as wrong password. } }Objective-C
[FIRAuth.auth signInWithEmail:email password:password completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error == nil || error.code != FIRAuthErrorCodeSecondFactorRequired) { // User is not enrolled with a second factor and is successfully signed in. // ... } else { // The user is a multi-factor user. Second factor challenge is required. } }];หากปัจจัยแรกของผู้ใช้เป็นผู้ให้บริการแบบรวมศูนย์ เช่น OAuth ให้ตรวจจับข้อผิดพลาดหลังจากเรียกใช้
getCredentialWith()หากผู้ใช้ลงทะเบียนปัจจัยรองไว้หลายรายการ ให้สอบถามผู้ใช้ว่าต้องการใช้ปัจจัยใด คุณดูหมายเลขโทรศัพท์ที่มาสก์ได้ด้วย
resolver.hints[selectedIndex].phoneNumberและดูชื่อที่แสดงได้ด้วยresolver.hints[selectedIndex].displayNameSwift
// Ask user which second factor to use. Then: if resolver.hints[selectedIndex].factorID == PhoneMultiFactorID { // User selected a phone second factor. // ... } else if resolver.hints[selectedIndex].factorID == TotpMultiFactorID { // User selected a TOTP second factor. // ... } else { // Unsupported second factor. }Objective-C
FIRMultiFactorResolver *resolver = (FIRMultiFactorResolver *) error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey]; // Ask user which second factor to use. Then: FIRPhoneMultiFactorInfo *hint = (FIRPhoneMultiFactorInfo *) resolver.hints[selectedIndex]; if (hint.factorID == FIRPhoneMultiFactorID) { // User selected a phone second factor. // ... } else if (hint.factorID == FIRTOTPMultiFactorID) { // User selected a TOTP second factor. // ... } else { // Unsupported second factor. }ส่งข้อความยืนยันไปยังโทรศัพท์ของผู้ใช้ โดยทำดังนี้
Swift
// Send SMS verification code. let hint = resolver.hints[selectedIndex] as! PhoneMultiFactorInfo PhoneAuthProvider.provider().verifyPhoneNumber( with: hint, uiDelegate: nil, multiFactorSession: resolver.session ) { (verificationId, error) in // verificationId will be needed for sign-in completion. }Objective-C
// Send SMS verification code [FIRPhoneAuthProvider.provider verifyPhoneNumberWithMultiFactorInfo:hint UIDelegate:nil multiFactorSession:resolver.session completion:^(NSString * _Nullable verificationID, NSError * _Nullable error) { if (error != nil) { // Failed to verify phone number. } }];เมื่อส่งรหัส SMS แล้ว ให้ขอให้ผู้ใช้ยืนยันรหัสและใช้รหัสดังกล่าวเพื่อสร้าง
PhoneAuthCredentialดังนี้Swift
// Ask user for the verification code. Then: let credential = PhoneAuthProvider.provider().credential( withVerificationID: verificationId!, verificationCode: verificationCodeFromUser)Objective-C
// Ask user for the SMS verification code. Then: FIRPhoneAuthCredential *credential = [FIRPhoneAuthProvider.provider credentialWithVerificationID:verificationID verificationCode:verificationCodeFromUser];เริ่มต้นออบเจ็กต์การยืนยันด้วยข้อมูลเข้าสู่ระบบ ดังนี้
Swift
let assertion = PhoneMultiFactorGenerator.assertion(with: credential)Objective-C
FIRMultiFactorAssertion *assertion = [FIRPhoneMultiFactorGenerator assertionWithCredential:credential];แก้ไขการลงชื่อเข้าใช้ จากนั้นคุณจะเข้าถึงผลการลงชื่อเข้าใช้เดิมได้ ซึ่งรวมถึงข้อมูลเฉพาะของผู้ให้บริการมาตรฐานและข้อมูลรับรองการตรวจสอบสิทธิ์ ดังนี้
Swift
// Complete sign-in. This will also trigger the Auth state listeners. resolver.resolveSignIn(with: assertion) { (authResult, error) in // authResult will also contain the user, additionalUserInfo, optional // credential (null for email/password) associated with the first factor sign-in. // For example, if the user signed in with Google as a first factor, // authResult.additionalUserInfo will contain data related to Google provider that // the user signed in with. // user.credential contains the Google OAuth credential. // user.credential.accessToken contains the Google OAuth access token. // user.credential.idToken contains the Google OAuth ID token. }Objective-C
// Complete sign-in. [resolver resolveSignInWithAssertion:assertion completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error != nil) { // User successfully signed in with the second factor phone number. } }];
โค้ดด้านล่างแสดงตัวอย่างที่เสร็จสมบูรณ์ของการลงชื่อเข้าใช้ของผู้ใช้แบบหลายปัจจัย
Swift
Auth.auth().signIn(
withEmail: email,
password: password
) { (result, error) in
let authError = error as NSError?
if authError?.code == AuthErrorCode.secondFactorRequired.rawValue {
let resolver =
authError!.userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver
// Ask user which second factor to use.
// ...
// Then:
let hint = resolver.hints[selectedIndex] as! PhoneMultiFactorInfo
// Send SMS verification code
PhoneAuthProvider.provider().verifyPhoneNumber(
with: hint,
uiDelegate: nil,
multiFactorSession: resolver.session
) { (verificationId, error) in
if error != nil {
// Failed to verify phone number.
}
// Ask user for the SMS verification code.
// ...
// Then:
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationId!,
verificationCode: verificationCodeFromUser)
let assertion = PhoneMultiFactorGenerator.assertion(with: credential)
// Complete sign-in.
resolver.resolveSignIn(with: assertion) { (authResult, error) in
if error != nil {
// User successfully signed in with the second factor phone number.
}
}
}
}
}
Objective-C
[FIRAuth.auth signInWithEmail:email
password:password
completion:^(FIRAuthDataResult * _Nullable authResult,
NSError * _Nullable error) {
if (error == nil || error.code != FIRAuthErrorCodeSecondFactorRequired) {
// User is not enrolled with a second factor and is successfully signed in.
// ...
} else {
FIRMultiFactorResolver *resolver =
(FIRMultiFactorResolver *) error.userInfo[FIRAuthErrorUserInfoMultiFactorResolverKey];
// Ask user which second factor to use.
// ...
// Then:
FIRPhoneMultiFactorInfo *hint = (FIRPhoneMultiFactorInfo *) resolver.hints[selectedIndex];
// Send SMS verification code
[FIRPhoneAuthProvider.provider
verifyPhoneNumberWithMultiFactorInfo:hint
UIDelegate:nil
multiFactorSession:resolver.session
completion:^(NSString * _Nullable verificationID,
NSError * _Nullable error) {
if (error != nil) {
// Failed to verify phone number.
}
// Ask user for the SMS verification code.
// ...
// Then:
FIRPhoneAuthCredential *credential =
[FIRPhoneAuthProvider.provider
credentialWithVerificationID:verificationID
verificationCode:kPhoneSecondFactorVerificationCode];
FIRMultiFactorAssertion *assertion =
[FIRPhoneMultiFactorGenerator assertionWithCredential:credential];
// Complete sign-in.
[resolver resolveSignInWithAssertion:assertion
completion:^(FIRAuthDataResult * _Nullable authResult,
NSError * _Nullable error) {
if (error != nil) {
// User successfully signed in with the second factor phone number.
}
}];
}];
}
}];
ยินดีด้วย คุณลงชื่อเข้าใช้ให้ผู้ใช้โดยใช้การตรวจสอบสิทธิ์แบบหลายปัจจัยเรียบร้อยแล้ว
ขั้นตอนถัดไป
- จัดการผู้ใช้ที่ใช้การตรวจสอบสิทธิ์แบบหลายปัจจัยแบบเป็นโปรแกรมด้วย Admin SDK