diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md index 4afb1a0e98bf..5e29f340599b 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.0.0 + +* Migrate to null safety. +* Fixes the requested scopes to use the `GoogleSignIn` instance's `scopes`. + ## 1.0.4 * Update the example app: remove the deprecated `RaisedButton` and `FlatButton` widgets. diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart index 597ab563ae5b..0ec62a832648 100755 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/lib/main.dart @@ -56,7 +56,8 @@ class SignInDemoState extends State { _contactText = 'Loading contact info...'; }); - final peopleApi = PeopleApi(await _googleSignIn.authenticatedClient()); + final peopleApi = + PeopleServiceApi(await _googleSignIn.authenticatedClient()); final response = await peopleApi.people.connections.list( 'people/me', personFields: 'names', diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml index 48dfef644a5c..d3b428d190c9 100755 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/example/pubspec.yaml @@ -4,7 +4,7 @@ description: Example of Google Sign-In plugin and googleapis. dependencies: flutter: sdk: flutter - google_sign_in: ^4.4.1 + google_sign_in: ^5.0.0 extension_google_sign_in_as_googleapis_auth: # When depending on this package from a real application you should use: # extension_google_sign_in_as_googleapis_auth: ^x.y.z @@ -12,10 +12,10 @@ dependencies: # The example app is bundled with the plugin so we use a path dependency on # the parent directory to use the current plugin's version. path: ../ - googleapis: ^0.55.0 + googleapis: ^1.0.0 dev_dependencies: - pedantic: ^1.8.0 + pedantic: ^1.10.0 integration_test: path: ../../../integration_test flutter_driver: diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart index eec45cc0e89a..8c8ede5eee1a 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/lib/extension_google_sign_in_as_googleapis_auth.dart @@ -15,15 +15,20 @@ import 'package:http/http.dart' as http; /// client that can be used with the rest of the `googleapis` libraries. extension GoogleApisGoogleSignInAuth on GoogleSignIn { /// Retrieve a `googleapis` authenticated client. - Future authenticatedClient({ - @visibleForTesting GoogleSignInAuthentication debugAuthentication, - @visibleForTesting List debugScopes = const [], + Future authenticatedClient({ + @visibleForTesting GoogleSignInAuthentication? debugAuthentication, + @visibleForTesting List? debugScopes, }) async { - final auth = debugAuthentication ?? await currentUser.authentication; + final GoogleSignInAuthentication? auth = + debugAuthentication ?? await currentUser?.authentication; + final String? oathTokenString = auth?.accessToken; + if (oathTokenString == null) { + return null; + } final credentials = googleapis_auth.AccessCredentials( googleapis_auth.AccessToken( 'Bearer', - auth.accessToken, + oathTokenString, // We don't know when the token expires, so we assume "never" DateTime.now().toUtc().add(Duration(days: 365)), ), diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml index 9da5f0baa848..7d86b67196d0 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/pubspec.yaml @@ -6,23 +6,23 @@ name: extension_google_sign_in_as_googleapis_auth description: A bridge package between google_sign_in and googleapis_auth, to create Authenticated Clients from google_sign_in user credentials. -version: 1.0.4 +version: 2.0.0 homepage: https://github.com/flutter/plugins/google_sign_in/extension_google_sign_in_as_googleapis_auth dependencies: flutter: sdk: flutter - google_sign_in: ^4.4.1 - googleapis_auth: ^0.2.11+1 - meta: ^1.1.8 - http: ^0.12.1 + google_sign_in: ^5.0.0 + googleapis_auth: ^1.0.0 + meta: ^1.3.0 + http: ^0.13.0 dev_dependencies: - mockito: ^4.1.1 - pedantic: ^1.9.0 + pedantic: ^1.10.0 + test: ^1.16.3 flutter_test: sdk: flutter environment: - sdk: ">=2.7.0 <3.0.0" + sdk: ">=2.12.0-259.9.beta <3.0.0" flutter: ">=1.12.13+hotfix.4" diff --git a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart index 9f86703d4bb8..508f366eacde 100644 --- a/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart +++ b/packages/google_sign_in/extension_google_sign_in_as_googleapis_auth/test/extension_google_sign_in_as_googleapis_auth_test.dart @@ -7,25 +7,25 @@ import 'package:google_sign_in/google_sign_in.dart'; import 'package:googleapis_auth/auth.dart' as auth; import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart'; -import 'package:mockito/mockito.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:test/fake.dart'; -// Mocks so I don't have to prepare all the GoogleSignIn environment. -class MockGoogleSignIn extends Mock implements GoogleSignIn {} +const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null'; +const DEBUG_FAKE_SCOPES = ['some-scope', 'another-scope']; +const SIGN_IN_FAKE_SCOPES = ['some-scope', 'another-scope']; -class MockGoogleSignInAuthentication extends Mock - implements GoogleSignInAuthentication {} +class FakeGoogleSignIn extends Fake implements GoogleSignIn { + final List scopes = SIGN_IN_FAKE_SCOPES; +} -const SOME_FAKE_ACCESS_TOKEN = 'this-is-something-not-null'; -const SOME_FAKE_SCOPES = ['some-scope', 'another-scope']; +class FakeGoogleSignInAuthentication extends Fake + implements GoogleSignInAuthentication { + final String accessToken = SOME_FAKE_ACCESS_TOKEN; +} void main() { - GoogleSignIn signIn = MockGoogleSignIn(); - final authMock = MockGoogleSignInAuthentication(); - - setUp(() { - when(authMock.accessToken).thenReturn(SOME_FAKE_ACCESS_TOKEN); - }); + GoogleSignIn signIn = FakeGoogleSignIn(); + final authMock = FakeGoogleSignInAuthentication(); test('authenticatedClient returns an authenticated client', () async { final client = await signIn.authenticatedClient( @@ -34,13 +34,21 @@ void main() { expect(client, isA()); }); + test('authenticatedClient uses GoogleSignIn scopes by default', () async { + final client = (await signIn.authenticatedClient( + debugAuthentication: authMock, + ))!; + expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN)); + expect(client.credentials.scopes, equals(SIGN_IN_FAKE_SCOPES)); + }); + test('authenticatedClient returned client contains the passed-in credentials', () async { - final client = await signIn.authenticatedClient( + final client = (await signIn.authenticatedClient( debugAuthentication: authMock, - debugScopes: SOME_FAKE_SCOPES, - ); + debugScopes: DEBUG_FAKE_SCOPES, + ))!; expect(client.credentials.accessToken.data, equals(SOME_FAKE_ACCESS_TOKEN)); - expect(client.credentials.scopes, equals(SOME_FAKE_SCOPES)); + expect(client.credentials.scopes, equals(DEBUG_FAKE_SCOPES)); }); }