diff --git a/test_integration/web/drive_demo.dart b/test_integration/web/drive_demo.dart index 08f7679e4..bd07ab5e0 100644 --- a/test_integration/web/drive_demo.dart +++ b/test_integration/web/drive_demo.dart @@ -11,8 +11,6 @@ import 'package:http/browser_client.dart'; import 'web_shared.dart'; -final _textArea = querySelector('textarea') as TextAreaElement; - final _loginButton = querySelector('#login') as ButtonElement; final _uploadInput = querySelector('#upload') as FileUploadInputElement; @@ -33,14 +31,14 @@ Future _login() async { logLevel: 'debug', ); } on AuthenticationException catch (e) { - _log(e.error); + logToTextArea(e.error); return; } _loginButton.disabled = _credentials != null; _uploadInput.disabled = _credentials == null; - _log([ + logToTextArea([ 'logged in!', jsonEncode(_credentials), ].join('\n')); @@ -50,7 +48,7 @@ Future _upload() async { final files = _uploadInput.files; if (files == null) { - _log('no file!'); + logToTextArea('no file!'); return; } @@ -65,7 +63,7 @@ Future _upload() async { final options = drive.UploadOptions.resumable; final file = files.single; - _log('starting upload'); + logToTextArea('starting upload'); final newFile = await api.create( drive.File(name: 'Google APIs test file on ${DateTime.now()}'), @@ -73,16 +71,12 @@ Future _upload() async { uploadOptions: options, ); - _log(jsonEncode(newFile)); + logToTextArea(jsonEncode(newFile)); } finally { client.close(); } } -void _log(Object value) { - _textArea.text = '$value\n\n\n${_textArea.text!}'; -} - extension on File { Stream read(int chunkSize) async* { if (chunkSize <= 0) { @@ -99,7 +93,7 @@ extension on File { final bytes = reader.result as Uint8List; loaded += bytes.length; yield bytes; - _log( + logToTextArea( 'Finished $loaded of $size - ' '${(100 * loaded / size).toStringAsFixed(2)}%', ); diff --git a/test_integration/web/google_identity_demo.dart b/test_integration/web/google_identity_demo.dart new file mode 100644 index 000000000..20cfdae5c --- /dev/null +++ b/test_integration/web/google_identity_demo.dart @@ -0,0 +1,58 @@ +import 'dart:html' show ButtonElement, querySelector; + +import 'package:googleapis_auth/auth_browser.dart'; + +import 'web_shared.dart'; + +String? _accessToken; + +final _revokeButton = querySelector('#revoke') as ButtonElement; + +void main() { + final loginButton = querySelector('#login') as ButtonElement; + loginButton.onClick.listen((event) => _login()); + + final loginCodeButton = querySelector('#loginCode') as ButtonElement; + loginCodeButton.onClick.listen((event) => _loginCode()); + + _revokeButton.onClick.listen((event) async { + if (_accessToken == null) return; + + await revokeConsent(_accessToken!); + + _accessToken = null; + }); +} + +Future _login() async { + final response = await requestAccessCredentials( + clientId: clientId().identifier, + scopes: ['openid', 'email'], + prompt: 'consent', + // ignore: deprecated_member_use + logLevel: 'debug', + ); + + logToTextArea([ + response.accessToken.toJson(), + response.scopes, + ].join('\n')); + + _accessToken = response.accessToken.data; + + _revokeButton.disabled = false; +} + +Future _loginCode() async { + final response = await requestAuthorizationCode( + clientId: clientId().identifier, + scopes: ['openid', 'email'], + // ignore: deprecated_member_use + logLevel: 'debug', + ); + + logToTextArea([ + response.code, + response.scopes, + ].join('\n')); +} diff --git a/test_integration/web/google_identity_demo.html b/test_integration/web/google_identity_demo.html new file mode 100644 index 000000000..5b87e964f --- /dev/null +++ b/test_integration/web/google_identity_demo.html @@ -0,0 +1,17 @@ + + + + + + + +

+

+

+ +

+ + + + + diff --git a/test_integration/web/implicit_browser_flow.dart b/test_integration/web/implicit_browser_flow.dart index 5c83ec901..66685d3a7 100644 --- a/test_integration/web/implicit_browser_flow.dart +++ b/test_integration/web/implicit_browser_flow.dart @@ -8,8 +8,6 @@ import 'web_shared.dart'; late final BrowserOAuth2Flow _flow; -final _textArea = querySelector('textarea') as TextAreaElement; - Future main() async { _flow = await createImplicitBrowserFlow( clientId(), @@ -28,7 +26,7 @@ Future main() async { Future _hybridFlow() async { final creds = await _flow.runHybridFlow(); - _log([ + logToTextArea([ 'cred json', prettyJsonEncode(creds.credentials), '', @@ -46,12 +44,8 @@ Future _login() async { ], ); - _log([ + logToTextArea([ 'cred json', prettyJsonEncode(creds), ].join('\n')); } - -void _log(String value) { - _textArea.text = value; -} diff --git a/test_integration/web/index.html b/test_integration/web/index.html index 3d2bb193d..41ef47951 100644 --- a/test_integration/web/index.html +++ b/test_integration/web/index.html @@ -6,8 +6,9 @@ diff --git a/test_integration/web/web_shared.dart b/test_integration/web/web_shared.dart index 3cbff8e46..411d3189e 100644 --- a/test_integration/web/web_shared.dart +++ b/test_integration/web/web_shared.dart @@ -1,3 +1,5 @@ +import 'dart:html'; + import 'package:googleapis_auth/auth_browser.dart'; const _clientId = String.fromEnvironment('clientId'); @@ -8,3 +10,9 @@ ClientId clientId() { } return ClientId(_clientId); } + +final _textArea = querySelector('textarea') as TextAreaElement; + +void logToTextArea(Object value) { + _textArea.text = '$value\n\n\n${_textArea.text!}'; +}