tests/testthat/test-par.R

request_body_text <- function(req) {
  body <- req$body %||% NULL
  if (is.null(body)) {
    return(NA_character_)
  }
  if (identical(body$type, "raw")) {
    return(rawToChar(body$data))
  }
  if (identical(body$type, "form")) {
    data <- body$data %||% list()
    if (!length(data)) {
      return("")
    }

    parts <- unlist(
      lapply(seq_along(data), function(i) {
        nm <- names(data)[[i]]
        paste0(
          nm,
          "=",
          utils::URLencode(as.character(data[[i]])[[1]], reserved = TRUE)
        )
      }),
      use.names = FALSE
    )
    return(paste(parts, collapse = "&"))
  }

  NA_character_
}

count_fixed_matches <- function(text, pattern) {
  matches <- gregexpr(pattern, text, fixed = TRUE)[[1]]
  sum(matches > 0L)
}

make_par_test_client <- function(
  token_auth_style = "body",
  client_id = "abc",
  client_secret = "",
  scopes = character(0),
  resource = character(0),
  extra_auth_params = list(),
  extra_token_headers = character(),
  client_assertion_audience = NULL,
  par_url = "https://example.com/par",
  issuer = NA_character_,
  dpop_private_key = NULL,
  response_mode = NULL,
  authorization_request_front_channel_mode = "compat"
) {
  prov <- oauth_provider(
    name = "example",
    auth_url = "https://example.com/auth",
    token_url = "https://example.com/token",
    par_url = par_url,
    issuer = issuer,
    authorization_request_front_channel_mode = authorization_request_front_channel_mode,
    use_nonce = FALSE,
    use_pkce = TRUE,
    token_auth_style = token_auth_style,
    id_token_required = FALSE,
    id_token_validation = FALSE,
    extra_auth_params = extra_auth_params,
    extra_token_headers = extra_token_headers,
    allowed_token_types = character()
  )

  oauth_client(
    provider = prov,
    client_id = client_id,
    client_secret = client_secret,
    redirect_uri = "http://localhost:8100",
    scopes = scopes,
    resource = resource,
    client_assertion_audience = client_assertion_audience,
    dpop_private_key = dpop_private_key,
    response_mode = response_mode,
    state_store = cachem::cache_mem(max_age = 60),
    state_key = paste0(
      "0123456789abcdefghijklmnopqrstuvwxyz",
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    )
  )
}

test_that("prepare_call pushes authorization params and redirects with request_uri", {
  cli <- make_par_test_client()
  body_text <- NULL

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      body_text <<- request_body_text(req)
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(
    auth_url,
    "request_uri=urn%3Aietf%3Aparams%3Aoauth%3Arequest_uri%3Atest"
  )
  expect_match(auth_url, "client_id=abc")
  expect_identical(
    attr(auth_url, "shinyOAuth.par_request_uri"),
    "urn:ietf:params:oauth:request_uri:test"
  )
  expect_identical(
    attr(auth_url, "shinyOAuth.par_expires_in"),
    90L
  )
  expect_s3_class(
    attr(auth_url, "shinyOAuth.par_expires_at"),
    "POSIXct"
  )
  expect_true(attr(auth_url, "shinyOAuth.par_expires_at") > Sys.time())
  expect_false(grepl("[?&]state=", auth_url))
  expect_false(grepl("[?&]redirect_uri=", auth_url))
  expect_false(grepl("[?&]code_challenge=", auth_url))

  expect_match(body_text, "response_type=code")
  expect_match(body_text, "client_id=abc")
  expect_match(body_text, "redirect_uri=http%3A%2F%2Flocalhost%3A8100")
  expect_match(body_text, "state=")
  expect_match(body_text, "code_challenge=")
  expect_match(body_text, "code_challenge_method=S256")
})

test_that("PAR pushes form_post response_mode in authorization request body", {
  cli <- make_par_test_client(response_mode = "form_post")
  body_text <- NULL

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      body_text <<- request_body_text(req)
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(body_text, "response_mode=form_post")
  expect_match(auth_url, "[?&]request_uri=")
  expect_false(grepl("[?&]response_mode=", auth_url))
})

