Creates AuthBackendBasic class object.

References

RFC7617 Wikipedia

Super class

RestRserve::AuthBackend -> AuthBackendBasic

Methods


Method new()

Creates AuthBackendBasic class object.

Usage

Arguments

FUN

Function to perform authentication which takes two arguments - user and password. Returns boolean - whether access is allowed for a requested user or not.


Method authenticate()

Provide authentication for the given request.

Usage

AuthBackendBasic$authenticate(request, response)

Arguments

request

Request object.

response

Response object.

Returns

Boolean - whether access is allowed for a requested user or not.


Method clone()

The objects of this class are cloneable with this method.

Usage

AuthBackendBasic$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

# init users database
user_db = list(
  "user-1" = "password-1",
  "user-2" = "password-2"
)
# define authentication handler
auth_fun = function(user, password) {
  if (is.null(user_db[[user]])) return(FALSE) # not found
  if (!identical(user_db[[user]], password)) return(FALSE) # incorrect
  return(TRUE)
}
# init backend
auth_backend = AuthBackendBasic$new(FUN = auth_fun)

# test backend
# define credentials (see RFC)
creds = jsonlite::base64_enc("user-1:password-1")
# generate request headers
h = list("Authorization" = sprintf("Basic %s", creds))
# simulate request
rq = Request$new(path = "/", headers = h)
# init response object
rs = Response$new()
# perform authentication
auth_backend$authenticate(rq, rs) # TRUE
#> [1] TRUE