unified code verification service

This commit is contained in:
Stephan D
2026-02-10 01:55:33 +01:00
parent 76c3bfdea9
commit 7f540671c1
120 changed files with 1863 additions and 1394 deletions

View File

@@ -1,39 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/models/confirmation_target.dart';
import 'package:pshared/api/requests/tokens/session_identifier.dart';
part 'login_confirmation.g.dart';
@JsonSerializable(explicitToJson: true)
class LoginConfirmationRequest {
final ConfirmationTarget target;
final String? destination;
const LoginConfirmationRequest({
this.target = ConfirmationTarget.login,
this.destination,
});
factory LoginConfirmationRequest.fromJson(Map<String, dynamic> json) => _$LoginConfirmationRequestFromJson(json);
Map<String, dynamic> toJson() => _$LoginConfirmationRequestToJson(this);
}
@JsonSerializable(explicitToJson: true)
class LoginConfirmationVerifyRequest {
final ConfirmationTarget target;
final String code;
final String? destination;
final SessionIdentifierDto sessionIdentifier;
const LoginConfirmationVerifyRequest({
this.target = ConfirmationTarget.login,
required this.code,
this.destination,
required this.sessionIdentifier,
});
factory LoginConfirmationVerifyRequest.fromJson(Map<String, dynamic> json) => _$LoginConfirmationVerifyRequestFromJson(json);
Map<String, dynamic> toJson() => _$LoginConfirmationVerifyRequestToJson(this);
}

View File

