| guard_oauth2 | R Documentation |
OAuth 2.0 is an authorization scheme that is powering much of the modern
internet and is behind things like "log in with GitHub" etc. It separates the
responsibility of authentication away from the server, and allows a user to
grant limited access to a service on the users behalf. While OAuth also
allows a server to make request on the users behalf the main purpose in the
context of fireproof is to validate that the user can perform a successful
login and potentially extract basic information about the user. The
guard_oauth2() function is the base constructor which can be used to create
guards with any provider. For ease of use fireproof comes with a
range of predefined constructors for popular services such as GitHub etc.
Central for all of these is the need for your server to register itself
with the provider and get a client id and a client secret which must be used
when logging users in.
guard_oauth2(
token_url,
redirect_url,
client_id,
client_secret,
auth_url = NULL,
grant_type = c("authorization_code", "password"),
oauth_scopes = NULL,
validate = function(info) TRUE,
redirect_path = get_path(redirect_url),
on_auth = replay_request,
user_info = NULL,
service_params = list(),
scopes_delim = " ",
name = "OAuth2Auth"
)
token_url |
The URL to the authorization servers token endpoint |
redirect_url |
The URL the authorization server should redirect to following a successful authorization. Must be equivalent to one provided when registering your application |
client_id |
The ID issued by the authorization server when registering your application |
client_secret |
The secret issued by the authorization server when registering your application. Do NOT store this in plain text |
auth_url |
The URL to redirect the user to when requesting
authorization (only needed for |
grant_type |
The type of authorization scheme to use, either
|
oauth_scopes |
Optional character vector of scopes to request the
user to grant you during authorization. These will not influence the
scopes granted by the |
validate |
Function to validate the user once logged in. It will be
called with a single argument |
redirect_path |
The path that should capture redirects after
successful authorization. By default this is derived from |
on_auth |
A function which will handle the result of a successful
authorization. It will be called with four arguments: |
user_info |
A function to extract user information from the
access token. It is called with a single argument: |
service_params |
A named list of additional query params to add to
the url when constructing the authorization url in the
|
scopes_delim |
The separator of the scopes as returned by the service.
The default |
name |
The name of the guard |
A GuardOAuth2 object
guard_oauth2() automatically adds some user information after
authentication, but it is advised to consult the service provider for more
information (this is done automatically for the provider specific
guards. See their documentation for details about what information is
assigned to which field). The base constructor will set the scopes field to
any scopes returned by the validate function. It will also set
the token field to a list with the token data provided by the service
during authorization. Some standard fields in the list are:
access_token: The actual token value
token_type: The type of token (usually "bearer")
expires_in: The lifetime of the token in seconds
refresh_token: A long-lived token that can be used to issue a new
access token if the current becomes stale
timestamp: The time the token was received
scopes: The scopes granted by the user for this token
But OAuth 2.0 providers may choose to supply others. Consult the documentation for the provider to learn of additional fields it may provide.
# Example using GitHub endpoints (use `guard_github()` in real code)
github <- guard_oauth2(
token_url = "https://github.com/login/oauth/access_token",
redirect_url = "https://example.com/auth",
client_id = "MY_APP_ID",
client_secret = "SUCHASECRET",
auth_url = "https://github.com/login/oauth/authorize",
grant_type = "authorization_code"
)
# Add it to a fireproof plugin
fp <- Fireproof$new()
fp$add_guard(github, "github_auth")
# Use it in an endpoint
fp$add_auth("get", "/*", github_auth)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.