A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.arcgis.com/kotlin/security-and-authentication/ below:

Security and authentication | ArcGIS Maps SDK for Kotlin

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:

Security and authentication guide

Be careful to adequately secure client secrets, access tokens, and user credentials. Never embed such information in a client application where it can be discovered by viewing the source code or using developer tools. See Security best practices for more information.

Choose a type of authentication

The following considerations can help determine which type of authentication to implement:

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.

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:

To authorize your app to access secured resources, obtain a API key access token using the Create an API key tutorial. Set the API key access token on the ArcGISEnvironment.

Use dark colors for code blocks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        ArcGISEnvironment.apiKey = ApiKey.create("YOUR_ACCESS_TOKEN")

        setContent {
            MaterialTheme {
                val map = remember {
                    ArcGISMap(basemapStyle = BasemapStyle.ArcGISTopographic)
                }

                MapView(
                    modifier = Modifier.fillMaxSize(),
                    arcGISMap = map
                )
            }
        }
    }
}

You will be able to view the app's usage telemetry on the respective ArcGIS Location Platform or ArcGIS Online account. You can also set the API key access token on any class that implements ApiKeyResource. This will override any access token you have set on the ArcGISEnvironment and will enable more granular usage telemetry.

Use dark colors for code blocks

1
2
3
4
5
6
7
8
    val map = remember {
        // Create a new ArcGIS basemap and apply an access token.
        val basemap = Basemap(basemapStyle = BasemapStyle.ArcGISTopographic)
        basemap.apiKey = ApiKey.create("YOUR_ACCESS_TOKEN")

        // Create a new map with the basemap.
        ArcGISMap(basemap = basemap)
    }

Classes that implement ApiKeyResource include:

Attention

All API keys created before June 2024 are known as API keys (legacy), or legacy API keys. Legacy API keys can no longer be created, and will cease to function after May 2026. See Update to API key credentials for instructions on how to migrate from legacy API keys to API key credentials.

For more information, see API key (legacy) retirement.

Learn more about API key authentication 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:

Note

You cannot access ArcGIS location services by authenticating with an ArcGIS Enterprise user account.

Learn more about user authentication 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:

Note

App authentication access tokens, generated using an ArcGIS Enterprise account, do not provide access to ArcGIS location services.

Learn more about app authentication

In this SDK, all aspects of ArcGIS and network authentication have been encapsulated into a single ArcGIS Maps SDK for Kotlin toolkit component called the Authenticator. This component supports multiple types of authentication challenges, including ArcGIS authentication methods (OAuth, Identity-Aware Proxy (IAP), and ArcGIS token), Integrated Windows Authentication (IWA), and Client Certificate (PKI). It also provides default user interfaces for login prompts, certificate selection prompts, and server trust prompts. For example, here is the default alert prompting the user for username and password credentials.

The Authenticator composable is designed to be displayed on top of your app's UI. It should be called at a near-root level; for example, at the same level as a NavHost or the Scaffold containing your map or scene view. The Authenticator should also be the last call of the function so it draws over other content. Username/password and server trust challenges are able to adapt to the size of the container, allowing flexible layout integration.

A toolkit composable named DialogAuthenticator provides the same functionality as Authenticator, but displays the username/password prompt and the server trust UI in a dialog.

All other authentication challenges—including OAuth, IAP, and client certificate prompts—are handled consistently across both composables, using the browser or Android certificate picker as appropriate.