test_that("PAR minimal front-channel mode omits duplicated OIDC params", {
  cli <- make_par_test_client(
    issuer = "https://issuer.example.com",
    scopes = c("openid"),
    authorization_request_front_channel_mode = "minimal"
  )
  body_text <- NULL

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      body_text <<- request_body_text(req)
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(
    auth_url,
    "request_uri=urn%3Aietf%3Aparams%3Aoauth%3Arequest_uri%3Atest"
  )
  expect_match(auth_url, "client_id=abc")
  expect_false(grepl("[?&]response_type=", auth_url))
  expect_false(grepl("[?&]scope=", auth_url))

  expect_match(body_text, "response_type=code")
  expect_match(body_text, "client_id=abc")
  expect_match(body_text, "scope=openid")
  expect_match(body_text, "redirect_uri=http%3A%2F%2Flocalhost%3A8100")
})

test_that("PAR requests keep nonce-challenge retries enabled", {
  cli <- make_par_test_client()
  retry_idempotent <- NULL

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      retry_args <- list(...)
      retry_idempotent <<- retry_args$idempotent %||% NULL
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(
    auth_url,
    "request_uri=urn%3Aietf%3Aparams%3Aoauth%3Arequest_uri%3Atest"
  )
  expect_identical(retry_idempotent, TRUE)
})

test_that("PAR attaches DPoP proof and retries once on nonce challenge", {
  cli <- make_par_test_client(dpop_private_key = openssl::rsa_keygen())
  proofs <- character(0)
  retry_count <- 0L

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      retry_count <<- retry_count + 1L
      dry <- httr2::req_dry_run(
        req,
        quiet = TRUE,
        redact_headers = FALSE
      )
      proofs <<- c(proofs, dry$headers$dpop %||% NA_character_)

      if (retry_count == 1L) {
        return(httr2::response(
          url = as.character(req$url),
          status = 400,
          headers = list(
            "content-type" = "application/json",
            "dpop-nonce" = "par-nonce-1"
          ),
          body = charToRaw('{"error":"use_dpop_nonce"}')
        ))
      }

      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(auth_url, "request_uri=")
  expect_identical(retry_count, 2L)
  expect_true(all(vapply(proofs, nzchar, logical(1))))

  first_payload <- jsonlite::fromJSON(
    shinyOAuth:::base64url_decode(strsplit(proofs[[1]], ".", fixed = TRUE)[[
      1
    ]][[2]])
  )
  second_payload <- jsonlite::fromJSON(
    shinyOAuth:::base64url_decode(strsplit(proofs[[2]], ".", fixed = TRUE)[[
      1
    ]][[2]])
  )

  expect_false("nonce" %in% names(first_payload))
  expect_identical(second_payload$nonce, "par-nonce-1")
})

test_that("PAR HTTP failures surface as PAR-specific errors", {
  cli <- make_par_test_client()

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 400,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"error":"invalid_request","error_description":"PAR rejected"}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "Pushed authorization request failed|PAR rejected|invalid_request"
  )
  expect_length(cli@state_store$keys(), 0L)
})

test_that("PAR rejects redirect responses", {
  cli <- make_par_test_client()

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 302,
        headers = list(location = "https://evil.example.com/par"),
        body = charToRaw("")
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "Unexpected redirect response during pushed_authorization_request"
  )
  expect_length(cli@state_store$keys(), 0L)
})

test_that("prepare_call preserves repeated resource indicators in PAR body", {
  cli <- make_par_test_client(
    resource = c(
      "https://api.example.com",
      "urn:example:ledger"
    )
  )
  body_text <- NULL

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      body_text <<- request_body_text(req)
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(auth_url, "request_uri=")
  expect_identical(count_fixed_matches(body_text, "resource="), 2L)
  expect_match(body_text, "resource=https%3A%2F%2Fapi\\.example\\.com")
  expect_match(body_text, "resource=urn%3Aexample%3Aledger")
})

test_that("prepare_call forwards extra auth params into the PAR body", {
  cli <- make_par_test_client(
    extra_auth_params = list(
      prompt = "login",
      custom_param = "value-1",
      custom_multi = c("alpha", "beta")
    )
  )
  body_text <- NULL

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      body_text <<- request_body_text(req)
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(auth_url, "request_uri=")
  expect_false(grepl("[?&]prompt=", auth_url))
  expect_false(grepl("[?&]custom_param=", auth_url))
  expect_false(grepl("[?&]custom_multi=", auth_url))

  expect_match(body_text, "prompt=login")
  expect_match(body_text, "custom_param=value-1")
  expect_identical(count_fixed_matches(body_text, "custom_multi="), 2L)
  expect_match(body_text, "custom_multi=alpha")
  expect_match(body_text, "custom_multi=beta")
})

