basic oauth finally but also more dependencies

This commit is contained in:
zoe 2022-06-30 20:45:00 +02:00
parent f6ea45d0e2
commit bb8680e3f6
2 changed files with 38 additions and 9 deletions

View file

@ -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<String, String> params = request.url.queryParameters;
if (params.containsKey("code") && params["code"] != null) {
String code = params["code"].toString();
print(code);
}
return Response(200,
body:
"<html><head><meta http-equiv='Refresh' content='0; URL=https://example.com/'></head></html>");
}
// returns status code
Future<int> 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<http.Response> doOauthFlow() async {
String url = await settings.loadInstanceUrl();
try {
@ -49,7 +80,7 @@ Future<http.Response> 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);
}

View file

@ -124,18 +124,16 @@ class _AuthPageState extends State<AuthPage> {
},
icon: const Icon(Icons.arrow_back),
label: Text("back-button".i18n())),
FutureBuilder<http.Response>(
future: oauth.doOauthFlow(),
FutureBuilder<int>(
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}")],
);
}
}