13 Plumber

13.1 User meta-data

Plumber APIs can access the username and groups of the current user by parsing the RStudio-Connect-Credentials request header.

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 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.")))
    }
}