diff --git a/portal/integrated_windows_authentication/IntegratedWindowsAuthentication.png b/portal/integrated_windows_authentication/IntegratedWindowsAuthentication.png new file mode 100644 index 0000000000..482ca91be5 Binary files /dev/null and b/portal/integrated_windows_authentication/IntegratedWindowsAuthentication.png differ diff --git a/portal/integrated_windows_authentication/README.md b/portal/integrated_windows_authentication/README.md new file mode 100644 index 0000000000..8510903cff --- /dev/null +++ b/portal/integrated_windows_authentication/README.md @@ -0,0 +1,47 @@ +# Integrated windows authentication + +Use Windows credentials to access services hosted on a portal secured with Integrated Windows Authentication (IWA). + +![](IntegratedWindowsAuthentication.png) + +## Use case + +IWA, which is built into Microsoft Internet Information Server (IIS), allows users to easily authenticate for different services using their domain's Windows credentials. This simplifies logging in to services that are provided through an intranet environment, as only a single username and password combination are required for the applications that support IWA. + +## How to use the sample + +Enter a URL to your IWA-secured portal into the URL field and select "Search IWA Secured Portal" to search for web maps stored there. Authenticate with a username (including domain, such as username@DOMAIN or domain\username), and password. If authentication is successful, portal item results will display in the list. Select a web map item to display it in the map view. + +## How it works + +1. Set up an `AuthenticationChallengeHandler` that challenges the user for authentication, such as the custom handler created for this sample: `IWAChallengeHandler`. +2. Set the `AuthenticationManager` to use the created challenge handler using `.setAuthenticationChallengeHandler(new IWAChallengeHandler());`. +3. Create a new `Portal` from a portal URL and a boolean indicating if it is a secured resource using `new Portal(url, true)`. +4. Load the portal. The authentication challenge handler will prompt the user for credentials. The portal will load successfully with valid credentials. +5. Create `PortalQueryParameters` to define a search query for map items in the portal. +5. Find web map portal items using `Portal.findItemsAsync(portalQueryParameters)`. +6. Create a map using a `PortalItem` result with `new ArcGISMap(portalItem)`. + +## Relevant API + +* AuthenticationChallenge +* AuthenticationChallengeHandler +* AuthenticationChallengeResponse +* AuthenticationManager +* Portal +* UserCredential + +## About the data + +Searching a public portal with this sample retrieves publicly available web maps from www.arcgis.com. + +## Additional information + +More information about IWA and its use with ArcGIS can be found at the following links: + +* [Use Integrated Windows Authentication with your portal](http://enterprise.arcgis.com/en/portal/latest/administer/windows/use-integrated-windows-authentication-with-your-portal.htm) +* [IWA - Wikipedia](https://en.wikipedia.org/wiki/Integrated_Windows_Authentication) + +## Tags + +authentication, IWA, login, portal, security, sign-in \ No newline at end of file diff --git a/portal/integrated_windows_authentication/README.metadata.json b/portal/integrated_windows_authentication/README.metadata.json new file mode 100644 index 0000000000..82cac9a4e2 --- /dev/null +++ b/portal/integrated_windows_authentication/README.metadata.json @@ -0,0 +1,34 @@ +{ + "category": "Portal", + "description": "Use Windows credentials to access services hosted on a portal secured with Integrated Windows Authentication (IWA).", + "ignore": false, + "images": [ + "IntegratedWindowsAuthentication.png" + ], + "keywords": [ + "authentication", + "IWA", + "login", + "portal", + "security", + "sign" + ], + "redirect_from": "/java/latest/sample-code/integrated-windows-authentication.htm", + "relevant_apis": [ + "AuthenticationChallenge", + "AuthenticationChallengeHandler", + "AuthenticationChallengeResponse", + "AuthenticationManager", + "Portal", + "UserCredential" + ], + "snippets": [ + "src/main/java/com/esri/samples/integrated_windows_authentication/AuthenticationDialog.java", + "src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationController.java", + "src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationSample.java", + "src/main/java/com/esri/samples/integrated_windows_authentication/IWAChallengeHandler.java", + "src/main/resources/iwa_auth_dialog.fxml", + "src/main/resources/iwa_controller.fxml" + ], + "title": "Integrated windows authentication" +} diff --git a/portal/integrated_windows_authentication/build.gradle b/portal/integrated_windows_authentication/build.gradle new file mode 100644 index 0000000000..97e55f1b6d --- /dev/null +++ b/portal/integrated_windows_authentication/build.gradle @@ -0,0 +1,74 @@ +plugins { + id 'application' + id 'org.openjfx.javafxplugin' version '0.0.5' +} + +group = 'com.esri.samples' + +ext { + arcgisVersion = '100.5.0' +} + +javafx { + version = "11.0.1" + modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.web' ] +} + +compileJava.options.encoding = 'UTF-8' + +repositories { + jcenter() + maven { + url 'https://esri.bintray.com/arcgis' + } + maven { + url 'http://olympus.esri.com/artifactory/arcgisruntime-repo' + } +} + +configurations { + natives +} + +dependencies { + compile "com.esri.arcgisruntime:arcgis-java:$arcgisVersion" + natives "com.esri.arcgisruntime:arcgis-java-jnilibs:$arcgisVersion" + natives "com.esri.arcgisruntime:arcgis-java-resources:$arcgisVersion" +} + +task copyNatives(type: Copy) { + description = "Copies the arcgis native libraries into the project build directory for development." + group = "build" + configurations.natives.asFileTree.each { + from(zipTree(it)) + } + // store native libraries in a common location shared with other samples + into "${System.properties.getProperty("user.home")}/.arcgis/$arcgisVersion" +} + +run { + dependsOn copyNatives + mainClassName = 'com.esri.samples.integrated_windows_authentication.IntegratedWindowsAuthenticationLauncher' +} + +jar { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + manifest { + attributes("Main-Class": "$mainClassName") + } + from { + configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } + } +} + +task productionZip(type: Zip) { + group = 'distribution' + from copyNatives + from jar.destinationDir + into (project.name) + baseName = project.name +} + +wrapper { + gradleVersion = '5.0' +} diff --git a/portal/integrated_windows_authentication/gradle/wrapper/gradle-wrapper.jar b/portal/integrated_windows_authentication/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000..457aad0d98 Binary files /dev/null and b/portal/integrated_windows_authentication/gradle/wrapper/gradle-wrapper.jar differ diff --git a/portal/integrated_windows_authentication/gradle/wrapper/gradle-wrapper.properties b/portal/integrated_windows_authentication/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000..75b8c7c8c6 --- /dev/null +++ b/portal/integrated_windows_authentication/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/portal/integrated_windows_authentication/gradlew b/portal/integrated_windows_authentication/gradlew new file mode 100644 index 0000000000..af6708ff22 --- /dev/null +++ b/portal/integrated_windows_authentication/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/portal/integrated_windows_authentication/gradlew.bat b/portal/integrated_windows_authentication/gradlew.bat new file mode 100644 index 0000000000..6d57edc706 --- /dev/null +++ b/portal/integrated_windows_authentication/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/portal/integrated_windows_authentication/settings.gradle b/portal/integrated_windows_authentication/settings.gradle new file mode 100644 index 0000000000..f4392e5b67 --- /dev/null +++ b/portal/integrated_windows_authentication/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'integrated_windows_authentication' diff --git a/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/AuthenticationDialog.java b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/AuthenticationDialog.java new file mode 100644 index 0000000000..684cf97d03 --- /dev/null +++ b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/AuthenticationDialog.java @@ -0,0 +1,66 @@ +/* + * Copyright 2019 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.samples.integrated_windows_authentication; + +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.scene.control.Alert; +import javafx.scene.control.ButtonType; +import javafx.scene.control.Dialog; +import javafx.scene.control.PasswordField; +import javafx.scene.control.TextField; + +import com.esri.arcgisruntime.security.UserCredential; + +class AuthenticationDialog extends Dialog { + + @FXML private TextField userDomain; + @FXML private PasswordField password; + @FXML private ButtonType cancelButton; + @FXML private ButtonType continueButton; + + AuthenticationDialog() { + + FXMLLoader loader = new FXMLLoader(getClass().getResource("/iwa_auth_dialog.fxml")); + loader.setRoot(this); + loader.setController(this); + + setTitle("Authenticate"); + + try { + loader.load(); + } catch (Exception e) { + e.printStackTrace(); + } + + setResultConverter(dialogButton -> { + if (dialogButton == continueButton) { + if (!userDomain.getText().equals("") && !password.getText().equals("")) { + try { + return new UserCredential(userDomain.getText(), password.getText()); + } catch (Exception e) { + new Alert(Alert.AlertType.ERROR, e.getMessage()).show(); + } + } else { + new Alert(Alert.AlertType.ERROR, "Missing credentials").show(); + } + } + return null; + }); + } + +} diff --git a/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IWAChallengeHandler.java b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IWAChallengeHandler.java new file mode 100644 index 0000000000..1cb7723a66 --- /dev/null +++ b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IWAChallengeHandler.java @@ -0,0 +1,90 @@ +/* + * Copyright 2019 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.samples.integrated_windows_authentication; + +import java.util.concurrent.CountDownLatch; + +import javafx.application.Platform; +import javafx.scene.control.Alert; + +import com.esri.arcgisruntime.loadable.LoadStatus; +import com.esri.arcgisruntime.portal.Portal; +import com.esri.arcgisruntime.security.AuthenticationChallenge; +import com.esri.arcgisruntime.security.AuthenticationChallengeHandler; +import com.esri.arcgisruntime.security.AuthenticationChallengeResponse; +import com.esri.arcgisruntime.security.UserCredential; + +/** + * Handler to be used when accessing a secured resource. + */ +final class IWAChallengeHandler implements AuthenticationChallengeHandler { + + private UserCredential userCredential; + + /** + * Handles challenge before accessing a secured resource. + * + * @param authenticationChallenge the authentication challenge to handle + * @return the AuthenticationChallengeResponse indicating which action to take + */ + @Override + public AuthenticationChallengeResponse handleChallenge(AuthenticationChallenge authenticationChallenge) { + + if (authenticationChallenge.getType() == AuthenticationChallenge.Type.USER_CREDENTIAL_CHALLENGE && authenticationChallenge.getRemoteResource() instanceof Portal) { + + // If challenge has been requested by a Portal and the Portal has been loaded, cancel the challenge + // This is required as some layers have private portal items associated with them and we don't + // want to auth against them + if (((Portal) authenticationChallenge.getRemoteResource()).getLoadStatus() == LoadStatus.LOADED) { + return new AuthenticationChallengeResponse(AuthenticationChallengeResponse.Action.CANCEL, authenticationChallenge); + } + + int maxAttempts = 5; + if (authenticationChallenge.getFailureCount() > maxAttempts) { + // exceeded maximum amount of attempts. Act like it was a cancel + new Alert(Alert.AlertType.ERROR, "Exceeded maximum amount of attempts. Please try again!").show(); + return new AuthenticationChallengeResponse(AuthenticationChallengeResponse.Action.CANCEL, authenticationChallenge); + } + + // create a countdown latch with a count of one to synchronize the authentication dialog + CountDownLatch authenticationCountDownLatch = new CountDownLatch(1); + // show the authentication dialog and capture the user credentials + Platform.runLater(() -> { + AuthenticationDialog authenticationDialog = new AuthenticationDialog(); + authenticationDialog.show(); + authenticationDialog.setOnCloseRequest(r -> { + userCredential = (UserCredential) authenticationDialog.getResult(); + authenticationCountDownLatch.countDown(); + }); + }); + + try { + authenticationCountDownLatch.await(); + } catch (InterruptedException e) { + new Alert(Alert.AlertType.ERROR, "Interruption handling AuthenticationChallengeResponse: " + e.getMessage()).show(); + } + + // if credentials were set, return a new auth challenge response with them. otherwise, act like it was a cancel action + if (userCredential != null) { + return new AuthenticationChallengeResponse(AuthenticationChallengeResponse.Action.CONTINUE_WITH_CREDENTIAL, userCredential); + } + } + + // no credentials were set, return a new auth challenge response with with a cancel action + return new AuthenticationChallengeResponse(AuthenticationChallengeResponse.Action.CANCEL, authenticationChallenge); + } +} diff --git a/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationController.java b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationController.java new file mode 100644 index 0000000000..f7a96c9649 --- /dev/null +++ b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationController.java @@ -0,0 +1,165 @@ +/* + * Copyright 2019 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.samples.integrated_windows_authentication; + +import java.util.List; +import java.util.concurrent.ExecutionException; + +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.ListView; +import javafx.scene.control.ProgressIndicator; +import javafx.scene.control.TextField; +import javafx.scene.text.Text; + +import com.esri.arcgisruntime.concurrent.ListenableFuture; +import com.esri.arcgisruntime.loadable.LoadStatus; +import com.esri.arcgisruntime.mapping.ArcGISMap; +import com.esri.arcgisruntime.mapping.Basemap; +import com.esri.arcgisruntime.mapping.view.DrawStatus; +import com.esri.arcgisruntime.mapping.view.MapView; +import com.esri.arcgisruntime.portal.Portal; +import com.esri.arcgisruntime.portal.PortalItem; +import com.esri.arcgisruntime.portal.PortalQueryParameters; +import com.esri.arcgisruntime.portal.PortalQueryResultSet; +import com.esri.arcgisruntime.security.AuthenticationManager; + +public class IntegratedWindowsAuthenticationController { + + @FXML private MapView mapView; + @FXML private ListView resultsListView; + @FXML private TextField portalUrlTextField; + @FXML private ProgressIndicator progressIndicator; + @FXML private Text loadWebMapTextView; + + public void initialize() { + try { + + // add a map to the map view + ArcGISMap map = new ArcGISMap(Basemap.createStreets()); + mapView.setMap(map); + + // set authentication challenge handler + AuthenticationManager.setAuthenticationChallengeHandler(new IWAChallengeHandler()); + + // add a listener to the map results list view that loads the map on selection + resultsListView.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> { + if (resultsListView.getSelectionModel().getSelectedItem() != null) { + // show progress indicator while map is drawing + progressIndicator.setVisible(true); + + // get the portal item ID from the selected list view item + PortalItem portalItem = resultsListView.getSelectionModel().getSelectedItem(); + + if (portalItem != null) { + // create a Map using the web map (portal item) + ArcGISMap webMap = new ArcGISMap(portalItem); + // set the map to the map view + mapView.setMap(webMap); + } + + loadWebMapTextView.setText("Loaded web map from item " + portalItem); + } + }); + + // make the list view show a preview of the portal items' map area + resultsListView.setCellFactory(c -> new PortalItemInfoListCell()); + + // hide the progress indicator when the map is finished drawing + mapView.addDrawStatusChangedListener(drawStatusChangedEvent -> { + if (drawStatusChangedEvent.getDrawStatus() == DrawStatus.COMPLETED) { + progressIndicator.setVisible(false); + } + }); + + } catch (Exception e) { + // on any error, display the stack trace. + e.printStackTrace(); + } + } + + /** + * Handles searching the provided IWA secured portal. + */ + @FXML + private void handleSearchPortalPress() { + // check for a url in the URL field + if (portalUrlTextField.getText().isEmpty()) { + new Alert(Alert.AlertType.ERROR, "Portal URL is empty. Please enter a portal URL.").show(); + + } else { + // keep hold of the portal we are searching and set a variable indicating that this is a secure portal, to allow retrieving portal items later + Portal iwaSecuredPortal = new Portal(portalUrlTextField.getText(), true); + + // clear any existing items in the list view + resultsListView.getItems().clear(); + + // clear the information about the previously loaded map + loadWebMapTextView.setText(""); + + // show progress indicator + progressIndicator.setVisible(true); + + // load the portal items + iwaSecuredPortal.loadAsync(); + iwaSecuredPortal.addDoneLoadingListener(() -> { + if (iwaSecuredPortal.getLoadStatus() == LoadStatus.LOADED) { + + // create portal query parameters searching for web maps + PortalQueryParameters portalQueryParameters = new PortalQueryParameters( + "type:(\"web map\" NOT \"web mapping application\")"); + + // search the portal for web maps + ListenableFuture> portalItemResultFuture = iwaSecuredPortal.findItemsAsync( + portalQueryParameters); + portalItemResultFuture.addDoneListener(() -> { + try { + // get the result + PortalQueryResultSet portalItemSet = portalItemResultFuture.get(); + List portalItems = portalItemSet.getResults(); + // add the IDs and titles of the portal items to the list view + resultsListView.getItems().addAll(portalItems); + + } catch (ExecutionException | InterruptedException e) { + new Alert(Alert.AlertType.ERROR, "Error getting portal item set from portal.").show(); + } + // hide the progress indicator + progressIndicator.setVisible(false); + }); + + } else { + // hide the progress indicator + progressIndicator.setVisible(false); + + // report error + new Alert(Alert.AlertType.ERROR, "Portal sign in failed").show(); + } + }); + } + } + + /** + * Stops and releases all resources used in the application. + */ + void terminate() { + + if (mapView != null) { + mapView.dispose(); + } + } + +} diff --git a/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationLauncher.java b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationLauncher.java new file mode 100644 index 0000000000..d7bd780100 --- /dev/null +++ b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationLauncher.java @@ -0,0 +1,11 @@ +package com.esri.samples.integrated_windows_authentication; + +/** + * Wrapper required for launching a JavaFX 11 app through Gradle or from a jar. + */ +public class IntegratedWindowsAuthenticationLauncher { + + public static void main(String[] args) { + IntegratedWindowsAuthenticationSample.main(args); + } +} diff --git a/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationSample.java b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationSample.java new file mode 100644 index 0000000000..a98176309d --- /dev/null +++ b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/IntegratedWindowsAuthenticationSample.java @@ -0,0 +1,61 @@ +/* + * Copyright 2019 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.samples.integrated_windows_authentication; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class IntegratedWindowsAuthenticationSample extends Application { + + private static IntegratedWindowsAuthenticationController controller; + + @Override + public void start(Stage stage) throws Exception { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/iwa_controller.fxml")); + Parent root = loader.load(); + controller = loader.getController(); + Scene scene = new Scene(root); + + // set title, size, and add scene to stage + stage.setTitle("Integrated Windows Authentication Sample"); + stage.setWidth(800); + stage.setHeight(700); + stage.setScene(scene); + stage.show(); + } + + /** + * Stops and releases all resources used in application. + */ + @Override + public void stop() { + controller.terminate(); + } + + /** + * Opens and runs application. + * + * @param args arguments passed to this application + */ + public static void main(String[] args) { + + Application.launch(args); + } +} \ No newline at end of file diff --git a/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/PortalItemInfoListCell.java b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/PortalItemInfoListCell.java new file mode 100644 index 0000000000..b74809890c --- /dev/null +++ b/portal/integrated_windows_authentication/src/main/java/com/esri/samples/integrated_windows_authentication/PortalItemInfoListCell.java @@ -0,0 +1,23 @@ +package com.esri.samples.integrated_windows_authentication; + +import javafx.scene.control.ListCell; +import javafx.util.Pair; + +import com.esri.arcgisruntime.portal.PortalItem; + +/** + * Shows the title of the portal items in the selection list view. + */ +class PortalItemInfoListCell extends ListCell { + + @Override + protected void updateItem(PortalItem portalItem, boolean empty) { + super.updateItem(portalItem, empty); + if (portalItem != null) { + // set the list cell's text to the map's index + setText(portalItem.getTitle()); + } else { + setText(null); + } + } +} diff --git a/portal/integrated_windows_authentication/src/main/resources/iwa_auth_dialog.fxml b/portal/integrated_windows_authentication/src/main/resources/iwa_auth_dialog.fxml new file mode 100644 index 0000000000..35f1ee034d --- /dev/null +++ b/portal/integrated_windows_authentication/src/main/resources/iwa_auth_dialog.fxml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/portal/integrated_windows_authentication/src/main/resources/iwa_controller.fxml b/portal/integrated_windows_authentication/src/main/resources/iwa_controller.fxml new file mode 100644 index 0000000000..43325327f3 --- /dev/null +++ b/portal/integrated_windows_authentication/src/main/resources/iwa_controller.fxml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +