R-ArcGIS / arcgislayers

ArcGIS Location Services
http://r.esri.com/arcgislayers/
Apache License 2.0
39 stars 9 forks source link

`token` arg is not validated sufficiently #128

Closed elipousson closed 7 months ago

elipousson commented 8 months ago

Describe the bug

This is arguably an enhancement (and may need to be handled within {arcgisutils}) but I definitely experienced it as a bug so figured I'd start here.

To Reproduce

Initially, I assumed I could pass the output of arcgisutils::auth_code() to arc_open() and was completely confused by the resulting error:

arcgislayers::arc_open(
  "https://services1.arcgis.com/43Lm3JYE3nM91DAF/arcgis/rest/services/Floodplain_Dissolved/FeatureServer",
  token = httr2::oauth_token("") # Error is the same if you use the output from arcgisutils::auth_code()
)
#> Error in `httr2::req_body_form()`:
#> ! All elements of `...` must be either an atomic vector or NULL.

Created on 2024-01-23 with reprex v2.1.0

Expected behavior

At a minimum, I expect arc_open() to return an informative error message explaining that token is the wrong type.

Ideally, I expect token to support both string inputs and httr2_token class inputs since I can't see any meaningful performance or technical reasons that wouldn't be straightforward to implement. One option could be to still limit the input token for arc_open() to strings only but allow arc_token() to take a token parameter that is converted to a string (reusing the code from arcgisutils::set_auth_token()) as a (hopefully) more intuitive alternate workflow.

JosiahParry commented 8 months ago

There's more than one issue here. For set_auth_token() not accepting strings, see https://github.com/R-ArcGIS/arcgisutils/issues/11

The other point is about validation of the token and permitting polymorphism in the argument.

I agree that this is inconsistent and could be improved. Only one type of object should be supported for clarity and type safety. I think httr2_token is the best choice. If that's the choice a number of changes have to take place:

elipousson commented 8 months ago

Hmmm. If the token moves from the environment variable to the package environment, I do think some caching features for the token would still be essential.

I will typically re-start a session several times in a single hour of analytic or programming work to ensure the full script is still reproducible from the start. A single token would still be valid throughout that time period—but if I need to re-authenticate in the browser every time I restart that would be a huge PITA.

I'm swamped today but I'll revisit the information you shared here and in our discussion about error handling with {httr2} and respond in more detail later this week.

JosiahParry commented 8 months ago

I think we should specifically focus on caching the (refresh token at least first) but probably move that convo to https://github.com/R-ArcGIS/arcgisutils/issues/10