-
-
Notifications
You must be signed in to change notification settings - Fork 214
feat(gotrue): Add phone mfa enrollment #1188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -28,39 +28,57 @@ class GoTrueMFAApi { | |||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
/// Starts the enrollment process for a new Multi-Factor Authentication (MFA) factor. | ||||||||||||||||||||||||||||||||
/// This method creates a new `unverified` factor. | ||||||||||||||||||||||||||||||||
/// To verify a factor, present the QR code or secret to the user and ask them to add it to their authenticator app. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// The user has to enter the code from their authenticator app to verify it. | ||||||||||||||||||||||||||||||||
/// For TOTP: To verify a factor, present the QR code or secret to the user and ask them to add it to their authenticator app. | ||||||||||||||||||||||||||||||||
/// For Phone: The user will receive an SMS with a verification code. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// The user has to enter the code from their authenticator app or SMS to verify it. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// Upon verifying a factor, all other sessions are logged out and the current session's authenticator level is promoted to `aal2`. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// [factorType] : Type of factor being enrolled. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// [issuer] : Domain which the user is enrolled with. | ||||||||||||||||||||||||||||||||
/// [issuer] : Domain which the user is enrolled with (TOTP only). | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// [friendlyName] : Human readable name assigned to the factor. | ||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||
/// [phone] : Phone number to enroll for Phone factor type. | ||||||||||||||||||||||||||||||||
Future<AuthMFAEnrollResponse> enroll({ | ||||||||||||||||||||||||||||||||
FactorType factorType = FactorType.totp, | ||||||||||||||||||||||||||||||||
String? issuer, | ||||||||||||||||||||||||||||||||
String? friendlyName, | ||||||||||||||||||||||||||||||||
String? phone, | ||||||||||||||||||||||||||||||||
}) async { | ||||||||||||||||||||||||||||||||
final session = _client.currentSession; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
final body = <String, dynamic>{ | ||||||||||||||||||||||||||||||||
'friendly_name': friendlyName, | ||||||||||||||||||||||||||||||||
'factor_type': factorType.name, | ||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (factorType == FactorType.totp) { | ||||||||||||||||||||||||||||||||
body['issuer'] = issuer; | ||||||||||||||||||||||||||||||||
} else if (factorType == FactorType.phone) { | ||||||||||||||||||||||||||||||||
if (phone == null) { | ||||||||||||||||||||||||||||||||
throw ArgumentError('Phone number is required for phone factor type'); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
body['phone'] = phone; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
Comment on lines
+59
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets validate that an issuer needs to be provided for the
Suggested change
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
final data = await _fetch.request( | ||||||||||||||||||||||||||||||||
'${_client._url}/factors', | ||||||||||||||||||||||||||||||||
RequestMethodType.post, | ||||||||||||||||||||||||||||||||
options: GotrueRequestOptions( | ||||||||||||||||||||||||||||||||
headers: _client._headers, | ||||||||||||||||||||||||||||||||
body: { | ||||||||||||||||||||||||||||||||
'friendly_name': friendlyName, | ||||||||||||||||||||||||||||||||
'factor_type': factorType.name, | ||||||||||||||||||||||||||||||||
'issuer': issuer, | ||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||
body: body, | ||||||||||||||||||||||||||||||||
jwt: session?.accessToken, | ||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
data['totp']['qr_code'] = | ||||||||||||||||||||||||||||||||
'data:image/svg+xml;utf-8,${data['totp']['qr_code']}'; | ||||||||||||||||||||||||||||||||
if (factorType == FactorType.totp && data['totp'] != null) { | ||||||||||||||||||||||||||||||||
data['totp']['qr_code'] = | ||||||||||||||||||||||||||||||||
'data:image/svg+xml;utf-8,${data['totp']['qr_code']}'; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
final response = AuthMFAEnrollResponse.fromJson(data); | ||||||||||||||||||||||||||||||||
return response; | ||||||||||||||||||||||||||||||||
|
@@ -150,8 +168,13 @@ class GoTrueMFAApi { | |||||||||||||||||||||||||||||||
factor.factorType == FactorType.totp && | ||||||||||||||||||||||||||||||||
factor.status == FactorStatus.verified) | ||||||||||||||||||||||||||||||||
.toList(); | ||||||||||||||||||||||||||||||||
final phone = factors | ||||||||||||||||||||||||||||||||
.where((factor) => | ||||||||||||||||||||||||||||||||
factor.factorType == FactorType.phone && | ||||||||||||||||||||||||||||||||
factor.status == FactorStatus.verified) | ||||||||||||||||||||||||||||||||
.toList(); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
return AuthMFAListFactorsResponse(all: factors, totp: totp); | ||||||||||||||||||||||||||||||||
return AuthMFAListFactorsResponse(all: factors, totp: totp, phone: phone); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
/// Returns the Authenticator Assurance Level (AAL) for the active session. | ||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,23 +4,33 @@ class AuthMFAEnrollResponse { | |
/// ID of the factor that was just enrolled (in an unverified state). | ||
final String id; | ||
|
||
/// Type of MFA factor. Only `[FactorType.totp] supported for now. | ||
/// Type of MFA factor. Supports both `[FactorType.totp]` and `[FactorType.phone]`. | ||
final FactorType type; | ||
|
||
/// TOTP enrollment information. | ||
final TOTPEnrollment totp; | ||
/// TOTP enrollment information (only present when type is totp). | ||
final TOTPEnrollment? totp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this isn't great, but I think we can call it that it's a fix. Open to suggestions to avoid this though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment in https://github.com/supabase/supabase-flutter/pull/1188/files#r2131807418 |
||
|
||
/// Phone enrollment information (only present when type is phone). | ||
final PhoneEnrollment? phone; | ||
|
||
const AuthMFAEnrollResponse({ | ||
required this.id, | ||
required this.type, | ||
required this.totp, | ||
this.totp, | ||
this.phone, | ||
}); | ||
|
||
factory AuthMFAEnrollResponse.fromJson(Map<String, dynamic> json) { | ||
final type = FactorType.values.firstWhere((e) => e.name == json['type']); | ||
return AuthMFAEnrollResponse( | ||
id: json['id'], | ||
type: FactorType.values.firstWhere((e) => e.name == json['type']), | ||
totp: TOTPEnrollment.fromJson(json['totp']), | ||
type: type, | ||
totp: type == FactorType.totp && json['totp'] != null | ||
? TOTPEnrollment.fromJson(json['totp']) | ||
: null, | ||
phone: type == FactorType.phone && json['phone'] != null | ||
? PhoneEnrollment._fromJsonValue(json['phone']) | ||
: null, | ||
); | ||
} | ||
} | ||
|
@@ -54,6 +64,34 @@ class TOTPEnrollment { | |
} | ||
} | ||
|
||
class PhoneEnrollment { | ||
/// The phone number that will receive the SMS OTP. | ||
final String phone; | ||
|
||
const PhoneEnrollment({ | ||
required this.phone, | ||
}); | ||
|
||
factory PhoneEnrollment.fromJson(Map<String, dynamic> json) { | ||
return PhoneEnrollment( | ||
phone: json['phone'], | ||
); | ||
} | ||
|
||
factory PhoneEnrollment._fromJsonValue(dynamic value) { | ||
if (value is String) { | ||
// Server returns phone number as a string directly | ||
return PhoneEnrollment(phone: value); | ||
} else if (value is Map<String, dynamic>) { | ||
// Server returns phone data as an object | ||
return PhoneEnrollment.fromJson(value); | ||
} else { | ||
throw ArgumentError( | ||
'Invalid phone enrollment data type: ${value.runtimeType}'); | ||
} | ||
} | ||
} | ||
|
||
class AuthMFAChallengeResponse { | ||
/// ID of the newly created challenge. | ||
final String id; | ||
|
@@ -120,8 +158,13 @@ class AuthMFAUnenrollResponse { | |
class AuthMFAListFactorsResponse { | ||
final List<Factor> all; | ||
final List<Factor> totp; | ||
final List<Factor> phone; | ||
|
||
AuthMFAListFactorsResponse({required this.all, required this.totp}); | ||
AuthMFAListFactorsResponse({ | ||
required this.all, | ||
required this.totp, | ||
required this.phone, | ||
}); | ||
} | ||
|
||
class AuthMFAAdminListFactorsResponse { | ||
|
@@ -151,7 +194,7 @@ class AuthMFAAdminDeleteFactorResponse { | |
|
||
enum FactorStatus { verified, unverified } | ||
|
||
enum FactorType { totp } | ||
enum FactorType { totp, phone } | ||
|
||
class Factor { | ||
/// ID of the factor. | ||
|
@@ -160,7 +203,7 @@ class Factor { | |
/// Friendly name of the factor, useful to disambiguate between multiple factors. | ||
final String? friendlyName; | ||
|
||
/// Type of factor. Only `totp` supported with this version but may change in future versions. | ||
/// Type of factor. Supports both `totp` and `phone`. | ||
final FactorType factorType; | ||
|
||
/// Factor's status. | ||
|
Uh oh!
There was an error while loading. Please reload this page.