This repository was archived by the owner on Feb 22, 2023. It is now read-only.
forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 6
[google_sign_in] Add platform_interface package #99
Merged
ditman
merged 2 commits into
federated_google_sign_in
from
federated_google_sign_in_platform_interface
Nov 8, 2019
+501
−0
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add the google_sign_in_platform_interface package.
- Loading branch information
commit 30591dd01e9294210fe1b4aced19c1971e8fb0ab
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
packages/google_sign_in/google_sign_in_platform_interface/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 1.0.0 | ||
|
|
||
| * Initial release. |
27 changes: 27 additions & 0 deletions
27
packages/google_sign_in/google_sign_in_platform_interface/LICENSE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright 2017 The Chromium Authors. All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 changes: 26 additions & 0 deletions
26
packages/google_sign_in/google_sign_in_platform_interface/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # google_sign_in_platform_interface | ||
|
|
||
| A common platform interface for the [`google_sign_in`][1] plugin. | ||
|
|
||
| This interface allows platform-specific implementations of the `google_sign_in` | ||
| plugin, as well as the plugin itself, to ensure they are supporting the | ||
| same interface. | ||
|
|
||
| # Usage | ||
|
|
||
| To implement a new platform-specific implementation of `google_sign_in`, extend | ||
| [`GoogleSignInPlatform`][2] with an implementation that performs the | ||
| platform-specific behavior, and when you register your plugin, set the default | ||
| `GoogleSignInPlatform` by calling | ||
| `GoogleSignInPlatform.instance = MyPlatformGoogleSignIn()`. | ||
|
|
||
| # Note on breaking changes | ||
|
|
||
| Strongly prefer non-breaking changes (such as adding a method to the interface) | ||
| over breaking changes for this package. | ||
|
|
||
| See https://flutter.dev/go/platform-interface-breaking-changes for a discussion | ||
| on why a less-clean interface is preferable to a breaking change. | ||
|
|
||
| [1]: ../google_sign_in | ||
| [2]: lib/google_sign_in_platform_interface.dart |
109 changes: 109 additions & 0 deletions
109
...ogle_sign_in/google_sign_in_platform_interface/lib/google_sign_in_platform_interface.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // Copyright 2017 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:async'; | ||
| import 'package:meta/meta.dart' show required, visibleForTesting; | ||
| import 'src/method_channel_google_sign_in.dart'; | ||
| import 'src/types.dart'; | ||
|
|
||
| export 'src/method_channel_google_sign_in.dart'; | ||
| export 'src/types.dart'; | ||
|
|
||
| /// The interface that implementations of google_sign_in must implement. | ||
| /// | ||
| /// Platform implementations that live in a separate package should extend this | ||
| /// class rather than implement it as `google_sign_in` does not consider newly | ||
| /// added methods to be breaking changes. Extending this class (using `extends`) | ||
| /// ensures that the subclass will get the default implementation, while | ||
| /// platform implementations that `implements` this interface will be broken by | ||
| /// newly added [GoogleSignInPlatform] methods. | ||
| abstract class GoogleSignInPlatform { | ||
| /// Only mock implementations should set this to `true`. | ||
| /// | ||
| /// Mockito mocks implement this class with `implements` which is forbidden | ||
| /// (see class docs). This property provides a backdoor for mocks to skip the | ||
| /// verification that the class isn't implemented with `implements`. | ||
| @visibleForTesting | ||
| bool get isMock => false; | ||
|
|
||
| /// The default instance of [GoogleSignInPlatform] to use. | ||
| /// | ||
| /// Platform-specific plugins should override this with their own | ||
| /// platform-specific class that extends [GoogleSignInPlatform] when they | ||
| /// register themselves. | ||
| /// | ||
| /// Defaults to [MethodChannelGoogleSignIn]. | ||
| static GoogleSignInPlatform get instance => _instance; | ||
|
|
||
| static GoogleSignInPlatform _instance = MethodChannelGoogleSignIn(); | ||
|
|
||
| // TODO(amirh): Extract common platform interface logic. | ||
| // https://github.com/flutter/flutter/issues/43368 | ||
| static set instance(GoogleSignInPlatform instance) { | ||
| if (!instance.isMock) { | ||
| try { | ||
| instance._verifyProvidesDefaultImplementations(); | ||
| } on NoSuchMethodError catch (_) { | ||
| throw AssertionError( | ||
| 'Platform interfaces must not be implemented with `implements`'); | ||
| } | ||
| } | ||
| _instance = instance; | ||
| } | ||
|
|
||
| /// This method ensures that [GoogleSignInPlatform] isn't implemented with `implements`. | ||
| /// | ||
| /// See class docs for more details on why using `implements` to implement | ||
| /// [GoogleSignInPlatform] is forbidden. | ||
| /// | ||
| /// This private method is called by the [instance] setter, which should fail | ||
| /// if the provided instance is a class implemented with `implements`. | ||
| void _verifyProvidesDefaultImplementations() {} | ||
|
|
||
| /// Initializes the plugin. You must call this method before calling other methods. | ||
| /// See: https://developers.google.com/identity/sign-in/web/reference#gapiauth2initparams | ||
| Future<void> init( | ||
| {@required String hostedDomain, | ||
| List<String> scopes, | ||
| SignInOption signInOption, | ||
| String clientId}) async { | ||
| throw UnimplementedError('init() has not been implemented.'); | ||
| } | ||
|
|
||
| /// Attempts to reuse pre-existing credentials to sign in again, without user interaction | ||
| Future<GoogleSignInUserData> signInSilently() async { | ||
| throw UnimplementedError('signInSilently() has not been implemented.'); | ||
| } | ||
|
|
||
| /// Signs in the user with the options specified to [init]. | ||
| Future<GoogleSignInUserData> signIn() async { | ||
| throw UnimplementedError('signIn() has not been implemented.'); | ||
| } | ||
|
|
||
| /// Returns the Tokens used to authenticate other API calls. | ||
| Future<GoogleSignInTokenData> getTokens( | ||
| {@required String email, bool shouldRecoverAuth}) async { | ||
| throw UnimplementedError('getTokens() has not been implemented.'); | ||
| } | ||
|
|
||
| /// Signs out the current account from the application. | ||
| Future<void> signOut() async { | ||
| throw UnimplementedError('signOut() has not been implemented.'); | ||
| } | ||
|
|
||
| /// Revokes all of the scopes that the user granted. | ||
| Future<void> disconnect() async { | ||
| throw UnimplementedError('disconnect() has not been implemented.'); | ||
| } | ||
|
|
||
| /// Returns whether the current user is currently signed in. | ||
| Future<bool> isSignedIn() async { | ||
| throw UnimplementedError('isSignedIn() has not been implemented.'); | ||
| } | ||
|
|
||
| /// Clears any cached information that the plugin may be holding on to. | ||
| Future<void> clearAuthCache({@required String token}) async { | ||
| throw UnimplementedError('clearAuthCache() has not been implemented.'); | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
...ogle_sign_in/google_sign_in_platform_interface/lib/src/method_channel_google_sign_in.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright 2017 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/services.dart'; | ||
| import 'package:meta/meta.dart' show required, visibleForTesting; | ||
|
|
||
| import '../google_sign_in_platform_interface.dart'; | ||
| import 'types.dart'; | ||
| import 'utils.dart'; | ||
|
|
||
| /// An implementation of [GoogleSignInPlatform] that uses method channels. | ||
| class MethodChannelGoogleSignIn extends GoogleSignInPlatform { | ||
| @visibleForTesting | ||
| MethodChannel channel = MethodChannel('plugins.flutter.io/google_sign_in'); | ||
|
|
||
| @override | ||
| Future<void> init( | ||
| {@required String hostedDomain, | ||
| List<String> scopes = const <String>[], | ||
| SignInOption signInOption = SignInOption.standard, | ||
| String clientId}) { | ||
| return channel.invokeMethod<void>('init', <String, dynamic>{ | ||
| 'signInOption': signInOption.toString(), | ||
| 'scopes': scopes, | ||
| 'hostedDomain': hostedDomain, | ||
| }); | ||
| } | ||
|
|
||
| @override | ||
| Future<GoogleSignInUserData> signInSilently() { | ||
| return channel.invokeMapMethod<String, dynamic>('signInSilently').then( | ||
| (Map<String, dynamic> data) => nativeUserDataToPluginUserData(data)); | ||
| } | ||
|
|
||
| @override | ||
| Future<GoogleSignInUserData> signIn() { | ||
| return channel.invokeMapMethod<String, dynamic>('signIn').then( | ||
| (Map<String, dynamic> data) => nativeUserDataToPluginUserData(data)); | ||
| } | ||
|
|
||
| @override | ||
| Future<GoogleSignInTokenData> getTokens( | ||
| {String email, bool shouldRecoverAuth = true}) { | ||
| return channel.invokeMapMethod<String, dynamic>( | ||
| 'getTokens', <String, dynamic>{ | ||
| 'email': email, | ||
| 'shouldRecoverAuth': shouldRecoverAuth, | ||
| }).then( | ||
| (Map<String, dynamic> data) => nativeTokenDataToPluginTokenData(data)); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> signOut() { | ||
| return channel.invokeMapMethod<String, dynamic>('signOut'); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> disconnect() { | ||
| return channel.invokeMapMethod<String, dynamic>('disconnect'); | ||
| } | ||
|
|
||
| @override | ||
| Future<bool> isSignedIn() { | ||
| return channel.invokeMethod<bool>('isSignedIn'); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> clearAuthCache({String token}) { | ||
| return channel.invokeMethod<void>( | ||
| 'clearAuthCache', | ||
| <String, String>{'token': token}, | ||
| ); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
packages/google_sign_in/google_sign_in_platform_interface/lib/src/types.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import 'package:quiver_hashcode/hashcode.dart'; | ||
|
|
||
| enum SignInOption { standard, games } | ||
|
|
||
| class GoogleSignInUserData { | ||
| GoogleSignInUserData( | ||
| {this.displayName, this.email, this.id, this.photoUrl, this.idToken}); | ||
| String displayName; | ||
| String email; | ||
| String id; | ||
| String photoUrl; | ||
| String idToken; | ||
|
|
||
| @override | ||
| int get hashCode => | ||
| hashObjects(<String>[displayName, email, id, photoUrl, idToken]); | ||
|
|
||
| @override | ||
| bool operator ==(dynamic other) { | ||
| if (identical(this, other)) return true; | ||
| if (other is! GoogleSignInUserData) return false; | ||
| return other.displayName == displayName && | ||
ditman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| other.email == email && | ||
| other.id == id && | ||
| other.photoUrl == photoUrl && | ||
| other.idToken == idToken; | ||
| } | ||
| } | ||
|
|
||
| class GoogleSignInTokenData { | ||
| GoogleSignInTokenData({this.idToken, this.accessToken}); | ||
| String idToken; | ||
| String accessToken; | ||
|
|
||
| @override | ||
| int get hashCode => hash2(idToken, accessToken); | ||
|
|
||
| @override | ||
| bool operator ==(dynamic other) { | ||
| if (identical(this, other)) return true; | ||
| if (other is! GoogleSignInTokenData) return false; | ||
| return other.idToken == idToken && other.accessToken == accessToken; | ||
| } | ||
| } | ||
24 changes: 24 additions & 0 deletions
24
packages/google_sign_in/google_sign_in_platform_interface/lib/src/utils.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import '../google_sign_in_platform_interface.dart'; | ||
|
|
||
| GoogleSignInUserData nativeUserDataToPluginUserData(Map<String, dynamic> data) { | ||
ditman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (data == null) { | ||
| return null; | ||
| } | ||
| return GoogleSignInUserData( | ||
| displayName: data['displayName'], | ||
| email: data['email'], | ||
| id: data['id'], | ||
| photoUrl: data['photoUrl'], | ||
| idToken: data['idToken']); | ||
| } | ||
|
|
||
| GoogleSignInTokenData nativeTokenDataToPluginTokenData( | ||
| Map<String, dynamic> data) { | ||
| if (data == null) { | ||
| return null; | ||
| } | ||
| return GoogleSignInTokenData( | ||
| idToken: data['idToken'], | ||
| accessToken: data['accessToken'], | ||
| ); | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
packages/google_sign_in/google_sign_in_platform_interface/pubspec.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| name: google_sign_in_platform_interface | ||
| description: A common platform interface for the google_sign_in plugin. | ||
| author: Flutter Team <flutter-dev@googlegroups.com> | ||
| homepage: https://github.com/flutter/plugins/tree/master/packages/google_sign_in/google_sign_in_platform_interface | ||
| # NOTE: We strongly prefer non-breaking changes, even at the expense of a | ||
| # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes | ||
| version: 1.0.0 | ||
|
|
||
| dependencies: | ||
| flutter: | ||
| sdk: flutter | ||
| meta: ^1.0.5 | ||
| quiver_hashcode: ^2.0.0 | ||
|
|
||
| dev_dependencies: | ||
| flutter_test: | ||
| sdk: flutter | ||
| mockito: ^4.1.1 | ||
|
|
||
| environment: | ||
| sdk: ">=2.0.0-dev.28.0 <3.0.0" | ||
| flutter: ">=1.9.1+hotfix.4 <2.0.0" |
37 changes: 37 additions & 0 deletions
37
...ign_in/google_sign_in_platform_interface/test/google_sign_in_platform_interface_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright 2019 The Chromium Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:google_sign_in_platform_interface/google_sign_in_platform_interface.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mockito/mockito.dart'; | ||
|
|
||
| void main() { | ||
| group('$GoogleSignInPlatform', () { | ||
| test('$MethodChannelGoogleSignIn is the default instance', () { | ||
| expect(GoogleSignInPlatform.instance, isA<MethodChannelGoogleSignIn>()); | ||
| }); | ||
|
|
||
| test('Cannot be implemented with `implements`', () { | ||
| expect(() { | ||
| GoogleSignInPlatform.instance = ImplementsGoogleSignInPlatform(); | ||
| }, throwsAssertionError); | ||
| }); | ||
|
|
||
| test('Can be extended', () { | ||
| GoogleSignInPlatform.instance = ExtendsGoogleSignInPlatform(); | ||
| }); | ||
|
|
||
| test('Can be mocked with `implements`', () { | ||
| final ImplementsGoogleSignInPlatform mock = | ||
| ImplementsGoogleSignInPlatform(); | ||
| when(mock.isMock).thenReturn(true); | ||
| GoogleSignInPlatform.instance = mock; | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| class ImplementsGoogleSignInPlatform extends Mock | ||
| implements GoogleSignInPlatform {} | ||
|
|
||
| class ExtendsGoogleSignInPlatform extends GoogleSignInPlatform {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.