Skip to content

Commit 6ee0337

Browse files
authored
[gis_web] Introduce new GIS SDK JS-Interop package. (#2653)
1 parent cd33647 commit 6ee0337

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2093
-6
lines changed

.ci/scripts/custom_package_tests.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
# Exclusions
77
#
8-
# cross_file
8+
# script/configs/linux_only_custom_test.yaml
99
# Custom tests need Chrome. (They run in linux-custom_package_tests)
1010

1111
dart pub global run flutter_plugin_tools custom-test \
1212
--packages-for-branch --log-timing \
13-
--exclude=cross_file
13+
--exclude=script/configs/linux_only_custom_test.yaml

.cirrus.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,12 @@ task:
261261
local_tests_script:
262262
# flutter_image
263263
# https://github.com/flutter/flutter/issues/100387
264-
# cross_file
265-
# Custom tests need Chrome. (They run in linux-custom_package_tests)
264+
# script/configs/linux_only_custom_test.yaml
265+
# Custom tests need Chrome for these packages. (They run in linux-custom_package_tests)
266266
- if [[ "$CHANNEL" == "master" ]]; then
267-
- ./script/tool_runner.sh custom-test --exclude=cross_file
267+
- ./script/tool_runner.sh custom-test --exclude=script/configs/linux_only_custom_test.yaml
268268
- else
269-
- ./script/tool_runner.sh custom-test --exclude=cross_file,flutter_image
269+
- ./script/tool_runner.sh custom-test --exclude=flutter_image,script/configs/linux_only_custom_test.yaml
270270
- fi
271271
### macOS desktop tasks ###
272272
- name: macos-platform_tests

.github/labeler.yml

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
'p: go_router_builder':
3838
- packages/go_router_builder/**/*
3939

40+
'p: google_identity_services':
41+
- packages/google_indentity_services_web/**
42+
4043
'p: imitation_game':
4144
- packages/imitation_game/**/*
4245

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Below is a list of people and organizations that have contributed
2+
# to the Flutter project. Names should be added to the list like so:
3+
#
4+
# Name/Organization <email address>
5+
6+
Google Inc.
7+
The Chromium Authors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0
2+
3+
* Initial release.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Copyright 2013 The Flutter Authors. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without modification,
4+
are permitted provided that the following conditions are met:
5+
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above
9+
copyright notice, this list of conditions and the following
10+
disclaimer in the documentation and/or other materials provided
11+
with the distribution.
12+
* Neither the name of Google Inc. nor the names of its
13+
contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?code-excerpt path-base="excerpts/packages/google_identity_services_web_example"?>
2+
3+
# google_identity_services_web
4+
5+
A JS-interop layer for Google Identity's Sign In With Google SDK.
6+
7+
See the original JS SDK reference:
8+
9+
* [Sign In With Google](https://developers.google.com/identity/gsi/web)
10+
11+
## Usage
12+
13+
This package is the Dart JS-interop layer of the new **Sign In With Google**
14+
SDK. Here's the API references for both of the sub-libraries:
15+
16+
* `id.dart`: [Sign In With Google JavaScript API reference](https://developers.google.com/identity/gsi/web/reference/js-reference)
17+
* `oauth2.dart`: [Google 3P Authorization JavaScript Library for websites - API reference](https://developers.google.com/identity/oauth2/web/reference/js-reference)
18+
* `loader.dart`: An (optional) loader mechanism that installs the library and
19+
resolves a `Future<void>` when it's ready.
20+
21+
### Loading the SDK
22+
23+
There are two ways to load the JS SDK in your app.
24+
25+
#### Modify your index.html (most performant)
26+
27+
The most performant way is to modify your `web/index.html` file to insert a
28+
script tag [as recommended](https://developers.google.com/identity/gsi/web/guides/client-library).
29+
Place the `script` tag in the `<head>` of your site, next to the script tag that
30+
loads `flutter.js`, so the browser can downloaded both in parallel:
31+
32+
<?code-excerpt "../../web/index-with-script-tag.html (script-tag)"?>
33+
```html
34+
<head>
35+
<!-- ··· -->
36+
<!-- Include the GSI SDK below -->
37+
<script src="https://accounts.google.com/gsi/client" async defer></script>
38+
<!-- This script adds the flutter initialization JS code -->
39+
<script src="flutter.js" defer></script>
40+
</head>
41+
```
42+
43+
#### With the `loadWebSdk` function (on-demand)
44+
45+
An alternative way, that downloads the SDK on demand, is to use the
46+
**`loadWebSdk`** function provided by the library. A simple location to embed
47+
this in a Flutter Web only app can be the `main.dart`:
48+
49+
<?code-excerpt "main.dart (use-loader)"?>
50+
```dart
51+
import 'package:google_identity_services_web/loader.dart' as gis;
52+
// ···
53+
void main() async {
54+
await gis.loadWebSdk(); // Load the GIS SDK
55+
// The rest of your code...
56+
// ···
57+
}
58+
```
59+
60+
(Note that the above won't compile for mobile apps, so if you're developing a
61+
cross-platform app, you'll probably need to hide the call to `loadWebSdk`
62+
behind a [conditional import/export](https://dart.dev/guides/libraries/create-library-packages#conditionally-importing-and-exporting-library-files).)
63+
64+
### Using the SDK
65+
66+
Once the SDK has been loaded, it can be used by importing the correct library:
67+
68+
* `import 'package:google_identity_services/id.dart' as id;` for Authentication
69+
* `import 'package:google_identity_services/oauth2.dart' as oauth2;` for
70+
Authorization.
71+
72+
## Browser compatibility
73+
74+
The new SDK is introducing concepts that are on track for standardization to
75+
most browsers, and it might not be compatible with older browsers.
76+
77+
Refer to the official documentation site for the latest browser compatibility
78+
information of the underlying JS SDK:
79+
80+
* **Sign In With Google > [Supported browsers and platforms](https://developers.google.com/identity/gsi/web/guides/supported-browsers)**
81+
82+
## Testing
83+
84+
This web-only package uses `dart:test` to test its features. They can be run
85+
with `dart test -p chrome`.
86+
87+
_(Look at `test/README.md` and `tool/run_tests.dart` for more info.)_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# See https://github.com/dart-lang/test/blob/master/pkgs/test/doc/configuration.md#arguments
2+
override_platforms:
3+
chrome:
4+
settings:
5+
executable: chrome
6+
arguments: --no-sandbox
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
migrate_working_dir/
12+
13+
# IntelliJ related
14+
*.iml
15+
*.ipr
16+
*.iws
17+
.idea/
18+
19+
# The .vscode folder contains launch configuration and tasks you configure in
20+
# VS Code which you may wish to be included in version control, so this line
21+
# is commented out by default.
22+
#.vscode/
23+
24+
# Flutter/Dart/Pub related
25+
**/doc/api/
26+
**/ios/Flutter/.last_build_id
27+
.dart_tool/
28+
.flutter-plugins
29+
.flutter-plugins-dependencies
30+
.packages
31+
.pub-cache/
32+
.pub/
33+
/build/
34+
35+
# Symbolication related
36+
app.*.symbols
37+
38+
# Obfuscation related
39+
app.*.map.json
40+
41+
# Android Studio will place build artifacts here
42+
/android/app/debug
43+
/android/app/profile
44+
/android/app/release
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled.
5+
6+
version:
7+
revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
8+
channel: master
9+
10+
project_type: app
11+
12+
# Tracks metadata for the flutter migrate command
13+
migration:
14+
platforms:
15+
- platform: root
16+
create_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
17+
base_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
18+
- platform: android
19+
create_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
20+
base_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
21+
- platform: ios
22+
create_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
23+
base_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
24+
- platform: linux
25+
create_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
26+
base_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
27+
- platform: macos
28+
create_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
29+
base_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
30+
- platform: web
31+
create_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
32+
base_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
33+
- platform: windows
34+
create_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
35+
base_revision: 822f6e3595d63f864ae2027ea37fd645b313b71b
36+
37+
# User provided section
38+
39+
# List of Local paths (relative to this file) that should be
40+
# ignored by the migrate tool.
41+
#
42+
# Files that are not part of the templates will be ignored by default.
43+
unmanaged_files:
44+
- 'lib/main.dart'
45+
- 'ios/Runner.xcodeproj/project.pbxproj'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# google_identity_services_web_example
2+
3+
* `lib/main.dart`: An example on how to use the google_identiy_services_web `id` library from Dart.
4+
* `lib/main_oauth.dart`: An example for the `oauth2` library from the same SDK.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
targets:
2+
$default:
3+
sources:
4+
include:
5+
- lib/**
6+
- web/**
7+
# Some default includes that aren't really used here but will prevent
8+
# false-negative warnings:
9+
- $package$
10+
- lib/$lib$
11+
exclude:
12+
- '**/.*/**'
13+
- '**/build/**'
14+
builders:
15+
code_excerpter|code_excerpter:
16+
enabled: true
17+
generate_for:
18+
- '**/*.dart'
19+
- '**/*.html'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// ignore_for_file: avoid_print
6+
7+
import 'package:google_identity_services_web/id.dart' as id;
8+
// #docregion use-loader
9+
import 'package:google_identity_services_web/loader.dart' as gis;
10+
// #enddocregion use-loader
11+
import 'package:js/js.dart' show allowInterop;
12+
import 'package:jwt_decoder/jwt_decoder.dart' as jwt;
13+
14+
// #docregion use-loader
15+
void main() async {
16+
await gis.loadWebSdk(); // Load the GIS SDK
17+
// The rest of your code...
18+
// #enddocregion use-loader
19+
id.setLogLevel('debug');
20+
21+
final id.IdConfiguration config = id.IdConfiguration(
22+
client_id: 'your-client_id.apps.googleusercontent.com',
23+
ux_mode: id.UxMode.popup,
24+
callback: allowInterop(onCredentialResponse),
25+
);
26+
27+
id.initialize(config);
28+
id.prompt(allowInterop(onPromptMoment));
29+
// #docregion use-loader
30+
}
31+
// #enddocregion use-loader
32+
33+
/// Handles the ID token returned from the One Tap prompt.
34+
/// See: https://developers.google.com/identity/gsi/web/reference/js-reference#callback
35+
void onCredentialResponse(id.CredentialResponse o) {
36+
final Map<String, dynamic>? payload = jwt.JwtDecoder.tryDecode(o.credential);
37+
if (payload != null) {
38+
print('Hello, ${payload["name"]}');
39+
print(o.select_by);
40+
print(payload);
41+
} else {
42+
print('Could not decode ${o.credential}');
43+
}
44+
}
45+
46+
/// Handles Prompt UI status notifications.
47+
/// See: https://developers.google.com/identity/gsi/web/reference/js-reference#google.accounts.id.prompt
48+
void onPromptMoment(id.PromptMomentNotification o) {
49+
final id.MomentType type = o.getMomentType();
50+
print(type.runtimeType);
51+
print(type);
52+
print(type.index);
53+
print(o.getDismissedReason());
54+
print(o.getNotDisplayedReason());
55+
print(o.getSkippedReason());
56+
}

0 commit comments

Comments
 (0)