mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-10 08:07:59 -06:00
Externalized API and models as own package
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:paperless_mobile/core/model/error_message.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/core/store/local_vault.dart';
|
||||
import 'package:paperless_mobile/di_initializer.dart';
|
||||
import 'package:paperless_mobile/features/login/model/authentication_information.dart';
|
||||
@@ -10,18 +10,19 @@ import 'package:paperless_mobile/features/login/model/client_certificate.dart';
|
||||
import 'package:paperless_mobile/features/login/model/user_credentials.model.dart';
|
||||
import 'package:paperless_mobile/features/login/services/authentication.service.dart';
|
||||
import 'package:paperless_mobile/features/settings/model/application_settings_state.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
|
||||
const authenticationKey = "authentication";
|
||||
|
||||
@singleton
|
||||
class AuthenticationCubit extends Cubit<AuthenticationState> {
|
||||
final LocalAuthenticationService _localAuthService;
|
||||
final PaperlessAuthenticationApi _authApi;
|
||||
final LocalVault localStore;
|
||||
final AuthenticationService authenticationService;
|
||||
|
||||
AuthenticationCubit(
|
||||
this.localStore,
|
||||
this.authenticationService,
|
||||
this._localAuthService,
|
||||
this._authApi,
|
||||
) : super(AuthenticationState.initial);
|
||||
|
||||
Future<void> initialize() {
|
||||
@@ -49,7 +50,7 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
|
||||
),
|
||||
),
|
||||
);
|
||||
final token = await authenticationService.login(
|
||||
final token = await _authApi.login(
|
||||
username: credentials.username!,
|
||||
password: credentials.password!,
|
||||
serverUrl: serverUrl,
|
||||
@@ -70,14 +71,14 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
|
||||
authentication: auth,
|
||||
));
|
||||
} on TlsException catch (_) {
|
||||
const error =
|
||||
ErrorMessage(ErrorCode.invalidClientCertificateConfiguration);
|
||||
const error = PaperlessServerException(
|
||||
ErrorCode.invalidClientCertificateConfiguration);
|
||||
throw error;
|
||||
} on SocketException catch (err) {
|
||||
if (err.message.contains("connection timed out")) {
|
||||
throw const ErrorMessage(ErrorCode.requestTimedOut);
|
||||
throw const PaperlessServerException(ErrorCode.requestTimedOut);
|
||||
} else {
|
||||
throw ErrorMessage.unknown();
|
||||
throw const PaperlessServerException.unknown();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,7 +96,7 @@ class AuthenticationCubit extends Cubit<AuthenticationState> {
|
||||
emit(AuthenticationState(isAuthenticated: false, wasLoginStored: false));
|
||||
} else {
|
||||
if (!appSettings.isLocalAuthenticationEnabled ||
|
||||
await authenticationService
|
||||
await _localAuthService
|
||||
.authenticateLocalUser("Authenticate to log back in")) {
|
||||
registerSecurityContext(storedAuth.clientCertificate);
|
||||
emit(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:paperless_mobile/core/model/error_message.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/di_initializer.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
|
||||
@@ -12,7 +12,8 @@ class LocalAuthenticationCubit extends Cubit<LocalAuthenticationState> {
|
||||
if (isAuthenticationSuccessful) {
|
||||
emit(LocalAuthenticationState(true));
|
||||
} else {
|
||||
throw const ErrorMessage(ErrorCode.biometricAuthenticationFailed);
|
||||
throw const PaperlessServerException(
|
||||
ErrorCode.biometricAuthenticationFailed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +1,17 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:paperless_mobile/core/model/error_message.dart';
|
||||
import 'package:paperless_mobile/core/store/local_vault.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
import 'package:paperless_mobile/core/store/local_vault.dart';
|
||||
|
||||
@singleton
|
||||
class AuthenticationService {
|
||||
final BaseClient httpClient;
|
||||
class LocalAuthenticationService {
|
||||
final LocalVault localStore;
|
||||
final LocalAuthentication localAuthentication;
|
||||
|
||||
AuthenticationService(
|
||||
LocalAuthenticationService(
|
||||
this.localStore,
|
||||
this.localAuthentication,
|
||||
@Named("timeoutClient") this.httpClient,
|
||||
);
|
||||
|
||||
///
|
||||
/// Returns the authentication token.
|
||||
///
|
||||
Future<String> login({
|
||||
required String username,
|
||||
required String password,
|
||||
required String serverUrl,
|
||||
}) async {
|
||||
late Response response;
|
||||
try {
|
||||
response = await httpClient.post(
|
||||
Uri.parse("/api/token/"),
|
||||
body: {"username": username, "password": password},
|
||||
);
|
||||
} on FormatException catch (e) {
|
||||
final source = e.source;
|
||||
if (source is String &&
|
||||
source.contains("400 No required SSL certificate was sent")) {
|
||||
throw const ErrorMessage(ErrorCode.missingClientCertificate);
|
||||
}
|
||||
}
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(utf8.decode(response.bodyBytes));
|
||||
return data['token'];
|
||||
} else if (response.statusCode == 400 &&
|
||||
response.body
|
||||
.toLowerCase()
|
||||
.contains("no required certificate was sent")) {
|
||||
throw const ErrorMessage(ErrorCode.invalidClientCertificateConfiguration);
|
||||
} else {
|
||||
throw const ErrorMessage(ErrorCode.authenticationFailed);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> authenticateLocalUser(String localizedReason) async {
|
||||
if (await localAuthentication.isDeviceSupported()) {
|
||||
return await localAuthentication.authenticate(
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_form_builder/flutter_form_builder.dart';
|
||||
import 'package:paperless_mobile/core/model/error_message.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/extensions/flutter_extensions.dart';
|
||||
import 'package:paperless_mobile/features/login/bloc/authentication_cubit.dart';
|
||||
import 'package:paperless_mobile/features/login/view/widgets/client_certificate_form_field.dart';
|
||||
@@ -97,12 +97,12 @@ class _LoginPageState extends State<LoginPage> {
|
||||
clientCertificate:
|
||||
form[ClientCertificateFormField.fkClientCertificate],
|
||||
);
|
||||
} on ErrorMessage catch (error, stackTrace) {
|
||||
} on PaperlessServerException catch (error, stackTrace) {
|
||||
showErrorMessage(context, error, stackTrace);
|
||||
} on Map<String, dynamic> catch (error, stackTrace) {
|
||||
showGenericError(context, error.values.first, stackTrace);
|
||||
} catch (unknownError, stackTrace) {
|
||||
showErrorMessage(context, ErrorMessage.unknown(), stackTrace);
|
||||
showGenericError(context, unknownError.toString(), stackTrace);
|
||||
} finally {
|
||||
setState(() => _isLoginLoading = false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user