I have created a plumber
API in R that authenticates user requests via a bearer token in the header.
The code for the authentication is a filter that looks like this:
#* @filter checkAuthentication
function(req, res) {
if (!req$PATH_INFO %in% c("/__docs__/", "/__swagger__/", "/openapi.json")) {
tryCatch({
token = req$HEADERS['authorization'][1]
secret_api_token = Sys.getenv("SECRET_TOKEN")
drop_bearer_token_string <- function(tokenString) {
# Use regex to drop the string "Bearer"
# and empty spaces before or after the token
return(gsub("^Bearer\\s+|\\s+Bearer$", "", tokenString))
}
if (drop_bearer_token_string(req$HEADERS['authorization'][1]) == drop_bearer_token_string(secret_api_token)) {
plumber::forward() #User authenticated, continue
}
},
error = function(cond) {
return(list(
status = "Failed.",
code = 401,
message = "No or wrong authentication provided."
))
})
} else {
plumber::forward()
}
}
Then I start the api with plumber like this:
pr("api.R") %>% pr_run(host = '0.0.0.0', port = 8000)
As a default this will create a swagger UI with default settings that can be accessed even without authentication due to the filter above.
However when opening the swagger UI there is no field to input the bearer token for authentication so all example calls will fail the authentication filter.
How can I add header fields to the swagger UI within the code/framework?