Title: | Manage 'OAuth' Credentials from 'Posit Connect' |
---|---|
Description: | A toolkit for making use of credentials mediated by 'Posit Connect'. It handles the details of communicating with the Connect API correctly, 'OAuth' token caching, and refresh behaviour. |
Authors: | Aaron Jacobs [aut, cre], Posit Software, PBC [cph, fnd] |
Maintainer: | Aaron Jacobs <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2025-02-11 15:24:08 UTC |
Source: | https://github.com/posit-dev/connectcreds |
Request an OAuth access token for a third-party resource from Posit Connect. The OAuth token will belong to the client (usually a "service principal" or "service account") managed by Connect, not the publisher.
connect_service_account_token( resource = NULL, scope = NULL, content_token = Sys.getenv("CONNECT_CONTENT_SESSION_TOKEN"), server_url = Sys.getenv("CONNECT_SERVER"), api_key = Sys.getenv("CONNECT_API_KEY") ) has_service_account_token(...)
connect_service_account_token( resource = NULL, scope = NULL, content_token = Sys.getenv("CONNECT_CONTENT_SESSION_TOKEN"), server_url = Sys.getenv("CONNECT_SERVER"), api_key = Sys.getenv("CONNECT_API_KEY") ) has_service_account_token(...)
resource |
The URI that identifies the resource that the client is trying to access, if applicable. |
scope |
Scopes to be requested from the resource owner. |
content_token |
A token that uniquely identifies this content session.
Defaults to the value of the |
server_url |
The Connect server to exchange credentials with. Defaults
to the value of the |
api_key |
An API key for the Connect server. Defaults to the value of
the |
... |
Further arguments passed on to |
connect_service_account_token()
handles caching automatically.
connect_service_account_token()
returns an httr2::oauth_token.
has_service_account_token()
returns TRUE
if there is a
Connect-managed service account avaiable and FALSE
otherwise.
token <- "default-token" if (has_service_account_token()) { token <- connect_service_account_token() }
token <- "default-token" if (has_service_account_token()) { token <- connect_service_account_token() }
Request an OAuth access token for a third-party resource belonging to the user associated with a given Shiny session. This works by exchanging a short-lived session credential for OAuth tokens issued to the client managed by the Connect server, without the Shiny app in question having to manage the user's authentication flow (or the associated client credentials) itself.
connect_viewer_token( resource = NULL, scope = NULL, session = get_connect_session(), server_url = Sys.getenv("CONNECT_SERVER"), api_key = Sys.getenv("CONNECT_API_KEY") ) has_viewer_token(..., session = get_connect_session())
connect_viewer_token( resource = NULL, scope = NULL, session = get_connect_session(), server_url = Sys.getenv("CONNECT_SERVER"), api_key = Sys.getenv("CONNECT_API_KEY") ) has_viewer_token(..., session = get_connect_session())
resource |
The URI that identifies the resource that the client is trying to access, if applicable. |
scope |
Scopes to be requested from the resource owner. |
session |
A Shiny session object. By default, this grabs the Shiny session of the parent environment (if any), provided we are also running on Connect. |
server_url |
The Connect server to exchange credentials with. Defaults
to the value of the |
api_key |
An API key for the Connect server. Defaults to the value of
the |
... |
Further arguments passed on to |
connect_viewer_token()
handles caching automatically.
connect_viewer_token()
returns an httr2::oauth_token.
has_viewer_token()
returns TRUE
if the session has a viewer
token and FALSE
otherwise.
token <- "default-token" if (has_viewer_token()) { token <- connect_viewer_token() }
token <- "default-token" if (has_viewer_token()) { token <- connect_viewer_token() }
These functions can be used to temporarily mock responses from the Connect server, which is useful for writing tests that verify the behaviour of viewer-based or service account credentials.
with_mocked_connect_responses( code, mock = NULL, token = NULL, error = FALSE, env = caller_env() ) local_mocked_connect_responses( mock = NULL, token = NULL, error = FALSE, env = caller_env() )
with_mocked_connect_responses( code, mock = NULL, token = NULL, error = FALSE, env = caller_env() ) local_mocked_connect_responses( mock = NULL, token = NULL, error = FALSE, env = caller_env() )
code |
Code to execute in the temporary environment. |
mock |
A function, a list, or
|
token |
When not |
error |
When |
env |
Environment to use for scoping changes. |
with_mocked_connect_responses()
returns the result of evaluating
code
.
with_mocked_connect_responses( connect_viewer_token(), token = "test" ) with_mocked_connect_responses( connect_service_account_token(), token = "test" )
with_mocked_connect_responses( connect_viewer_token(), token = "test" ) with_mocked_connect_responses( connect_service_account_token(), token = "test" )