The toolkit contains an essential related class named AuthenticatorState, which handles authentication challenges and exposes state that the Authenticator (or DialogAuthenticator) displays to the end user. You create an AuthenticatorState instance by calling the AuthenticatorState() composable.

  1. Call AuthenticatorState() to create an implementation of the AuthenticatorState interface. Then pass the authenticator state to the Authenticator composable, which should be invoked after MapView so that the login screen will hide the map view by displaying on top of it.

    Use dark colors for code blocks

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    @Composable
    fun Screen() {
    
        val authenticatorState by remember { mutableStateOf(AuthenticatorState()) }
    
    
    
        authenticatorState.oAuthUserConfigurations = listOf(
            OAuthUserConfiguration(
                portalUrl = "https://www.arcgis.com",
                clientId = "Your client ID goes here",
                redirectUrl = "Your redirect URL goes here"
            )
        )
    
        val map = remember {
            createMap()
        }
    
        Scaffold {
            MapView(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(it),
                arcGISMap = map
            )
        }
    
        DialogAuthenticator(authenticatorState = authenticatorState)
    
    }
    
  2. If the authenticator is going to use OAuth or Identity-Aware Proxy (IAP), you must specify the OAuthConfiguration or IapConfiguration, add it to a list, and assign the list to the corresponding property on AuthenticatorState.

    Use dark colors for code blocks

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
        authenticatorState.oAuthUserConfigurations = listOf(
            OAuthUserConfiguration(
                portalUrl = "https://www.arcgis.com",  // or URL of your ArcGIS Enterprise or ArcGIS Location Platform portal.
                clientId = "Your client ID goes here",
                redirectUrl = "Your redirect URL goes here"
            )
        )
    

    Use dark colors for code blocks

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
                val iapConfiguration =
                    IapConfiguration.create("Your IAP configuration JSON file path goes here")
                        .getOrThrow()
                authenticatorState.iapConfigurations = listOf(iapConfiguration)
    
  3. You can also create a credential store that will be persistent between sessions. If the application is restarted, the credential store is automatically pre-populated with stored credentials, and the user does not have to sign in again. In this code, you assign the new credential store to ArcGISEnvironment.authenticatorManager.arcGISCredentialStore. Configuration set on the AuthenticationManager will be used by Authenticator and DialogAuthenticator.

    Use dark colors for code blocks

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
                ArcGISEnvironment.authenticationManager.arcGISCredentialStore =
                    ArcGISCredentialStore.createWithPersistence().getOrThrow()
    
  4. During application sign-out all tokens should be revoked and all credentials cleared from the credentials store. AuthenticatorState.signOut() revokes the tokens and clears the credentials store.

    Use dark colors for code blocks

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
                arcGISCredentialStore.getCredentials().forEach {
                    when {
                        it is OAuthUserCredential -> {
                            it.revokeToken()
                        }
    
                        it is IapCredential -> {
                            it.invalidate { iapSignOut ->
                                promptForIapSignOut(iapSignOut)
                            }
                        }
                    }
                }
                networkCredentialStore.removeAll()
                arcGISCredentialStore.removeAll()
    

To see the Authenticator in action, including the use of OAuth, check out the Authentication Microapp.

Authentication manager

If you do not want to use the Authenticator toolkit component, you can manage the authentication process in your application code using the AuthenticationManager. This is available as a static property on the ArcGISEnvironment.

Use dark colors for code blocks Copy

1
ArcGISEnvironment.authenticationManager

The AuthenticationManager provides:

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:

You can catch and respond to these authentication challenges using the ArcGISAuthenticationChallengeHandler and NetworkAuthenticationChallengeHandler, respectively. These are functional interfaces that each implement a single abstract method called handleArcGISAuthenticationChallenge() and handleNetworkAuthenticationChallenge() respectively. Instead of creating a class that implements the interface, you can use a lambda expression.

ArcGIS authentication challenge handler