@@ -4,15 +4,15 @@ part 'session_identifier.g.dart';
@JsonSerializable()
class SessionIdentifierDto {
class SessionIdentifierDTO {
final String clientId;
final String deviceId;
const SessionIdentifierDto({
const SessionIdentifierDTO({
required this.clientId,
required this.deviceId,
});
factory SessionIdentifierDto.fromJson(Map<String, dynamic> json) => _$SessionIdentifierDtoFromJson(json);
Map<String, dynamic> toJson() => _$SessionIdentifierDtoToJson(this);
factory SessionIdentifierDTO.fromJson(Map<String, dynamic> json) => _$SessionIdentifierDTOFromJson(json);
Map<String, dynamic> toJson() => _$SessionIdentifierDTOToJson(this);
}

View File

@@ -0,0 +1,41 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:pshared/models/verification/purpose.dart';
import 'package:pshared/api/requests/tokens/session_identifier.dart';
part 'login.g.dart';
@JsonSerializable(explicitToJson: true)
class LoginVerificationRequest {
final VerificationPurpose purpose;
final String? target;
final String idempotencyKey;
const LoginVerificationRequest({
this.purpose = VerificationPurpose.login,
this.target,
required this.idempotencyKey,
});
factory LoginVerificationRequest.fromJson(Map<String, dynamic> json) => _$LoginVerificationRequestFromJson(json);
Map<String, dynamic> toJson() => _$LoginVerificationRequestToJson(this);
}
@JsonSerializable(explicitToJson: true)
class LoginCodeVerifyicationRequest extends LoginVerificationRequest {
final String code;
final SessionIdentifierDTO sessionIdentifier;
const LoginCodeVerifyicationRequest({
super.purpose = VerificationPurpose.login,
super.target,
required super.idempotencyKey,
required this.code,
required this.sessionIdentifier,
});
factory LoginCodeVerifyicationRequest.fromJson(Map<String, dynamic> json) => _$LoginCodeVerifyicationRequestFromJson(json);
@override
Map<String, dynamic> toJson() => _$LoginCodeVerifyicationRequestToJson(this);
}

View File

@@ -0,0 +1,22 @@
import 'package:json_annotation/json_annotation.dart';
part 'resend.g.dart';
@JsonSerializable()
class ResendVerificationEmailRequest {
final String login;
const ResendVerificationEmailRequest({
required this.login,
});
factory ResendVerificationEmailRequest.fromJson(Map<String, dynamic> json) => _$ResendVerificationEmailRequestFromJson(json);
Map<String, dynamic> toJson() => _$ResendVerificationEmailRequestToJson(this);
static ResendVerificationEmailRequest build({
required String login,
}) => ResendVerificationEmailRequest(login: login);
}

View File

@@ -11,13 +11,11 @@ class PendingLoginResponse {
final AccountResponse account;
final TokenData pendingToken;
final String destination;
final int ttlSeconds;
const PendingLoginResponse({
required this.account,
required this.pendingToken,
required this.destination,
required this.ttlSeconds,
});
factory PendingLoginResponse.fromJson(Map<String, dynamic> json) => _$PendingLoginResponseFromJson(json);

View File

@@ -1,27 +1,29 @@
import 'package:json_annotation/json_annotation.dart';
part 'confirmation.g.dart';
part 'response.g.dart';
@JsonSerializable()
class ConfirmationResponse {
class VerificationResponse {
@JsonKey(name: 'ttl_seconds', defaultValue: 0)
final int ttlSeconds;
@JsonKey(name: 'cooldown_seconds', defaultValue: 0)
final int cooldownSeconds;
@JsonKey(defaultValue: '')
final String destination;
final String idempotencyKey;
const ConfirmationResponse({
const VerificationResponse({
required this.ttlSeconds,
required this.cooldownSeconds,
required this.destination,
required this.idempotencyKey,
});
Duration get cooldownDuration => Duration(seconds: cooldownSeconds);
Duration get ttlDuration => Duration(seconds: ttlSeconds);
factory ConfirmationResponse.fromJson(Map<String, dynamic> json) => _$ConfirmationResponseFromJson(json);
factory VerificationResponse.fromJson(Map<String, dynamic> json) => _$VerificationResponseFromJson(json);
Map<String, dynamic> toJson() => _$ConfirmationResponseToJson(this);
Map<String, dynamic> toJson() => _$VerificationResponseToJson(this);
}

View File

@@ -2,13 +2,13 @@ import 'package:pshared/api/requests/tokens/session_identifier.dart';
import 'package:pshared/models/session_identifier.dart';
extension SessionIdentifierMapper on SessionIdentifier {
SessionIdentifierDto toDTO() => SessionIdentifierDto(
SessionIdentifierDTO toDTO() => SessionIdentifierDTO(
clientId: clientId,
deviceId: deviceId,
);
}
extension SessionIdentifierDtoMapper on SessionIdentifierDto {
extension SessionIdentifierDtoMapper on SessionIdentifierDTO {
SessionIdentifier toDomain() => SessionIdentifier(
clientId: clientId,
deviceId: deviceId,

View File

@@ -9,19 +9,22 @@ class PendingLogin {
final Account account;
final TokenData pendingToken;
final String destination;
final int ttlSeconds;
final SessionIdentifier session;
final int? ttlSeconds;
final int? cooldownSeconds;
final DateTime? cooldownUntil;
final String? idempotencyKey;
const PendingLogin({
required this.account,
required this.pendingToken,
required this.destination,
required this.ttlSeconds,
this.ttlSeconds,
required this.session,
this.cooldownSeconds,
this.cooldownUntil,
this.idempotencyKey,
});
factory PendingLogin.fromResponse(
@@ -31,7 +34,6 @@ class PendingLogin {
account: response.account.account.toDomain(),
pendingToken: response.pendingToken,
destination: response.destination,
ttlSeconds: response.ttlSeconds,
session: session,
);
@@ -44,15 +46,17 @@ class PendingLogin {
int? cooldownSeconds,
DateTime? cooldownUntil,
bool clearCooldown = false,
String? idempotencyKey,
}) {
return PendingLogin(
account: account ?? this.account,
pendingToken: pendingToken ?? this.pendingToken,
destination: destination ?? this.destination,
ttlSeconds: ttlSeconds ?? this.ttlSeconds,
ttlSeconds: ttlSeconds ?? this.cooldownSeconds,
session: session ?? this.session,
cooldownSeconds: clearCooldown ? null : cooldownSeconds ?? this.cooldownSeconds,
cooldownUntil: clearCooldown ? null : cooldownUntil ?? this.cooldownUntil,
idempotencyKey: idempotencyKey ?? this.idempotencyKey,
);
}

View File

@@ -3,9 +3,15 @@ import 'package:json_annotation/json_annotation.dart';
/// Targets for confirmation codes.
@JsonEnum(alwaysCreate: true)
enum ConfirmationTarget {
enum VerificationPurpose {
@JsonValue('login')
login,
@JsonValue('payout')
payout,
@JsonValue('account_activation')
accountActivation,
@JsonValue('email_change')
emailChange,
@JsonValue('password_reset')
passwordReset,
}

View File

@@ -7,7 +7,7 @@ import 'package:share_plus/share_plus.dart';
import 'package:pshared/api/errors/unauthorized.dart';
import 'package:pshared/api/requests/signup.dart';
import 'package:pshared/api/requests/login_data.dart';
import 'package:pshared/api/responses/confirmation.dart';
import 'package:pshared/api/responses/verification/response.dart';
import 'package:pshared/config/constants.dart';
import 'package:pshared/models/account/account.dart';
import 'package:pshared/models/auth/login_outcome.dart';
@@ -93,16 +93,16 @@ class AccountProvider extends ChangeNotifier {
password: password,
locale: locale,
));
if (outcome.account != null) {
if (outcome.isCompleted) {
_authState = AuthState.ready;
_setResource(Resource(data: outcome.account, isLoading: false));
_pickupLocale(outcome.account!.locale);
} else {
final pending = outcome.pending;
if (pending == null) {
throw Exception('Pending login data is missing');
if (!outcome.isPending || pending == null) {
throw StateError('Pending login data is missing');
}
final confirmation = await VerificationService.requestLoginCode(pending);
final confirmation = await VerificationService.requestLoginCode(pending, target: email);
_pendingLogin = _applyConfirmationMeta(pending, confirmation);
_authState = AuthState.idle;
_setResource(_resource.copyWith(isLoading: false));
@@ -115,7 +115,10 @@ class AccountProvider extends ChangeNotifier {
}
}
PendingLogin _applyConfirmationMeta(PendingLogin pending, ConfirmationResponse confirmation) {
PendingLogin _applyConfirmationMeta(
PendingLogin pending,
VerificationResponse confirmation,
) {
final ttlSeconds = confirmation.ttlSeconds != 0 ? confirmation.ttlSeconds : pending.ttlSeconds;
final destination = confirmation.destination.isNotEmpty ? confirmation.destination : pending.destination;
final cooldownSeconds = confirmation.cooldownSeconds;
@@ -126,10 +129,11 @@ class AccountProvider extends ChangeNotifier {
cooldownSeconds: cooldownSeconds > 0 ? cooldownSeconds : null,
cooldownUntil: cooldownSeconds > 0 ? DateTime.now().add(confirmation.cooldownDuration) : null,
clearCooldown: cooldownSeconds <= 0,
idempotencyKey: confirmation.idempotencyKey,
);
}
void updatePendingLogin(ConfirmationResponse confirmation) {
void updatePendingLogin(VerificationResponse confirmation) {
final pending = _pendingLogin;
if (pending == null) return;
_pendingLogin = _applyConfirmationMeta(pending, confirmation);

View File

@@ -1,4 +1,5 @@
import 'package:logging/logging.dart';
import 'package:pshared/api/requests/verification/resend.dart';
import 'package:pshared/service/device_id.dart';
import 'package:share_plus/share_plus.dart';
@@ -59,7 +60,7 @@ class AccountService {
static Future<void> resendVerificationEmail(String email) async {
_logger.fine('Resending verification email');
await getPUTResponse(_objectType, 'email', {'login': email});
await getPUTResponse(_objectType, 'email', ResendVerificationEmailRequest.build(login: email).toJson());
}
static Future<void> verifyEmail(String token) async {

View File

@@ -1,7 +1,6 @@
class Services {
static const String account = 'accounts';
static const String authorization = 'authorization';
static const String confirmations = 'confirmations';
static const String device = 'device';
static const String invitations = 'invitations';
static const String organization = 'organizations';
@@ -9,6 +8,7 @@ class Services {
static const String storage = 'storage';
static const String chainWallets = 'chain_wallets';
static const String ledger = 'ledger_accounts';
static const String verification = 'verification';
static const String recipients = 'recipients';
static const String paymentMethods = 'payment_methods';

View File

@@ -1,11 +1,13 @@
import 'package:logging/logging.dart';
import 'package:pshared/api/requests/confirmations/login_confirmation.dart';
import 'package:uuid/uuid.dart';
import 'package:pshared/api/requests/verification/login.dart';
import 'package:pshared/api/responses/login.dart';
import 'package:pshared/api/responses/verification/response.dart';
import 'package:pshared/data/mapper/account/account.dart';
import 'package:pshared/data/mapper/session_identifier.dart';
import 'package:pshared/models/account/account.dart';
import 'package:pshared/data/mapper/account/account.dart';
import 'package:pshared/api/responses/confirmation.dart';
import 'package:pshared/models/auth/pending_login.dart';
import 'package:pshared/service/authorization/storage.dart';
import 'package:pshared/service/services.dart';
@@ -14,43 +16,50 @@ import 'package:pshared/utils/http/requests.dart';
class VerificationService {
static final _logger = Logger('service.verification');
static const String _objectType = Services.confirmations;
static const String _objectType = Services.verification;
static Future<ConfirmationResponse> requestLoginCode(PendingLogin pending, {String? destination}) async {
static Future<VerificationResponse> requestLoginCode(PendingLogin pending, {String? target}) async {
_logger.fine('Requesting login confirmation code');
final response = await getPOSTResponse(
_objectType,
'',
LoginConfirmationRequest(destination: destination).toJson(),
LoginVerificationRequest(
target: target,
idempotencyKey: Uuid().v4(),
).toJson(),
authToken: pending.pendingToken.token,
);
return ConfirmationResponse.fromJson(response);
return VerificationResponse.fromJson(response);
}
static Future<ConfirmationResponse> resendLoginCode(PendingLogin pending, {String? destination}) async {
static Future<VerificationResponse> resendLoginCode(PendingLogin pending, {String? destination}) async {
_logger.fine('Resending login confirmation code');
final response = await getPOSTResponse(
_objectType,
'/resend',
LoginConfirmationRequest(destination: destination).toJson(),
LoginVerificationRequest(
target: destination,
idempotencyKey: pending.idempotencyKey ?? Uuid().v4(),
).toJson(),
authToken: pending.pendingToken.token,
);
return ConfirmationResponse.fromJson(response);
return VerificationResponse.fromJson(response);
}
static Future<Account> confirmLoginCode({
required PendingLogin pending,
required String code,
String? destination,
String? target,
}) async {
_logger.fine('Confirming login code');
final response = await getPOSTResponse(
_objectType,
'/verify',
LoginConfirmationVerifyRequest(
LoginCodeVerifyicationRequest(
code: code,
destination: destination,
target: target,
sessionIdentifier: pending.session.toDTO(),
idempotencyKey: pending.idempotencyKey ?? Uuid().v4(),
).toJson(),
authToken: pending.pendingToken.token,
);