Plumber¶
User meta-data¶
Plumber APIs can access the username and groups of the current logged in user by
parsing the RStudio-Connect-Credentials
request header.
Most environments have unique usernames where each
username
identifies a single user. In large organizations with hundreds of users, it is possible to have two users with, for example, the same last name and initials, which could result in a duplicated username. If you're expecting such a large number of logged in users to access your Plumber APIs, please be sure to consult your administrator to confirm whether this condition is possible in your environment.
Group meta-data is populated when using most authentication provider, except LDAP.
Your Plumber API should use the
HTTP_RSTUDIO_CONNECT_CREDENTIALS
name to obtain theRStudio-Connect-Credentials
header value. All HTTP headers are made available on thereq
request object using a normalized name prefixed withHTTP_
.
This simple Plumber API defines a /hello
route that greets the arriving user.
library(jsonlite)
library(plumber)
# Returns a list containing "user" and "groups" information
# populated by incoming request data.
getUserMetadata <- function(req) {
rawUserData <- req[["HTTP_RSTUDIO_CONNECT_CREDENTIALS"]]
if (!is.null(rawUserData)) {
jsonlite::fromJSON(rawUserData)
} else {
list()
}
}
#* @get /hello
function(req, res){
user <- getUserMetadata(req)
username <- user[["user"]]
if (!is.null(username)) {
return(list(message = paste0("So nice to see you, ", username, ".")))
} else {
return(list(message = paste0("Howdy, stranger.")))
}
}