Skip to content

Commit

Permalink
fixup!:
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda committed Aug 18, 2024
1 parent 9b7c338 commit 05efad7
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ include: package:neon_lints/dart.yaml
custom_lint:
rules:
- avoid_exports: false
- avoid_dart_io: false
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,17 @@ class AccountRepository {
final http.Client _httpClient;
final AccountStorage _storage;

(NextcloudClient, core.ClientFlowLoginV2PollRequestApplicationJson)? _flow;

final BehaviorSubject<({String? active, BuiltMap<String, Account> accounts})> _accounts =
BehaviorSubject.seeded((active: null, accounts: BuiltMap()));

/// A stream of account information.
///
/// The initial state of the stream will be `(active: null, accounts: BuiltMap())`.
Stream<({String? active, BuiltList<Account> accounts})> get accounts => _accounts.stream.map((e) {
Stream<({Account? active, BuiltList<Account> accounts})> get accounts => _accounts.stream.map((e) {
final active = e.accounts[e.active];
final accounts = e.accounts.values.toBuiltList();

return (active: e.active, accounts: accounts);
return (active: active, accounts: accounts);
}).asBroadcastStream();

/// Whether accounts are logged in.
Expand Down Expand Up @@ -157,7 +156,6 @@ class AccountRepository {
);
}

// TODO: this doesn't really belong here.
/// Fetches the status of the server.
///
/// May throw a [FetchStatusFailure].
Expand All @@ -179,7 +177,7 @@ class AccountRepository {
/// Initializes the Nextcloud login flow.
///
/// May throw a [InitLoginFailure].
Future<Uri> loginFlowInit(Uri serverURL) async {
Future<(Uri loginUrl, String token)> loginFlowInit(Uri serverURL) async {
final client = buildUnAuthenticatedClient(
httpClient: _httpClient,
userAgent: _userAgent,
Expand All @@ -189,35 +187,34 @@ class AccountRepository {
try {
final initResponse = await client.authentication.clientFlowLoginV2.init();

final pollBody = core.ClientFlowLoginV2PollRequestApplicationJson((b) {
b.token = initResponse.body.poll.token;
});
_flow = (client, pollBody);
final loginUrl = Uri.parse(initResponse.body.login);
final token = initResponse.body.poll.token;

return Uri.parse(initResponse.body.login);
return (loginUrl, token);
} on http.ClientException catch (error, stackTrace) {
Error.throwWithStackTrace(InitLoginFailure(error), stackTrace);
}
}

// TODO: update docs
/// Polls the login flow endpoint and returns the retrieved credentials.
///
/// Throws a [StateError] if the login flow has not been initialized with [loginFlowInit].
/// Throws a [PollLoginFailure] when polling the endpoint fails. The flow must be initialized
/// again if a failure occurs.
Future<Credentials?> loginFlowPoll() async {
final flow = _flow;
if (flow == null) {
throw StateError('No login flow initialized');
}
final (client, body) = flow;
Future<Credentials?> loginFlowPoll(Uri serverURL, String token) async {
final client = buildUnAuthenticatedClient(
httpClient: _httpClient,
userAgent: _userAgent,
serverURL: serverURL,
);

final body = core.ClientFlowLoginV2PollRequestApplicationJson((b) {
b.token = token;
});

try {
final resultResponse = await client.authentication.clientFlowLoginV2.poll($body: body);

final response = resultResponse.body;
_flow = null;
return Credentials((b) {
b
..serverURL = Uri.parse(response.server)
Expand All @@ -229,7 +226,6 @@ class AccountRepository {
return null;
}

_flow = null;
Error.throwWithStackTrace(PollLoginFailure(error), stackTrace);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ abstract class Account implements Built<Account, AccountBuilder> {
String get username => credentials.username;

/// App password.
String? get password => credentials.password;

/// The unique ID of the account.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:nextcloud/core.dart' as nc_core;
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/provisioning_api.dart' as nc_provisioning_api;

/// A AuthenticationClient client mock for testing.
/// An AuthenticationClient client mock for testing.
@internal
@visibleForTesting
AuthenticationClient? mockedClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ publish_to: none

environment:
sdk: ^3.0.0
flutter: ^3.22.0

dependencies:
built_collection: ^5.1.1
Expand Down
Loading

0 comments on commit 05efad7

Please sign in to comment.