Skip to content

Commit 54b4a50

Browse files
committed
Add an as_azure_token() for compatibility with AzureR packages.
This commit introduces a standalone `as_azure_token()` that converts the httr2 format to one that works out of the box with AzureR packages. For example: token <- NULL if (has_viewer_token()) { token <- as_azure_token(connect_viewer_token()) } Microsoft365R::list_sharepoint_sites(token = token) Fixes #4. Signed-off-by: Aaron Jacobs <aaron.jacobs@posit.co>
1 parent e08aecd commit 54b4a50

5 files changed

Lines changed: 98 additions & 0 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ License: MIT + file LICENSE
1313
Imports:
1414
cli,
1515
httr2 (>= 1.1.0),
16+
R6,
1617
rlang (>= 1.1.0)
1718
Suggests:
19+
AzureAuth,
1820
testthat (>= 3.0.0),
1921
withr
2022
Encoding: UTF-8

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(as_azure_token)
34
export(connect_service_account_token)
45
export(connect_viewer_token)
56
export(has_service_account_token)
@@ -8,3 +9,4 @@ export(local_mocked_connect_responses)
89
export(with_mocked_connect_responses)
910
import(httr2)
1011
import(rlang)
12+
importFrom(R6,R6Class)

R/azure-token.R

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#' Convert a token to the AzureAuth format
2+
#'
3+
#' Wraps an [httr2::oauth_token] in an [AzureAuth::AzureToken] for use with the
4+
#' AzureR package ecosystem.
5+
#'
6+
#' @param token An [httr2::oauth_token].
7+
#' @param resource The Azure resource this token is associated with, or `NULL`
8+
#' if this is unknown.
9+
#' @returns An R6 class that inherits from `AzureToken`.
10+
#' @examplesIf FALSE
11+
#' token <- NULL
12+
#' if (has_viewer_token()) {
13+
#' token <- as_azure_token(connect_viewer_token())
14+
#' }
15+
#'
16+
#' # For example, when using the Microsoft365R package:
17+
#' # Microsoft365R::list_sharepoint_sites(token = token)
18+
#' @export
19+
as_azure_token <- function(token, resource = NULL) {
20+
rlang::check_installed("AzureAuth", reason = "for AzureR compatibility")
21+
ConnectAzureToken$new(token, resource = resource)
22+
}
23+
24+
# An AzureR-interoperable token class.
25+
ConnectAzureToken <- R6Class(
26+
"ConnectAzureToken",
27+
inherit = AzureAuth::AzureToken,
28+
public = list(
29+
initialize = function(token, resource = NULL) {
30+
self$credentials <- unclass(token)
31+
self$auth_type <- "external"
32+
super$initialize(
33+
resource = resource,
34+
app = "Posit Connect",
35+
# Inaccurate but necessary to avoid errors.
36+
tenant = "common",
37+
version = 2,
38+
aad_host = NA,
39+
token_args = list(),
40+
# This triggers the "dummy" workflow that skips calling out to Azure.
41+
use_cache = NA
42+
)
43+
},
44+
45+
# Tokens must be refreshed via connectcreds library calls instead.
46+
can_refresh = function() FALSE,
47+
48+
# Custom validation.
49+
validate = function() {
50+
if (is.null(self$credentials$expires_at)) {
51+
TRUE
52+
} else {
53+
(as.integer(Sys.time()) + 5) > self$credentials$expires_at
54+
}
55+
}
56+
),
57+
private = list(
58+
# Necessary to avoid "Do not call this constructor directly" errors.
59+
initfunc = function(...) {}
60+
)
61+
)

R/connectcreds-package.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
## usethis namespace: start
55
#' @import httr2
66
#' @import rlang
7+
#' @importFrom R6 R6Class
78
## usethis namespace: end
89
NULL

man/as_azure_token.Rd

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)