6 changed files with 152 additions and 56 deletions
@ -1,3 +1,3 @@
|
||||
export 'package:neon/src/models/account.dart'; |
||||
export 'package:neon/src/models/account.dart' hide Credentials, LoginQrcode; |
||||
export 'package:neon/src/models/app_implementation.dart'; |
||||
export 'package:neon/src/models/notifications_interface.dart'; |
||||
|
@ -1,47 +0,0 @@
|
||||
import 'package:meta/meta.dart'; |
||||
|
||||
@internal |
||||
class LoginQrcode { |
||||
LoginQrcode({ |
||||
required this.server, |
||||
required this.user, |
||||
required this.password, |
||||
}); |
||||
|
||||
static final _loginQrcodeUrlRegex = RegExp(r'^nc://login/user:(.*)&password:(.*)&server:(.*)$'); |
||||
static final _loginQrcodePathRegex = RegExp(r'^/user:(.*)&password:(.*)&server:(.*)$'); |
||||
|
||||
static LoginQrcode parse(final String url) { |
||||
for (final regex in [_loginQrcodeUrlRegex, _loginQrcodePathRegex]) { |
||||
final matches = regex.allMatches(url); |
||||
if (matches.isEmpty) { |
||||
continue; |
||||
} |
||||
|
||||
final match = matches.single; |
||||
if (match.groupCount != 3) { |
||||
continue; |
||||
} |
||||
|
||||
return LoginQrcode( |
||||
server: match.group(3)!, |
||||
user: match.group(1)!, |
||||
password: match.group(2)!, |
||||
); |
||||
} |
||||
|
||||
throw const FormatException(); |
||||
} |
||||
|
||||
static LoginQrcode? tryParse(final String url) { |
||||
try { |
||||
return parse(url); |
||||
} on FormatException { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
final String server; |
||||
final String user; |
||||
final String password; |
||||
} |
@ -0,0 +1,25 @@
|
||||
import 'package:neon/src/models/account.dart'; |
||||
import 'package:test/test.dart'; |
||||
|
||||
void main() { |
||||
const qrCodePath = '/user:JohnDoe&password:super_secret&server:example.com'; |
||||
const qrCode = 'nc://login$qrCodePath'; |
||||
const invalidUrl = '::Not valid LoginQrcode::'; |
||||
const credentials = LoginQrcode( |
||||
serverURL: 'example.com', |
||||
username: 'JohnDoe', |
||||
password: 'super_secret', |
||||
); |
||||
|
||||
group('LoginQrcode', () { |
||||
test('parse', () { |
||||
expect(LoginQrcode.tryParse(qrCode), equals(credentials)); |
||||
expect(LoginQrcode.tryParse(qrCodePath), equals(credentials)); |
||||
expect(LoginQrcode.tryParse(invalidUrl), null); |
||||
}); |
||||
|
||||
test('equality', () { |
||||
expect(credentials, equals(credentials)); |
||||
}); |
||||
}); |
||||
} |
Loading…
Reference in new issue