AuthBackendBasic: Basic authorization backend

AuthBackendBasicR Documentation

Basic authorization backend

Description

Creates AuthBackendBasic class object.

Super class

RestRserve::AuthBackend -> AuthBackendBasic

Methods

Public methods


Method new()

Creates AuthBackendBasic class object.

Usage
AuthBackendBasic$new(FUN)
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.

References

RFC7617 Wikipedia

See Also

AuthMiddleware Request Response

Other AuthBackend: AuthBackend, AuthBackendBearer, AuthMiddleware

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


RestRserve documentation built on May 29, 2024, 8:18 a.m.