-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
multi_factor.dart
112 lines (95 loc) 路 4.04 KB
/
multi_factor.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
part of firebase_auth;
/// Defines multi-factor related properties and operations pertaining to a [User].
/// This class acts as the main entry point for enrolling or un-enrolling
/// second factors for a user, and provides access to their currently enrolled factors.
class MultiFactor {
MultiFactorPlatform _delegate;
MultiFactor._(this._delegate);
/// Returns a session identifier for a second factor enrollment operation.
Future<MultiFactorSession> getSession() {
return _delegate.getSession();
}
/// Enrolls a second factor as identified by the [MultiFactorAssertion] parameter for the current user.
///
/// [displayName] can be used to provide a display name for the second factor.
Future<void> enroll(
MultiFactorAssertion assertion, {
String? displayName,
}) async {
return _delegate.enroll(assertion._delegate, displayName: displayName);
}
/// Unenrolls a second factor from this user.
///
/// [factorUid] is the unique identifier of the second factor to unenroll.
/// [multiFactorInfo] is the [MultiFactorInfo] of the second factor to unenroll.
/// Only one of [factorUid] or [multiFactorInfo] should be provided.
Future<void> unenroll({String? factorUid, MultiFactorInfo? multiFactorInfo}) {
return _delegate.unenroll(
factorUid: factorUid,
multiFactorInfo: multiFactorInfo,
);
}
/// Returns a list of the [MultiFactorInfo] already associated with this user.
Future<List<MultiFactorInfo>> getEnrolledFactors() {
return _delegate.getEnrolledFactors();
}
}
/// Provider for generating a PhoneMultiFactorAssertion.
class PhoneMultiFactorGenerator {
/// Transforms a PhoneAuthCredential into a [MultiFactorAssertion]
/// which can be used to confirm ownership of a phone second factor.
static MultiFactorAssertion getAssertion(
PhoneAuthCredential credential,
) {
final assertion =
PhoneMultiFactorGeneratorPlatform.instance.getAssertion(credential);
return MultiFactorAssertion._(assertion);
}
}
/// Represents an assertion that the Firebase Authentication server
/// can use to authenticate a user as part of a multi-factor flow.
class MultiFactorAssertion {
final MultiFactorAssertionPlatform _delegate;
MultiFactorAssertion._(this._delegate) {
MultiFactorAssertionPlatform.verifyExtends(_delegate);
}
}
/// Utility class that contains methods to resolve second factor
/// requirements on users that have opted into two-factor authentication.
class MultiFactorResolver {
final FirebaseAuth _auth;
final MultiFactorResolverPlatform _delegate;
MultiFactorResolver._(this._auth, this._delegate) {
MultiFactorResolverPlatform.verifyExtends(_delegate);
}
/// List of [MultiFactorInfo] which represents the available
/// second factors that can be used to complete the sign-in for the current session.
List<MultiFactorInfo> get hints => _delegate.hints;
/// A MultiFactorSession, an opaque session identifier for the current sign-in flow.
MultiFactorSession get session => _delegate.session;
/// Completes sign in with a second factor using an MultiFactorAssertion which
/// confirms that the user has successfully completed the second factor challenge.
Future<UserCredential> resolveSignIn(
MultiFactorAssertion assertion,
) async {
final credential = await _delegate.resolveSignIn(assertion._delegate);
return UserCredential._(_auth, credential);
}
}
/// MultiFactor exception related to Firebase Authentication. Check the error code
/// and message for more details.
class FirebaseAuthMultiFactorException extends FirebaseAuthException {
final FirebaseAuth _auth;
final FirebaseAuthMultiFactorExceptionPlatform _delegate;
FirebaseAuthMultiFactorException._(this._auth, this._delegate)
: super(
code: _delegate.code,
message: _delegate.message,
email: _delegate.email,
credential: _delegate.credential,
phoneNumber: _delegate.phoneNumber,
tenantId: _delegate.tenantId,
);
MultiFactorResolver get resolver =>
MultiFactorResolver._(_auth, _delegate.resolver);
}