Skip to content

Commit

Permalink
馃挜 Add Web support (LinusU#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
czepiec committed May 25, 2021
1 parent 9af584d commit 1988697
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A Flutter plugin for authenticating a user with a web service, even if the web service is run by a third party. Most commonly used with OAuth2, but can be used with any web flow that can redirect to a custom scheme.

In the background, this plugin uses [`ASWebAuthenticationSession`][ASWebAuthenticationSession] on iOS 12+ and macOS 10.15+, [`SFAuthenticationSession`][SFAuthenticationSession] on iOS 11, and [Chrome Custom Tabs][Chrome Custom Tabs] on Android. You can build it with iOS 8+, but it is currently only supported by iOS 11 or higher.
In the background, this plugin uses [`ASWebAuthenticationSession`][ASWebAuthenticationSession] on iOS 12+ and macOS 10.15+, [`SFAuthenticationSession`][SFAuthenticationSession] on iOS 11, [Chrome Custom Tabs][Chrome Custom Tabs] on Android and opens a new window on Web. You can build it with iOS 8+, but it is currently only supported by iOS 11 or higher.

[ASWebAuthenticationSession]: https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession
[SFAuthenticationSession]: https://developer.apple.com/documentation/safariservices/sfauthenticationsession
Expand Down Expand Up @@ -92,3 +92,7 @@ In order to capture the callback url, the following `activity` needs to be added
</application>
</manifest>
```

### Web

On the web platform, the schema, host, and port (in the case of a non-standard port) of the redirection URL passed to the authentication service must be the same as the URL on which the application is running, and the path must be `/flutter-web-auth.html`. Parameter `callbackUrlScheme` of the `authenticate()` method does not take into account, so it is possible to use a schema for native platforms in the code.
40 changes: 40 additions & 0 deletions lib/flutter_web_auth_web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:async';
import 'dart:html';
import 'dart:js';

import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';

class FlutterWebAuthPlugin {
static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel(
'flutter_web_auth', const StandardMethodCodec(), registrar.messenger);
final FlutterWebAuthPlugin instance = FlutterWebAuthPlugin();
channel.setMethodCallHandler(instance.handleMethodCall);
}

Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'authenticate':
final String url = call.arguments['url'];
return _authenticate(url);
default:
throw PlatformException(
code: 'Unimplemented',
details: "The flutter_web_auth plugin for web doesn't implement "
"the method '${call.method}'");
}
}

static Future<String> _authenticate(String url) async {
context.callMethod('open', [url]);
await for (MessageEvent messageEvent in window.onMessage) {
final message = messageEvent.data['flutter-web-auth'];
if (messageEvent.origin == Uri.base.origin && message is String) {
return message;
}
}
throw new PlatformException(
code: 'error', message: 'Iterable window.onMessage is empty');
}
}
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ flutter:
pluginClass: FlutterWebAuthPlugin
macos:
pluginClass: FlutterWebAuthPlugin
web:
pluginClass: FlutterWebAuthPlugin
fileName: flutter_web_auth_web.dart

# To add assets to your plugin package, add an assets section, like this:
# assets:
Expand Down
10 changes: 10 additions & 0 deletions web/flutter-web-auth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<title>Authentication complete</title>
<p>Authentication is complete. If this does not happen automatically, please
close the window.
<script>
window.opener.postMessage({
'flutter-web-auth': window.location.href
}, window.location.origin);
window.close();
</script>

0 comments on commit 1988697

Please sign in to comment.