test_that("PAR response requires request_uri and expires_in", {
  cli <- make_par_test_client()

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw('{"expires_in":90}')
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "request_uri"
  )
  expect_length(cli@state_store$keys(), 0L)

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test"}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "expires_in"
  )
  expect_length(cli@state_store$keys(), 0L)
})

test_that("PAR response requires 201 JSON with integer expires_in", {
  cli <- make_par_test_client()

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 200,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "HTTP 201 Created|Status 200"
  )
  expect_length(cli@state_store$keys(), 0L)

  cli <- make_par_test_client()
  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "text/plain"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "not JSON|Content-Type"
  )
  expect_length(cli@state_store$keys(), 0L)

  cli <- make_par_test_client()
  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":"90"}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "expires_in"
  )
  expect_length(cli@state_store$keys(), 0L)

  cli <- make_par_test_client()
  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90.5}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "positive integer"
  )
  expect_length(cli@state_store$keys(), 0L)

  cli <- make_par_test_client()
  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          paste0(
            '{"request_uri":"urn:ietf:params:oauth:request_uri:first",',
            '"request_uri":"urn:ietf:params:oauth:request_uri:second",',
            '"expires_in":90}'
          )
        )
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    shinyOAuth:::prepare_call(cli, valid_browser_token()),
    regexp = "duplicate member name: request_uri"
  )
  expect_length(cli@state_store$keys(), 0L)
})

test_that("provider reserves request_uri and request object parameters", {
  expect_error(
    oauth_provider(
      name = "example",
      auth_url = "https://example.com/auth",
      token_url = "https://example.com/token",
      use_nonce = FALSE,
      use_pkce = TRUE,
      id_token_required = FALSE,
      id_token_validation = FALSE,
      extra_auth_params = list(request_uri = "urn:example:test")
    ),
    regexp = "request_uri"
  )

  expect_error(
    oauth_provider(
      name = "example",
      auth_url = "https://example.com/auth",
      token_url = "https://example.com/token",
      use_nonce = FALSE,
      use_pkce = TRUE,
      id_token_required = FALSE,
      id_token_validation = FALSE,
      extra_auth_params = list(request = "signed-request-object")
    ),
    regexp = "request"
  )

  cli <- make_par_test_client()
  expect_error(
    shinyOAuth:::push_authorization_request(
      cli,
      list(request_uri = "urn:attacker:request")
    ),
    regexp = "must not include request_uri"
  )
})

test_that("PAR endpoints follow the regular non-https host policy", {
  prov <- oauth_provider(
    name = "example",
    auth_url = "https://example.com/auth",
    token_url = "https://example.com/token",
    par_url = "http://localhost:9000/par",
    use_nonce = FALSE,
    use_pkce = TRUE,
    id_token_required = FALSE,
    id_token_validation = FALSE
  )

  expect_identical(prov@par_url, "http://localhost:9000/par")

  withr::local_options(list(shinyOAuth.allowed_non_https_hosts = character()))

  expect_error(
    oauth_provider(
      name = "example",
      auth_url = "https://example.com/auth",
      token_url = "https://example.com/token",
      par_url = "http://localhost:9000/par",
      use_nonce = FALSE,
      use_pkce = TRUE,
      id_token_required = FALSE,
      id_token_validation = FALSE
    ),
    regexp = "par_url provided but not accepted as a host"
  )
})

test_that("OIDC discovery rejects inconsistent required PAR metadata", {
  disc_body <- jsonlite::toJSON(
    list(
      issuer = "https://issuer.example.com",
      authorization_endpoint = "https://issuer.example.com/auth",
      token_endpoint = "https://issuer.example.com/token",
      jwks_uri = "https://issuer.example.com/jwks",
      response_types_supported = list("code"),
      subject_types_supported = list("public"),
      id_token_signing_alg_values_supported = list("RS256"),
      token_endpoint_auth_methods_supported = list(
        "client_secret_basic",
        "client_secret_post"
      ),
      require_pushed_authorization_requests = TRUE
    ),
    auto_unbox = TRUE
  )

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 200,
        headers = list("content-type" = "application/json"),
        body = charToRaw(disc_body)
      )
    },
    .package = "shinyOAuth"
  )

  expect_error(
    oauth_provider_oidc_discover("https://issuer.example.com"),
    regexp = "pushed_authorization_request_endpoint"
  )
})

