Plumber#
User meta-data#
Plumber APIs can access the username and the names of groups of the current
logged in user by parsing the RStudio-Connect-Credentials
request header.
Note
Your Plumber API should use the HTTP_RSTUDIO_CONNECT_CREDENTIALS
name to
obtain the RStudio-Connect-Credentials
header value. All HTTP headers are
made available on the req
request object using a normalized name prefixed with HTTP_
.
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.")))
}
}
User and Group uniqueness#
Most environments have unique usernames where each user
identifies a single
user and groups
the name of the groups the user is a member of.
However, in large organizations with hundreds of users and groups, this may not be true. See the Admin Guide sections Credentials for Content for more information.