2022-09-04 08:12:52 +00:00
|
|
|
// oauth page for web only
|
|
|
|
|
2022-09-03 19:08:53 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:localization/localization.dart';
|
|
|
|
import 'package:universal_html/html.dart' as html;
|
|
|
|
import 'package:loris/business_logic/auth/oauth.dart' as oauth;
|
|
|
|
|
|
|
|
class Weblogin extends StatefulWidget {
|
|
|
|
const Weblogin({Key? key, required this.url}) : super(key: key);
|
|
|
|
final String url;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<Weblogin> createState() => _WebloginState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _WebloginState extends State<Weblogin> {
|
|
|
|
bool doneLoading = false;
|
|
|
|
void doAuth() async {
|
|
|
|
final appresponse = await oauth.registerApp(widget.url);
|
|
|
|
if (appresponse.statusCode != 200) {
|
|
|
|
informAboutFailure(appresponse.statusCode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final app = oauth.App.fromJson(jsonDecode(appresponse.body));
|
|
|
|
|
2022-09-04 14:20:42 +00:00
|
|
|
html.WindowBase? popupWin = html.window.open(
|
2022-09-04 09:14:51 +00:00
|
|
|
oauth.getAuthUrl(widget.url, app).toString(),
|
|
|
|
"_blank",
|
2022-09-03 19:08:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
html.window.onMessage.listen((event) async {
|
|
|
|
final uri = Uri.parse(event.data);
|
|
|
|
if (uri.queryParameters.containsKey("code")) {
|
2022-09-04 14:20:42 +00:00
|
|
|
//ignore: ivalid_null_aware_operator
|
|
|
|
//popupWin.close();
|
2022-09-03 19:08:53 +00:00
|
|
|
final token = await oauth.getToken(
|
|
|
|
uri.queryParameters["code"]!,
|
|
|
|
app.clientId,
|
|
|
|
app.clientSecret,
|
|
|
|
widget.url,
|
|
|
|
);
|
|
|
|
await oauth.saveIdentity(token, widget.url, app.clientId,
|
|
|
|
app.clientSecret, uri.queryParameters["code"]!);
|
|
|
|
setState(() {
|
|
|
|
doneLoading = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void informAboutFailure(int i) {
|
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
.showSnackBar(SnackBar(content: Text("error: $i")));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
doAuth();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
body: Column(
|
|
|
|
children: [
|
|
|
|
SelectableText(widget.url),
|
|
|
|
doneLoading
|
|
|
|
? TextButton.icon(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pushReplacementNamed("/");
|
|
|
|
},
|
|
|
|
icon: const Icon(
|
|
|
|
Icons.navigate_next,
|
|
|
|
),
|
|
|
|
label: Text(
|
|
|
|
"jack-in".i18n(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
: const LoadingIndicator(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LoadingIndicator extends StatelessWidget {
|
|
|
|
const LoadingIndicator({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Wrap(
|
|
|
|
alignment: WrapAlignment.center,
|
|
|
|
spacing: 24,
|
|
|
|
children: [
|
|
|
|
const CircularProgressIndicator(),
|
|
|
|
SelectableText("jacking-in".i18n()),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|