test_that("OIDC discovery applies the regular non-https host policy to PAR", {
  disc_body <- jsonlite::toJSON(
    list(
      issuer = "https://issuer.example.com",
      authorization_endpoint = "https://issuer.example.com/auth",
      token_endpoint = "https://issuer.example.com/token",
      jwks_uri = "https://issuer.example.com/jwks",
      response_types_supported = list("code"),
      subject_types_supported = list("public"),
      id_token_signing_alg_values_supported = list("RS256"),
      token_endpoint_auth_methods_supported = list(
        "client_secret_basic",
        "client_secret_post"
      ),
      pushed_authorization_request_endpoint = "http://issuer.example.com/par"
    ),
    auto_unbox = TRUE
  )

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 200,
        headers = list("content-type" = "application/json"),
        body = charToRaw(disc_body)
      )
    },
    .package = "shinyOAuth"
  )

  withr::local_options(list(
    shinyOAuth.allowed_hosts = "issuer.example.com",
    shinyOAuth.allowed_non_https_hosts = "issuer.example.com"
  ))

  prov <- oauth_provider_oidc_discover(
    "https://issuer.example.com"
  )
  expect_identical(
    prov@par_url,
    "http://issuer.example.com/par"
  )
})

test_that("OIDC discovery rejects PAR when the regular non-https host policy forbids it", {
  disc_body <- jsonlite::toJSON(
    list(
      issuer = "https://issuer.example.com",
      authorization_endpoint = "https://issuer.example.com/auth",
      token_endpoint = "https://issuer.example.com/token",
      jwks_uri = "https://issuer.example.com/jwks",
      response_types_supported = list("code"),
      subject_types_supported = list("public"),
      id_token_signing_alg_values_supported = list("RS256"),
      token_endpoint_auth_methods_supported = list(
        "client_secret_basic",
        "client_secret_post"
      ),
      pushed_authorization_request_endpoint = "http://issuer.example.com/par"
    ),
    auto_unbox = TRUE
  )

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 200,
        headers = list("content-type" = "application/json"),
        body = charToRaw(disc_body)
      )
    },
    .package = "shinyOAuth"
  )

  withr::local_options(list(
    shinyOAuth.allowed_hosts = "issuer.example.com",
    shinyOAuth.allowed_non_https_hosts = character()
  ))

  expect_error(
    oauth_provider_oidc_discover("https://issuer.example.com"),
    regexp = "Endpoint host or scheme not allowed"
  )
})

test_that("client_secret_jwt PAR request sends client assertion and omits secret", {
  cli <- make_par_test_client(
    token_auth_style = "client_secret_jwt",
    client_secret = paste(rep("s", 32), collapse = "")
  )
  captured <- NULL

  testthat::local_mocked_bindings(
    req_body_form = function(req, ...) {
      captured <<- list(...)
      req
    },
    .package = "httr2"
  )
  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(auth_url, "request_uri=")
  expect_identical(
    captured$client_assertion_type,
    "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
  )
  expect_true(
    is.character(captured$client_assertion) && nzchar(captured$client_assertion)
  )
  expect_false("client_secret" %in% names(captured))
})

