AuthBackendBearer: Bearer token authorization backend

Description Super class Methods References See Also Examples

Description

Creates AuthBackendBearer class object.

Super class

RestRserve::AuthBackend -> AuthBackendBearer

Methods

Public methods

Inherited methods

Method new()

Creates AuthBackendBearer class object.

Usage
AuthBackendBearer$new(FUN)
Arguments
FUN

Function to perform authentication which takes one arguments - token. Returns boolean - whether access is allowed for a requested token or not.


Method authenticate()

Provide authentication for the given request.

Usage
AuthBackendBearer$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
AuthBackendBearer$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

References

RFC6750 Specification

See Also

AuthMiddleware Request Response

Other AuthBackend: AuthBackendBasic, AuthBackend, AuthMiddleware

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

RestRserve documentation built on Jan. 13, 2021, 7:05 p.m.