From bb8680e3f6094d01e75ba4a249ae13fb708e7314 Mon Sep 17 00:00:00 2001 From: zoe Date: Thu, 30 Jun 2022 20:45:00 +0200 Subject: [PATCH] basic oauth finally but also more dependencies --- lib/business_logic/auth/oauth.dart | 35 ++++++++++++++++++++++++++++-- lib/pages/login.dart | 12 +++++----- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/business_logic/auth/oauth.dart b/lib/business_logic/auth/oauth.dart index 6eda7ba..f9e0a7b 100644 --- a/lib/business_logic/auth/oauth.dart +++ b/lib/business_logic/auth/oauth.dart @@ -4,6 +4,8 @@ import 'package:http/http.dart' as http; import 'package:url_launcher/url_launcher.dart'; import '../../global.dart' as global; import '../../business_logic/settings.dart' as settings; +import 'package:shelf/shelf.dart'; +import 'package:shelf/shelf_io.dart' as shelf_io; class App { late String clientSecret; @@ -30,6 +32,35 @@ class App { } } +Response readAuthcode(Request request) { + Map params = request.url.queryParameters; + if (params.containsKey("code") && params["code"] != null) { + String code = params["code"].toString(); + print(code); + } + return Response(200, + body: + ""); +} + +// returns status code +Future handleFullOauth() async { + try { + http.Response response = await doOauthFlow(); + if (response.statusCode != 200) { + return response.statusCode; + } + + var handler = const Pipeline().addHandler(readAuthcode); + var server = await shelf_io.serve(handler, 'localhost', 1312); + server.idleTimeout = const Duration(minutes: 5); + server.autoCompress = true; + return 200; + } catch (e) { + return 400; + } +} + Future doOauthFlow() async { String url = await settings.loadInstanceUrl(); try { @@ -49,7 +80,7 @@ Future registerApp(String baseurl) async { headers: global.defaultHeaders, body: jsonEncode({ 'client_name': global.name, - 'redirect_uris': "http://localhost:4040", + 'redirect_uris': "http://localhost:1312", 'scopes': "read write", 'website': global.website })); @@ -64,7 +95,7 @@ void openBrowserForAuthCode(String baseurl, App app) { query: "client_id=" + app.clientId + "&scope=read+write" + - "&redirect_uri=http://localhost:4040" + + "&redirect_uri=http://localhost:1312" + "&response_type=code"); launchUrl(url); } diff --git a/lib/pages/login.dart b/lib/pages/login.dart index 7c77f12..0393b65 100644 --- a/lib/pages/login.dart +++ b/lib/pages/login.dart @@ -124,18 +124,16 @@ class _AuthPageState extends State { }, icon: const Icon(Icons.arrow_back), label: Text("back-button".i18n())), - FutureBuilder( - future: oauth.doOauthFlow(), + FutureBuilder( + future: oauth.handleFullOauth(), builder: (context, snapshot) { if (snapshot.hasError) { return Text("login-failed-snackbar-text".i18n()); } else if (snapshot.hasData) { - if (snapshot.data?.statusCode != null) { - if (snapshot.data?.statusCode != 200) { + if (snapshot.data != null) { + if (snapshot.data != 200) { return Row( - children: [ - Text("error ${snapshot.data!.statusCode}") - ], + children: [Text("error ${snapshot.data}")], ); } }