test_that("client_secret_jwt PAR retries rebuild client assertions", {
  cli <- make_par_test_client(
    token_auth_style = "client_secret_jwt",
    client_secret = paste(rep("s", 32), collapse = "")
  )
  seen_jtis <- character(0)

  withr::local_options(list(
    shinyOAuth.retry_max_tries = 2L,
    shinyOAuth.retry_backoff_base = 0.01,
    shinyOAuth.retry_backoff_cap = 0.01
  ))

  testthat::local_mocked_bindings(
    req_perform = function(req) {
      body_text <- request_body_text(req)
      assertion <- parse_query_param(
        paste0("https://example.com/?", body_text),
        "client_assertion",
        decode = TRUE
      )
      payload <- shinyOAuth:::parse_jwt_payload(assertion)
      seen_jtis <<- c(seen_jtis, payload$jti %||% NA_character_)

      if (length(seen_jtis) == 1L) {
        return(httr2::response(
          url = as.character(req$url),
          status = 500,
          headers = list("content-type" = "application/json"),
          body = charToRaw("{}")
        ))
      }

      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "httr2"
  )
  testthat::local_mocked_bindings(
    Sys.sleep = function(time) invisible(NULL),
    .package = "base"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(auth_url, "request_uri=")
  expect_length(seen_jtis, 2L)
  expect_length(unique(seen_jtis), 2L)
})

test_that("PAR body auth omits client_secret for public clients and keeps extra headers", {
  cli <- make_par_test_client(
    token_auth_style = "body",
    client_secret = "",
    extra_token_headers = c(`X-Test-Par` = "ok")
  )
  body_text <- NULL
  seen_header <- NULL

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      body_text <<- request_body_text(req)
      seen_header <<- req$headers$`X-Test-Par` %||% NULL
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(auth_url, "request_uri=")
  expect_match(body_text, "client_id=abc")
  expect_false(grepl("client_secret=", body_text, fixed = TRUE))
  expect_identical(seen_header, "ok")
})

test_that("PAR header auth uses Basic auth and keeps client_secret out of body", {
  cli <- make_par_test_client(
    token_auth_style = "header",
    client_secret = "secret"
  )
  body_text <- NULL
  auth_header_names <- character()

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      body_text <<- request_body_text(req)
      auth_header_names <<- names(as.list(req[["headers"]]))
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  auth_url <- shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_match(auth_url, "request_uri=")
  expect_true("Authorization" %in% auth_header_names)
  expect_false(grepl("client_secret=", body_text, fixed = TRUE))
})

test_that("PAR JWT client assertions target par_url by default and allow audience override", {
  cli_default <- make_par_test_client(
    token_auth_style = "client_secret_jwt",
    client_secret = paste(rep("s", 32), collapse = "")
  )
  captured_default <- NULL

  testthat::local_mocked_bindings(
    req_body_form = function(req, ...) {
      captured_default <<- list(...)
      req
    },
    .package = "httr2"
  )
  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  shinyOAuth:::prepare_call(cli_default, valid_browser_token())
  payload_default <- shinyOAuth:::parse_jwt_payload(
    captured_default$client_assertion
  )
  expect_identical(payload_default$aud, cli_default@provider@par_url)

  cli_override <- make_par_test_client(
    token_auth_style = "client_secret_jwt",
    client_secret = paste(rep("s", 32), collapse = ""),
    client_assertion_audience = "https://example.com/custom-par-aud"
  )
  captured_override <- NULL

  testthat::local_mocked_bindings(
    req_body_form = function(req, ...) {
      captured_override <<- list(...)
      req
    },
    .package = "httr2"
  )
  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  shinyOAuth:::prepare_call(cli_override, valid_browser_token())
  payload_override <- shinyOAuth:::parse_jwt_payload(
    captured_override$client_assertion
  )
  expect_identical(payload_override$aud, "https://example.com/custom-par-aud")
})

test_that("PAR JWT client assertions prefer issuer when available", {
  cli <- make_par_test_client(
    token_auth_style = "client_secret_jwt",
    client_secret = paste(rep("s", 32), collapse = ""),
    scopes = c("openid"),
    issuer = "https://example.com"
  )
  captured <- NULL

  testthat::local_mocked_bindings(
    req_body_form = function(req, ...) {
      captured <<- list(...)
      req
    },
    .package = "httr2"
  )
  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 201,
        headers = list("content-type" = "application/json"),
        body = charToRaw(
          '{"request_uri":"urn:ietf:params:oauth:request_uri:test","expires_in":90}'
        )
      )
    },
    .package = "shinyOAuth"
  )

  shinyOAuth:::prepare_call(cli, valid_browser_token())

  expect_identical(
    shinyOAuth:::parse_jwt_payload(captured$client_assertion)$aud,
    cli@provider@issuer
  )
})

test_that("OIDC discovery wires PAR metadata into provider", {
  disc_body <- jsonlite::toJSON(
    list(
      issuer = "https://issuer.example.com",
      authorization_endpoint = "https://issuer.example.com/auth",
      token_endpoint = "https://issuer.example.com/token",
      jwks_uri = "https://issuer.example.com/jwks",
      response_types_supported = list("code"),
      subject_types_supported = list("public"),
      id_token_signing_alg_values_supported = list("RS256"),
      token_endpoint_auth_methods_supported = list(
        "client_secret_basic",
        "client_secret_post"
      ),
      pushed_authorization_request_endpoint = "https://issuer.example.com/par"
    ),
    auto_unbox = TRUE
  )

  testthat::local_mocked_bindings(
    req_with_retry = function(req, ...) {
      httr2::response(
        url = as.character(req$url),
        status = 200,
        headers = list("content-type" = "application/json"),
        body = charToRaw(disc_body)
      )
    },
    .package = "shinyOAuth"
  )

  prov <- oauth_provider_oidc_discover("https://issuer.example.com")

  expect_identical(prov@par_url, "https://issuer.example.com/par")
})

Try the shinyOAuth package in your browser

Any scripts or data that you put into this service are public.

shinyOAuth documentation built on May 23, 2026, 9:06 a.m.