The ArcGISAuthenticationChallengeHandler is used to handle authentication challenges from ArcGIS secured resources that require OAuth, Identity-Aware Proxy (IAP), or ArcGIS Token authentication. The recommended way to handle authentication changes is to use the Authenticator component in the ArcGIS Maps SDK for Kotlin toolkit. If you choose not to use Authenticator, you can handle the challenge in your code and return one of the following subclasses of the ArcGISAuthenticationChallengeResponse sealed class:

  1. Create a custom ArcGISAuthenticationChallengeHandler and assign it to the AuthenticationManager.arcGISAuthenticationChallengeHandler.

    Use dark colors for code blocks Copy

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
                ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler =
                    ArcGISAuthenticationChallengeHandler { challenge ->
                        when (challenge.type) {
                            ArcGISAuthenticationChallengeType.Iap -> {
                                val matchingIapConfiguration = iapConfigurations.first {
                                    it.canBeUsedForUrl(challenge.requestUrl)
                                }
                                val iapCredential =
                                    IapCredential.create(matchingIapConfiguration) { iapSignIn ->
                                        // Prompt to open browser for IAP sign in
                                        // ...
                                    }.getOrThrow()
                                ArcGISAuthenticationChallengeResponse.ContinueWithCredential(
                                    iapCredential
                                )
                            }
    
                            ArcGISAuthenticationChallengeType.OAuthOrToken -> {
                                val matchingOAuthUserConfiguration = oAuthUserConfigurations.first {
                                    it.canBeUsedForUrl(challenge.requestUrl)
                                }
                                val oAuthUserCredential =
                                    OAuthUserCredential.create(matchingOAuthUserConfiguration) { oAuthUserSignIn ->
                                        // Prompt to open browser and sign in with Oauth username and password.
                                        // ...
                                }.getOrThrow()
                                ArcGISAuthenticationChallengeResponse.ContinueWithCredential(
                                    oAuthUserCredential
                                )
                            }
    
                            ArcGISAuthenticationChallengeType.Token -> {
                                val tokenCredential =
                                    TokenCredential.createWithChallenge(challenge, username, password)
                                        .getOrThrow()
                                ArcGISAuthenticationChallengeResponse.ContinueWithCredential(
                                    tokenCredential
                                )
                            }
                        }
                    }
    
Network authentication challenge handler

The NetworkAuthenticationChallengeHandler is used to handle authentication challenges from ArcGIS secured resources that require network credentials, such as Integrated Windows Authentication (IWA) or Public Key Infrastructure (PKI). Handle the challenge by and return one of the following subclasses of the NetworkAuthenticationChallengeResponse sealed class.

Create a custom NetworkAuthenticationChallengeHandler and pass it to the AuthenticationManager.networkAuthenticationChallengeHandler.

Use dark colors for code blocks Copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ArcGISEnvironment.authenticationManager.networkAuthenticationChallengeHandler =
    NetworkAuthenticationChallengeHandler { authenticationChallenge ->
        when (authenticationChallenge.networkAuthenticationType) {
            is NetworkAuthenticationType.UsernamePassword ->
                // Create the Password Credential
                val credential = PasswordCredential(username, password)
                NetworkAuthenticationChallengeResponse.ContinueWithCredential(credential)
            is NetworkAuthenticationType.ServerTrust ->
                NetworkAuthenticationChallengeResponse.ContinueWithCredential(ServerTrust)
            is NetworkAuthenticationType.Certificate -> {
                val selectedAlias = showCertificatePicker(activityContext)
                selectedAlias?.let {
                    NetworkAuthenticationChallengeResponse.ContinueWithCredential(CertificateCredential(it))
                } ?: NetworkAuthenticationChallengeResponse.ContinueAndFail
            }
        }
    }
Create and store credentials

When an authentication challenge is raised, your application can create a credential that is held in the ArcGIS and network credential stores provided by the AuthenticationManager.

These credential stores exist for the lifetime of the application. They ensure that an authentication challenge is only raised if a matching credential does not exist in the store. If you want to avoid prompting users for credentials between application sessions, persist the credential stores using the companion functions ArcGISCredentialStore.createWithPersistence() and NetworkCredentialStore.createWithPersistence(). This uses Android's EncryptedSharedPreferences.

Use dark colors for code blocks Copy

1
2
3
4
5
6
7
lifecycleScope.launch {
  val arcGISCredentialStore = ArcGISCredentialStore.createWithPersistence().getOrThrow()
  ArcGISEnvironment.authenticationManager.arcGISCredentialStore = arcGISCredentialStore

  val networkCredentialStore = NetworkCredentialStore.createWithPersistence().getOrThrow()
  ArcGISEnvironment.authenticationManager.networkCredentialStore = networkCredentialStore
}

