Skip to content

req_oauth_password prompts password-input continuously, but existing valid token are used #498

@exetico

Description

@exetico

I was facing some issues in #491, but now the API part are working as expected, so I'm able to use req_oauth_password. However, I noticed that #119 points out that req_oauth_password triggers the "Password" input prompt on all request. Even though, it's actually saving the token in the first execution, and does re-use the token in the next execution.

Have I implemented something in a wrong way for my first "initial httr2-based test project"? There's no solution in #119, but hadley does request an example.

I use the following simple test-code to try and fetch "me" from "users":

get_users_me <- function() {
  response <- NULL

  if (interactive()) {
    tryCatch({
      response <- request("https://domain.com/users/me") |>
       req_auth() |>
       req_perform()
     
     # Handle the response inside the conditional
     content <- response |> resp_body_json(auto_unbox = TRUE)
    }, error = function(e) {
      stop("An error occurred: ", conditionMessage(e))
    })
  } else {
    # Handle non-interactive case, e.g., tests or scripts
    # Make sure to define how response should be handled or mocked here
  }

  # return content
  content
}

I use the following for my (currently) test client, and the wrapper for my req_auth:

test_con_client <- function() {
  oauth_client(
    id = "28acfec0674bb3da9f39",
    token_url = "https://domain.com/oauth/token",
    name = "client-r-package"
  )
}

req_auth <- function(req) {
  req_oauth_password(req,
    client = test_con_client(),
    username = "user@domain.com"
  )
}

Using cache_disk does not change a thing. But it do look like it's using the existing token, even through it re-prompts me, asking me to enter the password again.

I noticed this by running with_verbosity(get_users_me()):

  1. with_verbosity(get_users_me())
    a) does a POST /oauth/token HTTP/2
    b) get's a HTTP/2 200
    c) requests the resources at GET /users/me HTTP/2 (The Bearer are set in the headers, as expected)
    d) gets a HTTP/2 200

  2. same R session, with_verbosity(get_users_me()) executed again.
    a) does a GET /users/me HTTP/2 (The Bearer are set in the headers, as expected)
    b) gets a HTTP/2 200

However, the password input prompt shows up on both executions:

billede

Ignore the code in the background.

In other words, it's clear to me, that the password are not used in run no. 2, but are still being prompted to the user, for some reason.

Is this a bug, or do I need to check something, and only wrap req_oauth_password, if it needs to be re-authenticated? (initial, once expired, and so on)?

If I run the official example with github_client with req_oauth_auth_code, things works as I expect it to do: 1. run prompts me to grant access, and fetch the information. 2. run just fetches the information. I'm not "reprompted" to grant access.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions