Creates AuthBackendBearer class object.
AuthMiddleware Request Response
Other AuthBackend:
AuthBackend
,
AuthBackendBasic
,
AuthMiddleware
RestRserve::AuthBackend
-> AuthBackendBearer
token_db = list(
"valid-token" = as.POSIXct("2099-12-31", tz = "GMT"),
"expired-token" = as.POSIXct("1900-01-01", tz = "GMT")
)
auth_fun = function(token) {
if (is.null(token_db[[token]])) return(FALSE) # not found
if (Sys.time() > token_db[[token]]) return(FALSE) # expired
return(TRUE)
}
# init backend
auth_backend = AuthBackendBearer$new(FUN = auth_fun)
# test backend
# define credentials (see RFC)
token = "valid-token"
# generate request headers
h = list("Authorization" = sprintf("Bearer %s", token))
# simulate request
rq = Request$new(path = "/", headers = h)
# init response object
rs = Response$new()
# perform authentication
auth_backend$authenticate(rq, rs) # TRUE
#> [1] TRUE