During application sign-out you should revoke all tokens and clear all credentials from the credential stores.

Use dark colors for code blocks Copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    val arcGISCredentialStore = ArcGISEnvironment.authenticationManager.arcGISCredentialStore
    val networkCredentialStore =
        ArcGISEnvironment.authenticationManager.networkCredentialStore
        arcGISCredentialStore.getCredentials ().forEach {
            when {
                it is OAuthUserCredential -> {
                    it.revokeToken()
                }

                it is IapCredential -> {
                    it.invalidate { iapSignOut ->
                        promptForIapSignOut(iapSignOut)
                    }
                }
        }
        networkCredentialStore.removeAll()
        arcGISCredentialStore.removeAll()
Note

If your application is using an IapCredential, a Microsoft page will be presented that allows the user to sign-out. Note that after a successful sign-out, the user must click the Cancel button to return to your application.

ArcGIS credentials

You can access secure resources with user authentication or app authentication using any of the following credential types:

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, instantiate an OAuthUserConfiguration with a valid portal URL, client ID, and redirect URL, and wrap the instance in a list. You must present a prompt, in a browser supported by the device, for the user to enter their username and password. The response from the browser should be handled within your activity or the fragment that launched the browser prompt. Once the OAuthUserCredential is created, you will be able to access the token information by calling getTokenInfo() on the credential.

Use dark colors for code blocks Copy

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
        // Define OAuthConfiguration
        val oAuthUserConfiguration = OAuthUserConfiguration(
            "<portal URL>",
            "<ClientID>",
            "<redirectURI>"
        )

        lifecycleScope.launch {
            val oAuthUserCredential = OAuthUserCredential.create(oAuthUserConfiguration) { oAuthUserSignIn ->
                promptForOAuthUserSignIn(oAuthUserSignIn)
            }.getOrThrow()

            oAuthUserCredential.getTokenInfo()
        }

See the Android documentation for details on how to launch custom chrome tabs in android and work with getting result from an activity. If you use the Authenticator in the Toolkit, all this is handled for you.

To see this authentication in action, look at the Authenticate with OAuth sample.

IapCredential

The IapCredential supports access to an ArcGIS Enterprise portal and its services that are protected behind an Identity-Aware Proxy (IAP). You must present a prompt, in a browser supported by the device, for the user to enter their username and password. The response from the browser should be handled within your activity or the fragment that launched the browser prompt.

To initiate an IAP authorization workflow, create an IapConfiguration to store your application's registration details and use it to create an IapCredential. Follow the steps below to create an IapCredential:

  1. To establish trust between your application and the IAP, you administrator registers your application with the Microsoft Identity Platform. Following this, you will be able to review the client ID, tenant ID, redirect url, and so on.

  2. Create the IapConfiguration using these app registration details. Depending on your workflow, you can initialize an IapConfiguration by either providing the details in a JSON file or supplying them directly in the application's code.

    1. To initialize the IapConfiguration with a JSON file, create a JSON file containing the following key/value pairs:

      Use dark colors for code blocks Copy

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      {
       "authorize_url" : "https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/authorize",
       "token_url" : "https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token",
       "logout_url" : "https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/logout",
       "client_id" : "<client_id>",
       "redirect_url" : "<redirect_url>",
       "scope" : [
           "<client_id>/.default",
           "offline_access",
           "openid",
           "profile"
       ],
       "hosts_behind_proxy" : ["*.domain.com"],
       "authorization_prompt_type" : "<empty string, none, login, consent, or select_account>"
      }

      The keys are defined as follows:

      • authorize_url - The authorize endpoint of the Identity-Aware Proxy (IAP) portal.
      • token_url - The token endpoint of the Identity-Aware Proxy (IAP) portal.
      • logout_url - The logout endpoint of the Identity-Aware Proxy (IAP) portal.
      • client_id - A unique identifier associated with an application which is registered with the Identity-Aware Proxy (IAP) portal.
      • redirect_url - The URL that the Identity-Aware Proxy (IAP) login and logout pages will redirect to when authentication completes. The scheme of this URL must be registered as a custom URL scheme in the application.
      • scope - The scope of user's account.
      • hosts_behind_proxy - The hosts to be accessed behind the Identity-Aware Proxy (IAP).
      • authorization_prompt_type - The type of user interaction required for authentication and consent while signing in to the Identity-Aware Proxy (IAP).
    2. Create the IapConfiguration using the IapConfiguration.create(path) companion function.

      Use dark colors for code blocks Copy

      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
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      161
      162
                  val iapConfigurationFromJson = IapConfiguration.create("/path/to/IAP/configuration/file.json").getOrElse { error ->
                      showMessage(error.message.toString())
                  }
      

      If the JSON file is missing any required properties, the IapConfiguration will fail.

    3. Alternatively, create the IapConfiguration by specifying each parameter in the IapConfiguration.create() companion function.

      Use dark colors for code blocks Copy

      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
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      100
      101
      102
      103
      104
      105
      106
      107
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      161
      162
                  val iapConfiguration = IapConfiguration.create(
                      authorizeUrl = "https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/authorize",
                      tokenUrl = "https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token",
                      logoutUrl = "https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/logout",
                      clientId = "<client_id>",
                      clientSecret = "<client_secret>",
                      redirectUrl = "<redirect_url>",
                      iapClientId = "<iap_client_id>",
                      scopes = listOf(
                          "<client_id>/.default", "offline_access", "openid", "profile"
                      ),
                      hostsBehindProxy = listOf("*.domain.com"),
                      authorizationPromptType = IapAuthorizationPromptType.Unspecified
                  )
      

    Best Practice: To avoid storing secure information in you app's code, the recommended approach is to use the IapConfiguration.create(path) companion function using a JSON file on disk to create a IapConfiguration.

  3. Implement the functional interface ArcGISAuthenticationChallengeHandler. Inside the lambda, check if the challenge is of type ArcGISAuthenticationChallengeType.Iap.

    Use dark colors for code blocks

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
                ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler =
                    ArcGISAuthenticationChallengeHandler { challenge ->
    
                        if (challenge.type == ArcGISAuthenticationChallengeType.Iap) {
    
                            val matchingIapConfiguration = iapConfigurations.first { iapConfiguration ->
    
                            }
    
                        }
    
                    }
    
  4. Call IapConfiguration.canBeUsedForUrl() to check if the configuration can be used for the requestUrl() contained in the challenge. A configuration can be used for a URL if the URL's host matches one of the hosts specified in the configuration's hosts behind proxy.

    Use dark colors for code blocks

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
                ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler =
                    ArcGISAuthenticationChallengeHandler { challenge ->
    
                        if (challenge.type == ArcGISAuthenticationChallengeType.Iap) {
    
                            val matchingIapConfiguration = iapConfigurations.first { iapConfiguration ->
    
                                iapConfiguration.canBeUsedForUrl(challenge.requestUrl)
    
                            }
    
                        }
    
                    }
    
  5. Create an IapCredential by calling the IapCredential.create() companion function and passing in the IapConfiguration. This will present the sign-in page for the user to enter their username and password. Once the IapCredential is created, it will be added to the ArcGISCredentialStore, and you will be able to access token information by calling getTokenInfo() on the credential.

    Use dark colors for code blocks

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
                ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler =
                    ArcGISAuthenticationChallengeHandler { challenge ->
    
                        if (challenge.type == ArcGISAuthenticationChallengeType.Iap) {
    
                            val matchingIapConfiguration = iapConfigurations.first { iapConfiguration ->
    
                                iapConfiguration.canBeUsedForUrl(challenge.requestUrl)
    
                            }
    
                            iapCredential =
                                IapCredential.create(matchingIapConfiguration) { iapSignIn ->
                                    promptForIapSignIn(iapSignIn)
                                    // ...
                                }.getOrThrow()
                            val iapTokenInfo = iapCredential.getTokenInfo().getOrThrow()
    
                            ArcGISAuthenticationChallengeResponse.ContinueWithCredential(
                                iapCredential
                            )
    
                        }
    
                        // Handle other types of ArcGIS AuthenticationChallenge.
                        else if (challenge.type == ArcGISAuthenticationChallengeType.OAuthOrToken) {
                            // ...
    
                    }
    
  6. During application sign-out, you must invalidate the credential by calling IapCredential.invalidate(). This will present a Microsoft page that allows the user to sign-out. Note that after a successful sign-out, the user must click the Cancel button to return to your application.

    Use dark colors for code blocks Copy

    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
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
                val signOutRequestResult = iapCredential.invalidate { iapSignOut ->
                    promptForIapSignOut(iapSignOut)
                }
                if (signOutRequestResult.isSuccess) {
                    showMessage("Invalidation of the IAP session was successful")
                } else {
                    showMessage("Invalidation of the IAP session failed: ${signOutRequestResult.exceptionOrNull()?.message}")
                }
    
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 by calling getTokenInfo() on the credential.

Use dark colors for code blocks Copy

1
2
3
4
5
6
7
scope.launch {
    TokenCredential.create(requestUrl,username, password, 30).onSuccess {
        // Get accesstoken from credential
        var tokenInfo = it.getTokenInfo().getOrNull()
        var accessToken = tokenInfo?.accessToken
    }
}
PregeneratedTokenCredential

To create a PregeneratedTokenCredential, provide a previously generated short or long-lived access token. Use this 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.

Use dark colors for code blocks Copy

1
2
3
var accessToken = "xxxxxxx"
var tokenInfo = val tokenInfo = TokenInfo(accessToken, Instant.now().plus(2, ChronoUnit.HOURS), true)
var pregeneratedTokenCredential = PregeneratedTokenCredential(requestUrl, tokenInfo)
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 by calling getTokenInfo() on the credential.

Use dark colors for code blocks Copy

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
        lifecycleScope.launch {
            val oAuthApplicationCredential = OAuthApplicationCredential.create(
                portalUrl = "portalUrl",
                clientId = "clientId",
                clientSecret = "clientSecret",
                tokenExpirationInterval = 30
            ).getOrNull() ?: return@launch

            val oAuthApplicationTokenInfo = oAuthApplicationCredential.getTokenInfo().getOrThrow()

            ArcGISEnvironment.authenticationManager.arcGISAuthenticationChallengeHandler =
                ArcGISAuthenticationChallengeHandler {
                    ArcGISAuthenticationChallengeResponse.ContinueWithCredential(oAuthApplicationCredential)
                }
        }
Attention

It is important that you secure the client secret like you would a password. Never embed it in a client application where it can be discovered through viewing source or developer tools.

Network credentials

You can access resources secured by network authentication using the following credential types:

PasswordCredential

The PasswordCredential is used to authenticate HTTP Basic or Integrated Windows Authentication (IWA) secured resources.

Use dark colors for code blocks Copy

1
var passwordCredential = PasswordCredential("username", "password")
CertificateCredential

CertificateCredential represents a digital client certificate used to access certificate secured resources. All the digital certificates need to be pre-installed in the device keychain. The CertificateCredential can be created using the alias of the chosen certificate.

Use dark colors for code blocks Copy

1
2
3
4
5
6
7
8
9
10
11
private suspend fun showCertificatePicker(activityContext: Activity): String? =
    suspendCancellableCoroutine { continuation ->
        val aliasCallback = KeyChainAliasCallback { alias ->
            continuation.resume(alias)
        }
        KeyChain.choosePrivateKeyAlias(
            activityContext, aliasCallback, null, null, null, null
        )
}
val selectedAlias = showCertificatePicker(activityContext)
var certificateCredential = selectedAlias?.let { CertificateCredential(it) } ?: null
Tutorials Samples

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4