Authenticate with OAuth

View on GitHubSample viewer app

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).

Image of authenticate with OAuth

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

  1. Create an AuthenticatorState and set the OAuthConfiguration with the portal URL, client ID, and redirect URI.

  2. Call the toolkit's DialogAuthenticator composable at the top level of your view hierarchy and pass the AuthenticatorState object. Because the AuthenticatorState object has an OAuthConfiguration set, the DialogAuthenticator will prompt for OAuth credentials when the associated Portal is loaded.

  3. Load a PortalItem(...) with connection type Portal.Connection.Authenticated which will issue an authentication challenge.

  4. Configure the manifest.xml to handle the OAuth redirect URI.

    • Define a second activity in the manifest.xml with the a name OAuthUserSignInActivity 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 and host 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" />

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

MainActivity.ktMainActivity.ktMapViewModel.ktMainScreen.kt
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* 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.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)
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.