forked from jellyflix-app/jellyflix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_service.dart
215 lines (180 loc) · 6.08 KB
/
auth_service.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import 'dart:async';
import 'package:jellyflix/models/user.dart';
import 'package:jellyflix/services/api_service.dart';
import 'package:jellyflix/services/database_service.dart';
class AuthService {
final ApiService _apiService;
final DatabaseService _databaseService;
final StreamController<bool> _authStateStream = StreamController();
Stream<bool> get authStateChange => _authStateStream.stream;
Future<bool> get isAuthenticated => _authStateStream.stream.last;
User? get currentProfile => _apiService.currentUser;
AuthService(
{required ApiService apiService,
required DatabaseService databaseService})
: _apiService = apiService,
_databaseService = databaseService {
_authStateStream.add(false);
checkAuthentication().then((value) {
_authStateStream.add(value);
});
}
Future<bool> checkAuthentication() async {
bool authenticated = await _apiService.checkAuthentication();
if (authenticated) {
_authStateStream.add(true);
return true;
} else {
String? profileId = currentProfileid();
if (profileId == null) {
_authStateStream.add(false);
return false;
}
User? user = _databaseService.get(profileId);
try {
await login(user!);
_authStateStream.add(true);
return true;
} catch (_) {
_authStateStream.add(false);
return false;
}
}
}
Future<User> login(User user) async {
final (url, candidates) = await inferServerUrl(user.serverAdress!);
if (url == null) {
throw Exception('No valid server found on the given url\n'
'\nTried the following urls:\n'
'${candidates.join('\n-------------\n')}');
}
user = await _apiService.login(
url,
user.name!,
user.password!,
);
_databaseService.put(user.id! + user.serverAdress!, user);
_databaseService.put("currentProfileId", user.id! + user.serverAdress!);
_authStateStream.add(true);
return user;
}
/// infer the server URL based on the provided incomplete URL.
Future<(String?, List<String>)> inferServerUrl(String url) async {
final candidates = generateUrlCandidates(url);
for (final url in candidates) {
if (await _isValidJellyfinServer(url)) {
return (url, <String>[]);
}
}
return (null, candidates);
}
Future<bool> _isValidJellyfinServer(String url) async =>
await _apiService.ping(user: User(serverAdress: url)) ?? false;
void updateCurrentProfileId(String? profileId) async {
if (profileId == null) {
_databaseService.delete("currentProfileId");
}
_databaseService.put("currentProfileId", profileId);
}
Future<void> logout() async {
await _apiService.logout();
_authStateStream.add(false);
}
Future<void> logoutAndDeleteProfile({String? profileId}) async {
await logout();
profileId ??= currentProfileid();
_databaseService.delete(profileId!);
_databaseService.delete("currentProfileId");
}
Future switchProfile(String profileId) async {
User? user = _databaseService.get(profileId);
if (user != null &&
user.serverAdress != null &&
user.name != null &&
user.password != null) {
await _apiService.login(user.serverAdress!, user.name!, user.password!);
_authStateStream.add(true);
} else {
throw Exception("Profile not found");
}
}
String? currentProfileid() {
String? currentProfileIndexString =
_databaseService.get("currentProfileId");
return currentProfileIndexString;
}
Future<List<User>> getAllProfiles() async {
Map allValues = await _databaseService.getAll();
allValues.remove("currentProfileId");
List<User> profiles = allValues.values.map((e) => e as User).toList();
return profiles;
}
Future<bool?> checkServerReachable({String? profileId}) async {
// get latest element from the stream
profileId ??= currentProfileid();
if (profileId == null) {
return null;
}
User? user = _databaseService.get(profileId);
return await _apiService.ping(user: user);
}
}
/// Function to generate URL candidates based on the input URL.
List<String> generateUrlCandidates(String input) {
if (input.endsWith('/')) {
input = input.substring(0, input.length - 1);
}
final result = parseUrl(input);
if (result == null) return [];
final (scheme, host, port, path) = result;
List<String> protoCandidates = [];
List<String> supportedProtos = ['https:', 'http:'];
if (scheme.isNotEmpty) {
protoCandidates.add('$scheme//$host');
} else {
// The user did not declare a protocol
for (String proto in supportedProtos) {
protoCandidates.add('$proto//$host');
}
}
List<String> finalCandidates = [];
if (port.isNotEmpty) {
for (String candidate in protoCandidates) {
finalCandidates.add('$candidate:$port$path');
}
} else {
// The port wasn't declared, so use default Jellyfin and protocol ports
for (final finalUrl in protoCandidates) {
// add url without port
finalCandidates.add('$finalUrl$path');
// Jellyfin defaults
if (finalUrl.startsWith('https')) {
finalCandidates.add('$finalUrl:8920$path');
} else if (finalUrl.startsWith('http')) {
finalCandidates.add('$finalUrl:8096$path');
}
}
}
return finalCandidates;
}
/// parse url and separate it into its components
/// if you are wondering why we don't use Uri.tryParse() it cannot parse ipv4 or ipv6 addresses
(String, String, String, String)? parseUrl(String input) {
if (!(input.startsWith('http://') || input.startsWith('https://'))) {
// fill in a empty protocol, so regex matches
input = 'none://$input';
}
final rgx = RegExp(r'^(.*:)//([A-Za-z0-9\-.]+)(:[0-9]+)?(.*)$');
final match = rgx.firstMatch(input);
if (match != null) {
var scheme = match.group(1) ?? '';
final body = match.group(2) ?? '';
final port = match.group(3)?.substring(1) ?? ''; // Remove leading colon
final path = match.group(4) ?? '';
if (scheme == 'none:') {
scheme = '';
}
return (scheme, body, port, path);
}
return null;
}