Use flutter_web_auth instead of our own OAuth mechanism

This works slightly better on iOS and on Android it has a keep alive,
which will prevent our app from being killed. Additionally, this way
there is less for me to maintain, which is always nicer.

The API for flutter_web_auth is also much simpler.

This also inolves some custom logic for parsing the Query Parameters
from the GitLab callback, as it doesn't seem to be a proper URI. Not
sure what is going on with Gitlab.
This commit is contained in:
Vishesh Handa
2020-06-09 17:09:38 +02:00
parent 32bd44dac4
commit 763cbf8493
10 changed files with 84 additions and 166 deletions

View File

@ -2,9 +2,8 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:url_launcher/url_launcher.dart';
import 'package:flutter_web_auth/flutter_web_auth.dart';
import 'package:gitjournal/utils/logger.dart';
import 'githost.dart';
@ -13,39 +12,29 @@ class GitHub implements GitHost {
static const _clientID = "aa3072cbfb02b1db14ed";
static const _clientSecret = "010d303ea99f82330f2b228977cef9ddbf7af2cd";
var _platform = const MethodChannel('gitjournal.io/git');
var _accessCode = "";
@override
void init(OAuthCallback callback) {
Future _handleMessages(MethodCall call) async {
if (call.method != "onURL") {
Log.d("GitHub Unknown Call: " + call.method);
return;
}
Future<void> init() async {
var url = "https://github.com/login/oauth/authorize?client_id=" +
_clientID +
"&scope=repo";
closeWebView();
Log.d("GitHub: Called onUrl with " + call.arguments.toString());
var resultUrl = await FlutterWebAuth.authenticate(
url: url, callbackUrlScheme: "gitjournal");
String url = call.arguments["URL"];
var uri = Uri.parse(url);
var authCode = uri.queryParameters['code'];
if (authCode == null) {
Log.d("GitHub: Missing auth code. Now what?");
callback(GitHostException.OAuthFailed);
}
_accessCode = await _getAccessCode(authCode);
if (_accessCode == null || _accessCode.isEmpty) {
Log.d("GitHub: AccessCode is invalid: " + _accessCode);
callback(GitHostException.OAuthFailed);
}
callback(null);
var uri = Uri.parse(resultUrl);
var authCode = uri.queryParameters['code'];
if (authCode == null) {
Log.d("GitHub: Missing auth code. Now what?");
throw GitHostException.OAuthFailed;
}
_platform.setMethodCallHandler(_handleMessages);
Log.d("GitHub: Installed Handler");
_accessCode = await _getAccessCode(authCode);
if (_accessCode == null || _accessCode.isEmpty) {
Log.d("GitHub: AccessCode is invalid: " + _accessCode);
throw GitHostException.OAuthFailed;
}
}
Future<String> _getAccessCode(String authCode) async {
@ -66,16 +55,6 @@ class GitHub implements GitHost {
return map["access_token"];
}
@override
Future launchOAuthScreen() async {
// FIXME: Add some 'state' over here!
var url = "https://github.com/login/oauth/authorize?client_id=" +
_clientID +
"&scope=repo";
return launch(url);
}
@override
Future<List<GitHostRepo>> listRepos() async {
if (_accessCode.isEmpty) {