Need help passing authentication token to ServiceFeatureTable using ArcGIS runtime SDK 100.15.0

1226
8
05-09-2023 12:38 PM
VipulPurohit
New Contributor II

Hello,

I'm trying to use the ServiceFeatureTable to display a track on the map, but before the request is served, it needs to be authenticated by an intermediate server. I'm passing the authentication token to the ServiceFeatureTable's credential class, but I'm getting the following error:

com.esri.arcgisruntime.io.HttpResponseException: status code: 404, reason phrase: Page not found.

It seems that the error is related to the "referer" value, which is mandatory, but I don't know what to pass for it. I cannot pass null or an empty value. Here is my implementation:

 

// Get the service URL from the getTracksUseCase
val serviceUrl = "service_url"
val authToken = "auth_token"

val serviceFeatureTable = ServiceFeatureTable(serviceUrl)

// Pass the auth token to authenticate the request
val credential = UserCredential.createFromToken(
    authToken,
    "referer"
)
serviceFeatureTable.credential = credential
trackFeatureLayer = FeatureLayer(serviceFeatureTable).apply {
    id = "user_track_map"
    mapView?.map?.operationalLayers?.add(this)
    loadAsync()
    addDoneLoadingListener {
        if (loadStatus == LoadStatus.LOADED) {
            mapView?.setViewpointAsync(Viewpoint(fullExtent))
            completionCallback?.onSuccess(visible)
        } else {
            completionCallback?.onError(loadError)
        }
    }
}

 


However, the iOS SDK version we are using allows passing a null value for the referer, and it works without any issues.

Can you guys help me to pass the authentication token to our server as the iOS SDK allows? The version of ArcGIS runtime SDK we are using is 100.15.0 for Kotlin..

The documentation for the iOS class can be found at the following link:
https://developers.arcgis.com/ios/api-reference/interface_a_g_s_credential.html

0 Kudos
8 Replies
Shubham_Sharma
Esri Contributor

You need to pass the user agent used to authenticate the credential. In your case it would be:

 

val credential = UserCredential.createFromToken(
    "AuthTokenHere", ArcGISRuntimeEnvironment.getUserAgent()
)

 

Api ref: createFromToken()

0 Kudos
VipulPurohit
New Contributor II

Hey @Shubham_Sharma, thanks for your quick response.

Unfortunately passing the  ArcGISRuntimeEnvironment.getUserAgent() as referer didn't resolve the issue. Do you see any other way to fix this issue? 

Basically, I need to add a token query parameter to the ServiceFeatureTable URL the value of the token will our custom JWT. The structure should be like this:

https://abcd.execute-api.us-west-2.amazonaws.com/tracks/rest/services/3c382f6a-9a0f-43cf-851a-9fcc7f...

If you guys have any other method to add the token query parameter that would be a great help. 

0 Kudos
Shubham_Sharma
Esri Contributor

I think it is important to understand how the token is being created. The user agent you would provide as the referer must be the same as the user agent used to create the authToken. The example above I provided was assuming that the device you are creating the credential also would request the token. 

 

To find the right user agent used to create the token, you need to verify with the intermediate server making the API call to /generateToken. Visit https://developers.arcgis.com/rest/users-groups-and-items/generate-token.htm , and you will notice that a referer is used to create the token. You could also use this POST request field to set the referer for the token to be the same as the device you plan to run the application on.  

 

POST /webadaptor/sharing/rest/generateToken HTTP/1.1
Host: machine.domain.com
Content-Type: application/x-www-form-urlencoded
Content-Length: []

username=admin&password=test1234&client=referer&ip=&referer=https://myserver/mywebapp&expiration=60&f=pjson
0 Kudos
VipulPurohit
New Contributor II

Thank you for your response, @Shubham_Sharma 

I attempted to use the referer provided in the /generateToken API call, but I'm still encountering the same error.

Do you know if it's possible to make the referer field optional, similar to how it's done in the iOS SDK? Alternatively, is there a way to intercept the request or add a custom query parameter to it?

0 Kudos
Shubham_Sharma
Esri Contributor

Could you provide the ArcGIS enterprise sever version being used? 

If you would like direct support on this issue, please reach out to: https://support.esri.com/en-us/overview

 

0 Kudos
VipulPurohit
New Contributor II

Sorry @zshu, I don't have any idea about the ArcGIS enterprise server version, I asked the backend engineer about the version, but they also don't have any idea about it.

0 Kudos
AnonymousUser23
New Contributor III

@VipulPurohit@Shubham_Sharma@MikeBranscomb  This is actually a Koop service being fed from Velocity that we are bringing into runtime. 

0 Kudos
RamaChintapalli
Esri Contributor

Hi @VipulPurohit @AnonymousUser23 ,
There could be multiple things at play here to understand the problem and root cause. It would be best to reach out to us through support to have a repro case and provide some additional details (Ex: network logs etc.)

Unfortunately with the current 100.15.2 API, the referer is not optional in Android. We have made the changes to the 200.1 version (Kotlin SDK) where providing referer is optional while creating PregeneratedTokenCredential.

Thanks
Rama

0 Kudos