Learn R Programming

RestRserve (version 0.2.0.2)

AuthBackendBearer: Bearer token authorization backend

Description

Creates AuthBackendBearer class object.

Arguments

Format

R6::R6Class object.

Construction

AuthBackendBearer$new(FUN)
  • FUN :: function character(1) -> logical(1) Function to perform authentication which takes one arguments - token. Returns boolean - whether access is allowed for a requested token or not.

Methods

  • authenticate(request, response) Request, Response -> NULL Provide authentication for the given request.

References

RFC6750 Specification

See Also

AuthMiddleware Request Response

Other AuthBackend: AuthBackendBasic, AuthBackend, AuthMiddleware

Examples

Run this code
# NOT RUN {
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

# }

Run the code above in your browser using DataLab