|
| 1 | +// |
| 2 | +// Copyright 2024 Esri |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import 'package:arcgis_maps/arcgis_maps.dart'; |
| 18 | +import 'package:flutter/material.dart'; |
| 19 | + |
| 20 | +import '../../utils/sample_state_support.dart'; |
| 21 | + |
| 22 | +class AuthenticateWithTokenSample extends StatefulWidget { |
| 23 | + const AuthenticateWithTokenSample({super.key}); |
| 24 | + |
| 25 | + @override |
| 26 | + State<AuthenticateWithTokenSample> createState() => |
| 27 | + _AuthenticateWithTokenSampleState(); |
| 28 | +} |
| 29 | + |
| 30 | +class _AuthenticateWithTokenSampleState |
| 31 | + extends State<AuthenticateWithTokenSample> |
| 32 | + with SampleStateSupport |
| 33 | + implements ArcGISAuthenticationChallengeHandler { |
| 34 | + // Create a controller for the map view. |
| 35 | + final _mapViewController = ArcGISMapView.createController(); |
| 36 | + |
| 37 | + @override |
| 38 | + void initState() { |
| 39 | + super.initState(); |
| 40 | + |
| 41 | + // This class implements the ArcGISAuthenticationChallengeHandler interface, |
| 42 | + // which allows it to handle authentication challenges via calls to its |
| 43 | + // handleArcGISAuthenticationChallenge() method. |
| 44 | + ArcGISEnvironment |
| 45 | + .authenticationManager.arcGISAuthenticationChallengeHandler = this; |
| 46 | + } |
| 47 | + |
| 48 | + @override |
| 49 | + void dispose() { |
| 50 | + // We do not want to handle authentication challenges outside of this sample, |
| 51 | + // so we remove this as the challenge handler. |
| 52 | + ArcGISEnvironment |
| 53 | + .authenticationManager.arcGISAuthenticationChallengeHandler = null; |
| 54 | + |
| 55 | + // Log out by removing all credentials. |
| 56 | + ArcGISEnvironment.authenticationManager.arcGISCredentialStore.removeAll(); |
| 57 | + |
| 58 | + super.dispose(); |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + Widget build(BuildContext context) { |
| 63 | + return Scaffold( |
| 64 | + resizeToAvoidBottomInset: false, |
| 65 | + // Add a map view to the widget tree and set a controller. |
| 66 | + body: ArcGISMapView( |
| 67 | + controllerProvider: () => _mapViewController, |
| 68 | + onMapViewReady: onMapViewReady, |
| 69 | + ), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + void onMapViewReady() async { |
| 74 | + // Set a portal item map that has a secure layer (traffic). |
| 75 | + // Loading the secure layer will trigger an authentication challenge. |
| 76 | + _mapViewController.arcGISMap = ArcGISMap.withItem( |
| 77 | + PortalItem.withPortalAndItemId( |
| 78 | + portal: Portal.arcGISOnline(connection: PortalConnection.authenticated), |
| 79 | + itemId: 'e5039444ef3c48b8a8fdc9227f9be7c1', |
| 80 | + ), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + @override |
| 85 | + void handleArcGISAuthenticationChallenge( |
| 86 | + ArcGISAuthenticationChallenge challenge) async { |
| 87 | + // Show a login dialog to handle the authentication challenge. |
| 88 | + await showDialog( |
| 89 | + context: context, |
| 90 | + builder: (context) => LoginWidget(challenge: challenge), |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// A widget that handles an authentication challenge by prompting the user to log in. |
| 96 | +class LoginWidget extends StatefulWidget { |
| 97 | + final ArcGISAuthenticationChallenge challenge; |
| 98 | + |
| 99 | + const LoginWidget({ |
| 100 | + super.key, |
| 101 | + required this.challenge, |
| 102 | + }); |
| 103 | + |
| 104 | + @override |
| 105 | + State<LoginWidget> createState() => _LoginWidgetState(); |
| 106 | +} |
| 107 | + |
| 108 | +class _LoginWidgetState extends State<LoginWidget> { |
| 109 | + // Controllers for the username and password text fields. |
| 110 | + final _usernameController = TextEditingController(); |
| 111 | + final _passwordController = TextEditingController(); |
| 112 | + // An error message to display. |
| 113 | + String? _error; |
| 114 | + // The result: true if the user logged in, false if the user canceled. |
| 115 | + bool? _result; |
| 116 | + |
| 117 | + @override |
| 118 | + void dispose() { |
| 119 | + // If the widget was dismissed without a result, the challenge should fail. |
| 120 | + if (_result == null) widget.challenge.continueAndFail(); |
| 121 | + |
| 122 | + // Text editing controllers must be disposed. |
| 123 | + _usernameController.dispose(); |
| 124 | + _passwordController.dispose(); |
| 125 | + |
| 126 | + super.dispose(); |
| 127 | + } |
| 128 | + |
| 129 | + @override |
| 130 | + Widget build(BuildContext context) { |
| 131 | + return Dialog( |
| 132 | + child: Padding( |
| 133 | + padding: const EdgeInsets.all(20.0), |
| 134 | + child: SingleChildScrollView( |
| 135 | + child: Column( |
| 136 | + mainAxisSize: MainAxisSize.min, |
| 137 | + children: [ |
| 138 | + Text( |
| 139 | + 'Authentication Required', |
| 140 | + style: Theme.of(context).textTheme.titleLarge, |
| 141 | + ), |
| 142 | + // Show the server URL that is requiring authentication. |
| 143 | + Text(widget.challenge.requestUri.toString()), |
| 144 | + // Text fields for the username and password. |
| 145 | + TextField( |
| 146 | + controller: _usernameController, |
| 147 | + autocorrect: false, |
| 148 | + decoration: const InputDecoration(hintText: 'Username'), |
| 149 | + ), |
| 150 | + TextField( |
| 151 | + controller: _passwordController, |
| 152 | + autocorrect: false, |
| 153 | + obscureText: true, |
| 154 | + decoration: const InputDecoration(hintText: 'Password'), |
| 155 | + ), |
| 156 | + const SizedBox(height: 10.0), |
| 157 | + // Buttons to cancel or log in. |
| 158 | + Row( |
| 159 | + children: [ |
| 160 | + ElevatedButton( |
| 161 | + onPressed: cancel, |
| 162 | + child: const Text('Cancel'), |
| 163 | + ), |
| 164 | + const Spacer(), |
| 165 | + ElevatedButton( |
| 166 | + onPressed: login, |
| 167 | + child: const Text('Login'), |
| 168 | + ), |
| 169 | + ], |
| 170 | + ), |
| 171 | + // Display an error message if there is one. |
| 172 | + Text( |
| 173 | + _error ?? '', |
| 174 | + style: const TextStyle(color: Colors.red), |
| 175 | + ), |
| 176 | + ], |
| 177 | + ), |
| 178 | + ), |
| 179 | + ), |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + void login() async { |
| 184 | + setState(() => _error = null); |
| 185 | + |
| 186 | + // Username and password are required. |
| 187 | + final username = _usernameController.text; |
| 188 | + if (username.isEmpty) { |
| 189 | + setState(() => _error = 'Username is required.'); |
| 190 | + return; |
| 191 | + } |
| 192 | + final password = _passwordController.text; |
| 193 | + if (password.isEmpty) { |
| 194 | + setState(() => _error = 'Password is required.'); |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + try { |
| 199 | + // Attempt to create a credential with the provided username and password. |
| 200 | + final credential = await TokenCredential.createWithChallenge( |
| 201 | + widget.challenge, |
| 202 | + username: username, |
| 203 | + password: password, |
| 204 | + ); |
| 205 | + if (!mounted) return; |
| 206 | + |
| 207 | + // If successful, continue with the credential. |
| 208 | + widget.challenge.continueWithCredential(credential); |
| 209 | + Navigator.of(context).pop(_result = true); |
| 210 | + } on ArcGISException catch (e) { |
| 211 | + // If there was an error, display the error message. |
| 212 | + setState(() => _error = e.message); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + void cancel() { |
| 217 | + // If the user cancels, cancel the challenge and dismiss the dialog. |
| 218 | + widget.challenge.cancel(); |
| 219 | + Navigator.of(context).pop(_result = false); |
| 220 | + } |
| 221 | +} |
0 commit comments