diff --git a/lib/business_logic/auth/oauth.dart b/lib/business_logic/auth/oauth.dart index edb2c4c..6eda7ba 100644 --- a/lib/business_logic/auth/oauth.dart +++ b/lib/business_logic/auth/oauth.dart @@ -30,10 +30,13 @@ class App { } } -Future askKnownServerForAppRegistration() async { +Future doOauthFlow() async { String url = await settings.loadInstanceUrl(); try { - return await registerApp(url); + http.Response response = await registerApp(url); + print(response.body); + openBrowserForAuthCode(url, App.fromJson(jsonDecode(response.body))); + return response; } catch (e) { return http.Response(jsonEncode({}), 404); } @@ -46,7 +49,7 @@ Future registerApp(String baseurl) async { headers: global.defaultHeaders, body: jsonEncode({ 'client_name': global.name, - 'redirect_uris': "urn:ietf:wg:oauth:2.0:oob", + 'redirect_uris': "http://localhost:4040", 'scopes': "read write", 'website': global.website })); @@ -54,14 +57,14 @@ Future registerApp(String baseurl) async { } void openBrowserForAuthCode(String baseurl, App app) { - Uri url = Uri.https( - baseurl, + Uri url = Uri( + scheme: "https", + path: "$baseurl/oauth/authorize", // ignore: prefer_interpolation_to_compose_strings - "/oauth/authorize" - "?client_id=" + + query: "client_id=" + app.clientId + "&scope=read+write" + - "&redirect_uri=urn:ietf:wg:oauth:2.0:oob" + + "&redirect_uri=http://localhost:4040" + "&response_type=code"); launchUrl(url); } diff --git a/lib/business_logic/settings.dart b/lib/business_logic/settings.dart index 002c8b6..6cefc4a 100644 --- a/lib/business_logic/settings.dart +++ b/lib/business_logic/settings.dart @@ -1,4 +1,5 @@ import 'package:shared_preferences/shared_preferences.dart'; +import 'package:slothmu/business_logic/auth/oauth.dart'; enum Settings { instanceUrl, diff --git a/lib/pages/login.dart b/lib/pages/login.dart index 43157b6..7c77f12 100644 --- a/lib/pages/login.dart +++ b/lib/pages/login.dart @@ -125,13 +125,12 @@ class _AuthPageState extends State { icon: const Icon(Icons.arrow_back), label: Text("back-button".i18n())), FutureBuilder( - future: oauth.askKnownServerForAppRegistration(), + future: oauth.doOauthFlow(), builder: (context, snapshot) { if (snapshot.hasError) { return Text("login-failed-snackbar-text".i18n()); } else if (snapshot.hasData) { if (snapshot.data?.statusCode != null) { - print(snapshot.data?.body); if (snapshot.data?.statusCode != 200) { return Row( children: [ diff --git a/pubspec.lock b/pubspec.lock index 9b9bd34..678978c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -273,6 +273,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" + shelf: + dependency: "direct main" + description: + name: shelf + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 7412342..f2885e0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -42,6 +42,7 @@ dependencies: shared_preferences: ^2.0.15 intl: ^0.17.0 url_launcher: ^6.1.4 + shelf: ^1.3.1 dev_dependencies: flutter_test: