ArcGIS supports secure access to ArcGIS services and portal items. It ensures that only valid, authorized users and services can access protected information. To access secure ArcGIS resources, you need an access token that you can obtain by implementing an authentication workflow in your app. The type of authentication you use will depend on the security and access requirements of your app.
There are three types of authentication that you can use to get an access token:
-
API key authentication: grants a long-lived access token to authenticate requests to ArcGIS services and secure portal items. For more information see the Introduction to API key authentication. To obtain an API Key access token, go to the Create an API key tutorial using your ArcGIS account. Here you can configure the API key privileges to authorize access to different services and portal items.
- API key access tokens created with an ArcGIS Enterprise account, from ArcGIS Enterprise in version 11.4 onwards, provide access to secure items in an ArcGIS Enterprise portal.
-
User authentication: a collection of authentication workflows that connect your app to a user's ArcGIS account.
- OAuth 2.0: manage ArcGIS authentication and grant a short-lived access token generated via OAuth 2.0. This gives your application permission to access ArcGIS secured services authorized to an existing ArcGIS user's account.
- Generate token: manages ArcGIS authentication and grants a short-lived access token generated via Esri's proprietary token-based authentication mechanism. This gives your application permission to access ArcGIS secured services authorized to an existing ArcGIS user's account.
- Network credential: manage network authentication (also known as web-tier authentication) for ArcGIS Enterprise. This gives your application permission to access network secured services authorized to your web-tier's identity store user accounts.§
§ This capability has not yet been implemented in ArcGIS Maps SDK for Flutter, but will be added in a future release. See this page for more details.
-
App authentication: uses the registered application's credentials to access location services on ArcGIS. It manages ArcGIS authentication and grants a short-lived access token generated via OAuth 2.0 using the Application item's Client ID and Client Secret outside of the context of a user.
For more information, see the Security and authentication guide.
Choose a type of authentication
The following considerations can help determine which type of authentication to implement:
-
Access to resources—Your app can access ArcGIS services and portal items using API key authentication, User authentication, or App authentication.
-
User experience—If you don't want to make users log in, your app can access ArcGIS services using API key authentication or App authentication. In this case, users will not need to have an ArcGIS account in order to use your app.
-
Usage charges—If you want service usage to be charged to the user's account, your app must request that the user log in using User authentication. When using API key authentication or App authentication, all access to services from your app will be charged to your ArcGIS account.
You might also need to consider the level of security required for your app, how your app will be distributed, and your available ArcGIS products and accounts.
Scenario | Solution |
---|---|
Your app requires access to ArcGIS services only, you don't want to make users log in, and you are willing to pay for all charges incurred from usage of the app. | API key authentication or App authentication |
Your app requires access to location services only and you want usage charged to the user. | User authentication |
Your app needs to access content that requires an ArcGIS Online subscription. | User authentication |
Your app needs to access private hosted data on your ArcGIS Location Platform account. | API key authentication or App authentication |
Your app allows users to view and edit private data hosted in ArcGIS Online or ArcGIS Enterprise. | User authentication |
You plan to distribute your app through ArcGIS Marketplace. | User authentication |
API key authentication
An API Key can grant your public-facing application access to specific ArcGIS services and portal items.
Use API key authentication when you want to:
- Quickly write applications that consume ArcGIS services.
- Provide access to services without requiring users to sign in with an ArcGIS account.
Use API key access tokens
An API key, can be used to authorize access to specific ArcGIS services and portal items. Go to the Create an API key tutorial to obtain a new access token.
If you set the access token on the ArcGISEnvironment
, all requests made by your app will be authorized using this token. You will be able to view the app's usage telemetry on the respective ArcGIS Location Platform, ArcGIS Online, or ArcGIS Enterprise account.
import 'package:arcgis_maps/arcgis_maps.dart';
import 'package:flutter/material.dart';
void main() {
ArcGISEnvironment.apiKey = 'YOUR_ACCESS_TOKEN';
}
You can also set the access token on any class that implements APIKeyResourceInterface
. When you set an access token for a specific class, it will override any access token you may have set on ArcGISEnvironment
, enabling more granular usage telemetry and management for ArcGIS Online resources used by your app.
// Create a new ArcGIS basemap and apply an access token.
final basemap = Basemap.withStyle(BasemapStyle.arcGISNavigation);
basemap.apiKey = 'YOUR_ACCESS_TOKEN';
// Create a new map with the basemap.
final map = ArcGISMap.withBasemap(basemap);
Classes that implement APIKeyResourceInterface
include:
-
ArcGISTiledLayer
-
ArcGISVectorTiledLayer
-
Basemap
-
ClosestFacilityTask
-
ExportTileCacheTask
-
ExportVectorTilesTask
-
GeodatabaseSyncTask
-
LocatorTask
-
RouteTask
-
ServiceAreaTask
-
ServiceFeatureTable
User authentication
User authentication is a set of authentication workflows that allow users with an ArcGIS account to sign into an application and access ArcGIS content, services, and resources. The typical authentication protocol used is OAuth2.0. When a user signs into an application with their ArcGIS account, an access token is generated that authorizes the application to access services and content on their behalf. The resources and functionality available depend on the user type, roles, and privileges of the user's ArcGIS account.
Services that your app accesses with user authentication will be billed to the authenticated user's ArcGIS account and its associated ArcGIS subscription. If your application will access your users' secure content in ArcGIS or if you plan to distribute your application through ArcGIS Marketplace, you must use user authentication.
Implement user authentication when you want to:
- Ensure users are signed in and authenticated with their own ArcGIS account.
- Use your app user's credits to pay for their private data, content, or service transactions.
- Limit the length of time users can be signed in to your app with a temporary token.
- Distribute your app through ArcGIS Marketplace.
App authentication
App authentication grants a short-lived access token, generated via OAuth 2.0, authorizing your application to access ArcGIS location services, such as basemap layers, search, and routing.
Use app authentication when you want to:
- Access location services with a more secure process and a short-lived token.
- Provide access to services without requiring users to have an ArcGIS account.
Authentication manager
If you want to manage the authentication process yourself, use the AuthenticationManager
that is available as a static property on the ArcGISEnvironment
.
ArcGISEnvironment.authenticationManager
The AuthenticationManager
provides:
-
ArcGIS and network challenge handlers that allow you to respond to the authentication challenges. For example, you can write code to present the user with a login screen and then continue to authenticate with those credentials. For more information, see Handle authentication challenges.
-
The credential stores are available for you to place ArcGIS and network credentials that are automatically checked when your application attempts to connect to secure resources. These stores can also be made persistent in the keychain so that user does not have to sign in again when the application is re-launched. For more information, see Create and store credentials.
Handle authentication challenges
If your application attempts to access a secure resource and there is no matching credential in the credential store, an authentication challenge is raised:
ArcGISAuthenticationChallenge
is raised if the ArcGIS secured resource requires OAuth or ArcGIS Token authentication.NetworkAuthenticationChallenge
is raised if the ArcGIS secured resource requires network credentials, such as Integrated Windows Authentication (IWA) or Public Key Infrastructure (PKI).§
§ This API has not yet been implemented in ArcGIS Maps SDK for Flutter, but will be added in a future release. See this page for more details.
Catch and respond to the authentication challenge using the ArcGISAuthenticationChallengeHandler
. This challenge handler represents a protocol that you can adopt in any class, structure, or other type.
ArcGIS authentication challenge handler
The ArcGISAuthenticationChallengeHandler
is used to handle authentication challenges from ArcGIS secured resources that require OAuth or ArcGIS Token authentication. Handle the challenge by returning one of the ArcGISAuthenticationChallenge
options:
-
continueWithCredential(ArcGISCredential)
- Continue the challenge with the given credential. -
continueAndFail()
- The request that issued the authentication challenge will fail with the challenge's error. -
cancel()
- The request that issued the authentication challenge will fail withCancellation
.Error
Create a class adopting the ArcGISAuthenticationChallengeHandler
protocol to handle ArcGIS authentication challenges.
// Defines an ArcGIS authentication challenge handler.
class _AuthenticateWithOAuthState extends State<AuthenticateWithOAuth>
implements ArcGISAuthenticationChallengeHandler {
// The OAuth configuration that this challenge handler can work with.
final _oauthUserConfiguration = OAuthUserConfiguration(
portalUri: portalUri,
clientId: clientId,
redirectUri: redirectUri,
);
// Handles the challenge to an ArcGIS secured resources that requires
// OAuth or ArcGIS Token authentication.
@override
void handleArcGISAuthenticationChallenge(
ArcGISAuthenticationChallenge challenge,
) async {
try {
// Initiate the sign in process to the OAuth server using the
// defined user configuration.
final credential = await OAuthUserCredential.create(
configuration: _oauthUserConfiguration,
);
// If sign in was successful, then continue with the provided
// credential.
challenge.continueWithCredential(credential);
} on ArcGISException catch (error) {
// The sign in was canceled or there was some other error.
final e = (error.wrappedException as ArcGISException?) ?? error;
if (e.errorType == ArcGISExceptionType.commonUserCanceled) {
// Cancel the request that initiated the challenge.
challenge.cancel();
} else {
// Handle the challenge without a credential, causing it to fail
// with the original authentication error.
challenge.continueAndFail();
}
}
}
}
Set an instance of the class on the AuthenticationManager.arcGISAuthenticationChallengeHandler
property of the ArcGISEnvironment
.
// `this` represents an instance of the class that implements the
// ArcGISAuthenticationChallengeHandler.
ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = this;
Create and store credentials
You can create a credential in the authentication challenge handler when an authentication challenge is raised. See Handle authentication challenges for more information. Store these credentials in ArcGIS credential store provided by the AuthenticationManager
.
ArcGISCredentialStore
stores ArcGIS credentials.
This credential store exists for the lifetime of the application and ensures that an authentication challenge is not raised if a matching credential exists in the store.
During application sign-out you should revoke all OAuth user tokens and then clear all credentials from the credential stores.
@override
void dispose() async {
// Remove the challenge handler from the ArcGIS environment.
ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler = null;
super.dispose();
// Clear all ArcGIS credentials from the ArcGIS credential store.
await Future.wait(
ArcGISEnvironment.authenticationManager.arcGISCredentialStore
.getCredentials()
.whereType<OAuthUserCredential>()
.map((credential) => credential.revokeToken()),
);
ArcGISEnvironment.authenticationManager.arcGISCredentialStore.removeAll();
}
ArcGIS credentials
You can access secured resources with user authentication or app authentication using the following credential types.
OAuthUserCredential
- A credential object used to access OAuth token-secured ArcGIS resources with a specificOAuthUserConfiguration
.OAuthApplicationCredential
- A credential object used to access OAuth token-secured ArcGIS resources using the application's credentials.TokenCredential
- A credential object used to access token-secured ArcGIS resources.PregeneratedTokenCredential
- A credential object used to access token-secured ArcGIS resources using a token that is generated outside of your application.
If you know the services domain/server context, you can create an ArcGIS credential, independent of loading the specific resource and store it in the ArcGISCredentialStore
.
OAuthUserCredential
To create an OAuthUserCredential
, provide an OAuthUserConfiguration
with a valid portal URL, client ID, and redirect URL. This will present the OAuth sign in page for the user to enter their username and password. Once the OAuthUserCredential
is created, you will be able to retrieve the access token information from the asynchronous getTokenInfo()
method.
final oauthUserConfiguration = OAuthUserConfiguration(
portalUri: portalUri,
clientId: 'clientId',
redirectUri: redirectUri,
);
final credential = await OAuthUserCredential.create(
configuration: oauthUserConfiguration,
);
final tokenInfo = await credential.getTokenInfo();
OAuthApplicationCredential
To create an OAuthApplicationCredential
, provide a valid portal URL, a client ID, and a client secret. Optionally, you can specify the token expiration in minutes. Once the OAuthApplicationCredential
is created, you will be able to access the token information using the asynchronous getTokenInfo()
method.
final credential = OAuthApplicationCredential.create(
portalUri: portalUri,
clientId: 'clientId',
clientSecret: 'clientSecret',
tokenExpirationInterval: 5,
);
final tokenInfo = await credential.getTokenInfo();
TokenCredential
To create a TokenCredential
, provide a secured service URL, valid username, and password. Optionally, you can specify token expiration minutes. Once a TokenCredential
is created, you will be able to access token information from the asynchronous getTokenInfo()
method.
final credential = await TokenCredential.create(
uri: uri,
username: 'username',
password: 'password',
tokenExpirationInterval: 5,
);
final tokenInfo = await credential.getTokenInfo();
PregeneratedTokenCredential
To create a PregeneratedTokenCredential
, provide a previously generated short or long-lived access token. Use this credential when the access token is created using the generateToken REST endpoint directly. You must provide the referer if one was used while generating the token.
final tokenInfo = TokenInfo.create(
accessToken: 'accessToken',
expirationDate: date,
isSslRequired: true,
);
final credential = PregeneratedTokenCredential(
uri: uri,
tokenInfo: tokenInfo,
referer: 'referer',
);