This plugin orchestrates all guards for your fiery app. It is a special Route that manages all the different guards you have defined as well as testing all the endpoints that have auth requirements.
Details
A guard is an object deriving from the Guard class which is usually created
with one of the guard_*() constructors. You can provide it with a name as
you register it and can thus have multiple instances of the same scheme (e.g.
two guard_basic() with different user lists)
An auth handler is a handler that consists of a method, path, and
flow. The flow is a logical expression of the various guards the request
must pass to get access to that endpoint. For example, if you have two
guards named auth1 and auth2, a flow could be auth1 || auth2 to allow
a request if it passes either of the guards. Given an additional guard,
auth3, it could also be something like auth1 || (auth2 && auth3). The
flow is given as a bare expression, not as a string. In addition to the three
required arguments you can also supply a character vector of scopes that are
required to have access to the endpoint. If your guard has scope support
then the request will be tested against these to see if the (otherwise valid)
user has permission to the resource.
Super class
routr::Route -> Fireproof
Active bindings
nameThe name of the plugin:
"fireproof"requireRequired plugins for Fireproof
guardsThe name of all the guards currently added to the plugin
Methods
Method add_auth()
Add a new authentication handler. It invisibly returns the parsed flow so it can be used for generating OpenAPI specs with.
Arguments
methodThe http method to match the handler to
pathThe URL path to match to
flowThe authentication flow the request must pass to be valid. See Details. If
NULLthen authentication is turned off for the endpoint.scopeAn optional character vector of scopes that the request must have permission for to access the resource
Method add_guard()
Add a guard to the plugin
Arguments
guardEither a Guard object defining the guard (preferred) or a function taking the standard route handler arguments (
request,response,keys, and...) and returnsTRUEif the request is valid andFALSEif not.nameThe name of the guard to be used when defining flow for endpoint auth.
Method add_handler()
Defunct overwrite of the add_handler() method to prevent
this route to be used for anything other than auth. Will throw
an error if called.
Method flow_to_openapi()
Turns a parsed flow (as returned by add_auth())
into an OpenAPI Security Requirement compliant list. Not all flows can be
represented by the OpenAPI spec and the method will return NULL with a
warning if so. Scope is added to all schemes, even if not applicable, so
the final OpenAPI doc should be run through prune_openapi() before
serving it.
Method on_attach()
Method for use by fiery when attached as a plugin. Should not be called directly. This method looks for a header route stack in the app and if it doesn't exist it creates one. It then attaches the plugin as the first route to it.
Examples
# Create a fireproof plugin
fp <- Fireproof$new()
# Create some authentication schemes and add them
basic <- guard_basic(
validate = function(user, password) {
user == "thomas" && password == "pedersen"
},
user_info = function(user) {
new_user_info(
name_given = "Thomas",
name_middle = "Lin",
name_family = "Pedersen"
)
}
)
fp$add_guard(basic, "basic_auth")
key <- guard_key(
key_name = "my-key-location",
validate = "SHHH!!DONT_TELL_ANYONE"
)
fp$add_guard(key, "key_auth")
google <- guard_google(
redirect_url = "https://example.com/auth",
client_id = "MY_APP_ID",
client_secret = "SUCHASECRET",
)
fp$add_guard(google, "google_auth")
# Add authentication to different paths
fp$add_auth("get", "/require_basic", basic_auth)
fp$add_auth("get", "/require_basic_and_key", basic_auth && key_auth)
fp$add_auth(
"get",
"/require_google_or_the_others",
google_auth || (basic_auth && key_auth)
)
# Add plugin to fiery app
app <- fiery::Fire$new()
# First add the firesale plugin as it is required
fs <- firesale::FireSale$new(storr::driver_environment(new.env()))
app$attach(fs)
# Then add the fireproof plugin
app$attach(fp)