Revert "Use flutter_web_auth instead of our own OAuth mechanism"

This reverts commit 763cbf8493c610dec0e7e344bee40ad331e7a272.
This reverts commit ddad699b259bafe6c7ed630e7afc2eb38b7825e6.

This is causing way too many problems -

On Android with GitHub we occasionally get a User Cancelled exception.
On iOS this doesn't work < ios11

I prefer keeping my way till then. Even though it doesn't support
KeepAlive on Android.
This commit is contained in:
Vishesh Handa
2020-06-10 11:46:55 +02:00
parent 783dbaa505
commit 66a76a0767
11 changed files with 170 additions and 80 deletions

View File

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