Manage several OAuth connections in one Shiny app
Source:R/oauth_connections.R
oauth_connections.RdLet users connect several services or accounts in one Shiny app. Create the
manager outside server() with named clients from oauth_client() or
smart_client(), then use oauth_connections_ui() and
oauth_connections_server() to display and use their saved connections.
Arguments
- clients
Non-empty named list of OAuthClient objects, at most 64.
resource_basesis required only for connection$request()calls; clients used solely to supply tokens to an SDK can omit it. Names select local configurations (for examplehospital_a), independently of OAuthclient_id: a letter followed by letters, digits,_or-, at most 64 characters.- app_origin
Public application origin, including a non-default port. HTTPS is required for retained owners, except an explicit browser-owner HTTP loopback exception. Session-only development also permits loopback HTTP.
- retention
"shiny"(default) discards connections at Shiny session end."browser"restores the browser owner's connections after navigation;"account"uses a trusted local application login.- retention_seconds
Maximum lifetime of each stored grant, in seconds. Positive and finite, no larger than the store's
max_age. Refresh never resets it. Browser retention is also capped by the owner's absolute expiry.- owner_policy
oauth_browser_owner()oroauth_account_owner()matching the retained mode. Must beNULLfor session-only retention.- store
A store from
oauth_connection_store_memory(). Required for retained modes; session-only mode creates a memory store by default. Only the supplied memory store in one R process is supported.- keys
Named list with
credentialsandowner, each a deployment-held raw vector of 32 bytes. Required for retained modes. Session-only mode creates ephemeral keys if omitted. Keys are held by the manager, separately from the ciphertext-only connection store.- callback_policy
"distinct_routes"(default) gives each client its own registered callback route. With several clients, configure every client withauthorization_server_mode = "multi_redirect_uri"and the complete set of routes inauthorization_server_redirect_uris. Every client's declared set must include all manager callback routes; additional application routes are permitted. Routes are compared by canonical origin and path."issuer"allows shared routes for distinct authorization-server issuers."shared_routes"additionally supports several clients or registrations at one issuer through a protected pending-state index. Both opt-in policies require explicitauthorization_server_mode = "multi_issuer"clients, with RFC 9207 issuer responses or signed JARM. Encrypted JARM requires distinct routes. Routing never substitutes for callback authentication.- x
A connection manager to print.
- ...
Unused print arguments.
Value
An OAuthConnections server-side configuration object. Printing shows
only the retention mode and client count. It holds client configuration
and deployment keys for the UI and server modules.
Details
By default, connections last for one Shiny session. Optional browser or local
account retention can restore them after navigation. See
vignette("multiple-authorizations", package = "shinyOAuth")
(online guide)
for a complete example.
The manager's named clients select the configuration for each login. Connections are separate local records, but the provider may reuse one grant; revoking a connection can therefore affect other tokens from that grant.
Advanced deployment and shared credentials
Refreshes using one shared credential are coordinated. Rotation or an uncertain refresh can require other connections sharing that credential to reconnect; one connection's response never replaces another's identity or permissions. Revocation can also affect related provider grants.
A manager is bound to one UI/server module ID and one public origin. The memory store and owner registries survive Shiny sessions, not R restarts. Copies in another R process are rejected.
In session-only mode, a pending authorization survives navigation through the existing single-use OAuth state and browser binding, while existing grants are discarded with the old Shiny session. Browser/account mode additionally binds authorization to the initiating local owner and its session generation.
The manager supports generic OAuth clients and the SMART discovery, scope and
launch policies configured by smart_client().
Examples
# Replace this example provider and client ID with your registered application.
provider <- oauth_provider(
name = "Example service",
auth_url = "https://example.com/authorize",
token_url = "https://example.com/token",
token_auth_style = "public"
)
client <- oauth_client(
provider = provider,
client_id = "example-client",
redirect_uri = "http://127.0.0.1:8100/callback/service",
resource_bases = c(api = "https://api.example.com")
)
# Create the manager once, outside server(). Default retention is one Shiny session.
manager <- oauth_connections(
clients = list(service = client),
app_origin = "http://127.0.0.1:8100"
)
ui <- oauth_connections_ui(
shiny::fluidPage(
shiny::actionButton("connect", "Connect to service"),
shiny::verbatimTextOutput("connections")
),
id = "auth",
manager = manager
)
server <- function(input, output, session) {
auth <- oauth_connections_server("auth", manager)
shiny::observeEvent(input[["connect"]], auth[["connect"]]("service"))
output[["connections"]] <- shiny::renderPrint(auth[["connections"]]())
}
# Construct the app without launching it or contacting the provider.
app <- shiny::shinyApp(
ui,
server,
uiPattern = ".*",
options = list(port = 8100)
)