Add a POC OAuth client implementation

I've registered GitJournal as an app on Github and tried to get the
access_token within the app. This seems to work. Though, I need to
provide the client secret as well. It's quite sad that GitHub does not
even support the Implicit Grant auth type.

Credit: https://stackoverflow.com/questions/46196240/oauth2-flow-in-flutter-app
This commit is contained in:
Vishesh Handa
2019-01-23 20:17:59 +01:00
parent 03db7d62e7
commit c2e49bb630
4 changed files with 101 additions and 1 deletions

View File

@ -35,5 +35,17 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".UriReceiverActivity"
android:parentActivityName=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="gitjournal" android:host="login.oauth2" />
<!-- Triggering URI would be gitjournal://login.oauth2 -->
</intent-filter>
</activity>
</application>
</manifest>

View File

@ -22,13 +22,15 @@ import io.flutter.util.PathUtils;
public class MainActivity extends FlutterActivity implements MethodCallHandler {
private static final String CHANNEL_NAME = "gitjournal.io/git";
static MethodChannel channel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL_NAME).setMethodCallHandler(this);
channel = new MethodChannel(getFlutterView(), CHANNEL_NAME);
channel.setMethodCallHandler(this);
}
@Override

View File

@ -0,0 +1,35 @@
package io.gitjournal.gitjournal;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.net.*;
import android.content.Intent;
import java.util.HashMap;
import java.util.Map;
public class UriReceiverActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri data = getIntent().getData();
Map<String, Object> map = new HashMap<>();
map.put("URL", data.toString());
MainActivity.channel.invokeMethod("onURL", map);
// Now that all data has been sent back to Dart-land, we should re-open the Flutter
// activity. Due to the manifest-setting of the MainActivity ("singleTop), only a single
// instance will exist, popping the old one back up and destroying the preceding
// activities on the backstack, such as the custom tab.
// Flags taken from how the AppAuth-library accomplishes the same thing
Intent mainIntent = new Intent(this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(mainIntent);
finish();
}
}

51
lib/oauthapp.dart Normal file
View File

@ -0,0 +1,51 @@
import 'package:flutter/material.dart';
import 'package:journal/apis/git.dart';
import 'package:flutter/services.dart';
import 'package:url_launcher/url_launcher.dart';
const _platform = const MethodChannel('gitjournal.io/git');
// Actual message handler:
Future _handleMessages(MethodCall call) async {
switch (call.method) {
case "onURL":
print("Call onURL" + call.arguments.toString());
// Do something nice using call.arguments["URL"]
}
}
class OAuthApp extends StatefulWidget {
@override
OAuthAppState createState() {
return new OAuthAppState();
}
}
class OAuthAppState extends State<OAuthApp> {
void initState() {
super.initState();
_platform.setMethodCallHandler(_handleMessages);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'OAuth App',
home: Scaffold(
appBar: AppBar(
title: Text('OAuth Test'),
),
body: Column(children: <Widget>[
RaisedButton(
child: Text("Open OAuth URL"),
onPressed: () {
var url =
"https://github.com/login/oauth/authorize?client_id=aa3072cbfb02b1db14ed&scope=repo";
launch(url);
},
),
]),
),
);
}
}