-
Notifications
You must be signed in to change notification settings - Fork 11
[New] Authenticate with token #613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
50befe6
Add new sample
des12437 3a649d7
Update authenticate-with-token1.png
des12437 9067e28
Update README.metadata.json
des12437 46666e2
Sort by name
des12437 fe08a28
Address PR feedback
des12437 74a29b0
Add step
des12437 23182ed
Add setup and teardown methods
des12437 4a513b7
Merge branch 'v.next' into destiny/Authenticate-with-token
des12437 f226aa9
Sort by name
des12437 ea47180
Merge branch 'v.next' into destiny/Authenticate-with-token
des12437 37dc8db
sort by name
des12437 71923ee
refactor sample
des12437 db4e322
Merge branch 'v.next' into destiny/Authenticate-with-token
des12437 c922607
check layer name
des12437 bbdf803
check layer instance
des12437 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
Shared/Samples/Authenticate with token/AuthenticateWithTokenView.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright 2025 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 | ||
// | ||
// https://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. | ||
|
||
import ArcGIS | ||
import ArcGISToolkit | ||
import SwiftUI | ||
|
||
struct AuthenticateWithTokenView: View { | ||
/// The authenticator to handle authentication challenges. | ||
@StateObject private var authenticator = Authenticator() | ||
|
||
/// The loaded map result. | ||
@State private var mapLoadResult: Result<Map, Error>? | ||
|
||
/// Creates the map for this sample. | ||
static func makeMap() -> Map { | ||
// The portal to authenticate with named user. | ||
let portal = Portal(url: .portal, connection: .authenticated) | ||
|
||
// The portal item to be displayed on the map. | ||
let portalItem = PortalItem( | ||
portal: portal, | ||
id: .trafficMap | ||
) | ||
|
||
// Creates map with portal item. | ||
return Map(item: portalItem) | ||
} | ||
|
||
var body: some View { | ||
Group { | ||
if let mapLoadResult = mapLoadResult { | ||
switch mapLoadResult { | ||
case .success(let value): | ||
MapView(map: value) | ||
case .failure(let error): | ||
Text("Error loading map: \(errorString(for: error))") | ||
des12437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.padding() | ||
} | ||
} else { | ||
ProgressView() | ||
.task { | ||
mapLoadResult = await Result { @MainActor in | ||
des12437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let map = Self.makeMap() | ||
try await map.load() | ||
try await map.operationalLayers.first?.load() | ||
des12437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return map | ||
} | ||
} | ||
} | ||
} | ||
.authenticator(authenticator) | ||
.onAppear { | ||
// Setting the challenge handlers here in `onAppear` so user is prompted to enter | ||
// credentials every time trying the sample. In real world applications, set challenge | ||
// handlers at the start of the application. | ||
|
||
// Sets authenticator as ArcGIS and Network challenge handlers to handle authentication | ||
// challenges. | ||
ArcGISEnvironment.authenticationManager.handleChallenges(using: authenticator) | ||
|
||
// In real world applications, uncomment this code to persist credentials in the | ||
// keychain and remove `signOut()` from `onDisappear`. | ||
// setupPersistentCredentialStorage() | ||
} | ||
.onDisappear { | ||
// Resetting the challenge handlers and clearing credentials here in `onDisappear` | ||
// so user is prompted to enter credentials every time trying the sample. In real | ||
// world applications, do these from sign out functionality of the application. | ||
|
||
// Resets challenge handlers. | ||
ArcGISEnvironment.authenticationManager.handleChallenges(using: nil) | ||
|
||
signOut() | ||
des12437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
/// The string describing an error. | ||
/// - Parameter error: The error. | ||
private func errorString(for error: Error) -> String { | ||
if error is CancellationError { | ||
return "User cancelled error" | ||
} else if let error = error as? ArcGISError { | ||
return error.details | ||
} else { | ||
return error.localizedDescription | ||
} | ||
des12437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Signs out from the portal by revoking OAuth tokens and clearing credential stores. | ||
private func signOut() { | ||
Task { | ||
await ArcGISEnvironment.authenticationManager.revokeOAuthTokens() | ||
des12437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
await ArcGISEnvironment.authenticationManager.clearCredentialStores() | ||
} | ||
} | ||
|
||
/// Sets up new ArcGIS and Network credential stores that will be persisted in the keychain. | ||
private func setupPersistentCredentialStorage() { | ||
Task { | ||
try await ArcGISEnvironment.authenticationManager.setupPersistentCredentialStorage( | ||
access: .whenUnlockedThisDeviceOnly, | ||
synchronizesWithiCloud: false | ||
) | ||
} | ||
} | ||
} | ||
|
||
private extension URL { | ||
/// The URL of the portal to authenticate. | ||
/// - Note: If you want to use your own portal, provide URL here. | ||
static let portal = URL(string: "https://www.arcgis.com")! | ||
} | ||
|
||
private extension PortalItem.ID { | ||
/// The portal item ID of a web map to be displayed on the map. | ||
static var trafficMap: Self { Self("e5039444ef3c48b8a8fdc9227f9be7c1")! } | ||
} | ||
|
||
#Preview { | ||
AuthenticateWithTokenView() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Authenticate with token | ||
|
||
Access a web map that is secured with ArcGIS token-based authentication. | ||
|
||
 | ||
 | ||
|
||
## Use case | ||
|
||
Allows you to access a secure service with the convenience and security of ArcGIS token-based authentication. For example, rather than providing a user name and password every time you want to access a secure service, you only provide those creditials initially to obtain a token which then can be used to access secured resources. | ||
|
||
## How to use the sample | ||
|
||
Once you launch the app, you will be challenged for an ArcGIS Online login to view the protected map service. 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 protected map service will display in the map. | ||
|
||
## How it works | ||
|
||
1. Create a toolkit component `Authenticator` object. | ||
des12437 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
2. Create a `Portal`. | ||
3. Create a `PortalItem` for the protected web map using the Portal and Item ID of the protected map service. | ||
4. Create a map to display in the MapView using the `PortalItem`. | ||
5. Set the map to display in the `MapView`. | ||
6. Set authenticator object as ArcGIS and Network challenge handlers on authentication manager to handle authentication challenges. | ||
|
||
## Relevant API | ||
|
||
* AuthenticationManager | ||
* Authenticator | ||
* Map | ||
* MapView | ||
* Portal | ||
* PortalItem | ||
|
||
## About the data | ||
|
||
The [Traffic web map](https://arcgisruntime.maps.arcgis.com/home/item.html?id=e5039444ef3c48b8a8fdc9227f9be7c1) uses public layers as well as the world traffic (premium content) layer. The world traffic service presents historical and near real-time traffic information for different regions in the world. The data is updated every 5 minutes. This map service requires an ArcGIS Online organizational subscription. | ||
|
||
## Additional information | ||
|
||
Please note: the username and password are case sensitive for token-based authentication. If the user doesn't have permission to access all the content within the portal item, partial or no content will be returned. | ||
|
||
## Tags | ||
|
||
authentication, cloud, portal, remember, security |
35 changes: 35 additions & 0 deletions
35
Shared/Samples/Authenticate with token/README.metadata.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"category": "Cloud and Portal", | ||
"description": "Access a web map that is secured with ArcGIS token-based authentication.", | ||
"ignore": false, | ||
"images": [ | ||
"authenticate-with-token1.png", | ||
"authenticate-with-token2.png" | ||
], | ||
"keywords": [ | ||
"authentication", | ||
"cloud", | ||
"portal", | ||
"remember", | ||
"security", | ||
"AuthenticationManager", | ||
"Authenticator", | ||
"Map", | ||
"MapView", | ||
"Portal", | ||
"PortalItem" | ||
], | ||
"redirect_from": [], | ||
"relevant_apis": [ | ||
"AuthenticationManager", | ||
"Authenticator", | ||
"Map", | ||
"MapView", | ||
"Portal", | ||
"PortalItem" | ||
], | ||
"snippets": [ | ||
"AuthenticateWithTokenView.swift" | ||
], | ||
"title": "Authenticate with token" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.