RSTUDIO_USER_IDENTITY in Plumber API on Posit Connect using API Key for authentication

63 Views Asked by At

I make requests to an R Plumber API which is hosted on Posit Connect using a Posit Connect API Key for authentication. The API returns that the value of Sys.getenv()["RSTUDIO_USER_IDENTITY"] is empty. I would expect to receive a value for RSTUDIO_USER_IDENTITY, because the API Key is linked to a specific user account.

Why is Sys.getenv()["RSTUDIO_USER_IDENTITY"] empty when connecting to an R Plumber API hosted on Posit Connect using a Posit Connect API Key?

Does a workaround exist to find out which user account is linked to the API Key that is being used for the API request?

Example R Plumber API:

library(plumber)

#* @get /exampleapi
information <- function(req, res, session) {
  return(Sys.getenv()["RSTUDIO_USER_IDENTITY"][[1]])
}

Thank you for any advice!

1

There are 1 best solutions below

1
On BEST ANSWER

We can ask plumber to show us what it sees :-)

#* Retrieve client data visible to the app
#*
#* @get /clientdata
function(req, res) {
  list(
    req = capture.output(str(mget(setdiff(names(req), "pr"), envir = req))),
    res = capture.output(str(res)),
    env = capture.output(Sys.getenv())
  )
}

Visit that API (in your instance of Connect) and try it out:

enter image description here

Notice within the req list, you'll find my (there) username bevans in what appears to be a json string.

Also available in a gist.

I originally found this or a hint for it at https://dzone.com/articles/my-5-tips-for-better-restapi-design, though that link is now a login-wall, not sure if it's still valid.