authenticate: authenticate

Description Usage Arguments Details Value See Also Examples

View source: R/authenticate.R

Description

authenticate takes one of the is_authed_* functions of sealr (or a custom function) as input. If the request is authenticated / authorized, authenticate will forward the request to the next handler. If the request is not authenticated / authorized, a 401 response will be returned to the caller. Hence, authenticate should only be used in plumber filters as it calls plumber::forward.

Usage

1
authenticate(req, res, is_authed_fun, ...)

Arguments

req

plumber request object

res

plumber response object

is_authed_fun

function. Function to check whether API call is authenticated / authorized. Use any of sealr's is_authed_* functions or your own custom function. See Details for requirements for custom functions.

...

arguments to be passed down to the is_authed_fun function.

Details

Custom is_authed_fun functions should return a list with the following elements:

You can use the helper functions is_authed_return_list and is_authed_return_list_401 to generate those lists in your custom function.

Value

either TRUE (invisibly from plumber::forward()) or a list containing HTTP status, HTTP status code, and a message (see details).

See Also

https://www.rplumber.io/docs/routing-and-input.html

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## Not run: 
 pr$filter("sealr-jwt-filter", function(req, res){
   sealr::authenticate(req = req, res = res, sealr::is_authed_jwt, secret = "averylongsupersecretsecret")
 })

## End(Not run)
## Not run: 
 # define your own function somewhere
 is_authed_custom <- function(req, res, a, b){
  # some logic with request parameters (in req) and function parameters (a, b)
  if(TRUE){ # implement this
    # is authed
    return(sealr::is_authed_return_list(TRUE))
  } else {
    # not authed :(
    return(sealr::is_authed_return_list_401())
  }
 }

 pr$filter("sealr-custom-filter", function(req, res){
  sealr::authenticate(req = req, res = res, sealr::is_authed_custom, a = 5, b = 4)
 })

## End(Not run)

jandix/sealr documentation built on Oct. 3, 2021, 1:16 p.m.