Authenticate with ArcGIS Online (or your own portal) using OAuth2 to access secured resources (such as private web maps or layers). Accessing secured items requires logging in to the portal that hosts them (an ArcGIS Online account, for example).
Use case
Your app may need to access items that are only shared with authorized users. For example, your organization may host private data layers or feature services that are only accessible to verified users. You may also need to take advantage of premium ArcGIS Online services, such as geocoding or routing services, which require a named user login.
How to use the sample
When you run the sample, the app will load a web map which contains premium content. You will be challenged for an ArcGIS Online login to view the private layers. Enter a user name and password for an ArcGIS Online named user account (such as your ArcGIS for Developers account). If you authenticate successfully, the traffic layer will display, otherwise the map will contain only the public basemap layer.
How it works
-
Create an
AuthenticatorState
and set theOAuthConfiguration
with the portal URL, client ID, and redirect URI. -
Call the toolkit's
DialogAuthenticator
composable at the top level of your view hierarchy and pass theAuthenticatorState
object. Because theAuthenticatorState
object has anOAuthConfiguration
set, theDialogAuthenticator
will prompt for OAuth credentials when the associatedPortal
is loaded. -
Load a
PortalItem(...)
with connection typePortal.Connection.Authenticated
which will issue an authentication challenge. -
Configure the manifest.xml to handle the OAuth redirect URI.
- Define a second activity in the
manifest.xml
with the a nameOAuthUserSignInActivity
from the toolkit
<activity android:name="com.arcgismaps.toolkit.authentication.OAuthUserSignInActivity" android:launchMode="singleTop"> <!--keeps only one instance to the top of the stack-->
- Set the
<intent-filter>
categories tags to be able to launch a custom browser tab.
<intent-filter> <action android:name="android.intent.action.VIEW" /> <!--required to launch a custom browser tab--> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" />
- Set the
data
andhost
tags to be able to use the redirect URI to navigate back to the app after prompting for OAuth credentials.
<data android:host="auth" android:scheme="my-ags-app" />
- Define a second activity in the
To learn more on setting up the data specification to an intent filter, visit the Android docs.
Relevant API
- ArcGISAuthenticationChallengeHandler
- ArcGISAuthenticationChallengeResponse
- AuthenticationManager
- AuthenticatorState
- DialogAuthenticator
- OAuthUserConfiguration
- PortalItem
Additional information
This sample uses the toolkit's authentication module to handle authentication.
The workflow presented in this sample works for all SAML based enterprise (IWA, PKI, Okta, etc.) & social (facebook, google, etc.) identity providers for ArcGIS Online or Portal. For more information, see the topic Set up enterprise logins.
For additional information on using Oauth in your app, see the topic Authenticate with the API in Mobile and Native Named User Login.
For more information on how OAuth works visit OAuth 2.0 with ArcGIS
Tags
authentication, cloud, credential, OAuth, portal, security, toolkit
Sample Code
/* Copyright 2024 Esri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.esri.arcgismaps.sample.authenticatewithoauth
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.lifecycle.viewmodel.compose.viewModel
import com.arcgismaps.toolkit.authentication.DialogAuthenticator
import com.esri.arcgismaps.sample.authenticatewithoauth.components.MapViewModel
import com.esri.arcgismaps.sample.authenticatewithoauth.screens.MainScreen
import com.esri.arcgismaps.sample.sampleslib.theme.SampleAppTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SampleAppTheme {
AuthenticateWithOAuthApp()
}
}
}
@Composable
private fun AuthenticateWithOAuthApp() {
// create a ViewModel to handle interactions
val mapViewModel: MapViewModel = viewModel()
Surface(
color = MaterialTheme.colorScheme.background
) {
MainScreen(
sampleName = getString(R.string.authenticate_with_oauth_app_name)
)
// Displays appropriate Authentication UI when an authentication challenge is issued.
// Because the authenticatorState has an oAuthUserConfiguration set, authentication
// challenges will happen via OAuth.
// Call the DialogAuthenticator composable function at the top level of your view
// hierarchy, for example at the same level as MainScreen(). This ensures that
// authentication handling is set up before any components of the ArcGIS Maps SDK that
// may require authentication are used.
DialogAuthenticator(authenticatorState = mapViewModel.authenticatorState)
}
}
}