rstudioapi askForPassword without masking for username entry
Asked Answered
L

2

7

Is there a way to disable the masking in the rstudioapi::askForPassword pop up window so a user can enter their username?

I am building a function I can share with my team for connecting to our Oracle DB instance using the ROracle pacakge

Current solution,

  connection <- dbConnect(
    driver
    , username = rstudioapi::askForPassword(prompt = 'Please enter username: ')
    , password = rstudioapi::askForPassword(prompt = 'Password: ')
    , dbname = 'my.connection.string'
  )

This pops up the following prompt: (image in link)

which is the solution described in the RStudio database docs that uses the Rstudio api

Would like to fund a solution that does not mask the password but pops up the same prompt (one nice line of code if possible...)

Lashoh answered 6/9, 2017 at 16:58 Comment(4)
the function you are using doesn't allow unmasked input, why not use readline() from base? If you need it to be graphical, writing a Shiny Gadget (miniUI) for this is trivial.Vidovik
not using readline from base because easier for users on my team to see the modal pop up (but admit its possible to use it). will look into the shiny gadget use case, have only used shiny for fully fledged web appsLashoh
preview edition flagged a virustotal error for my organization so just waiting for approval then going to roll it out to my teamLashoh
finally managed to get it globally approved. have my whole department upgrading now this is amazing thank youLashoh
V
9

If you are using the preview version of RStudio (1.1.67+) there are newer functions available to you in the rstudioapi package, showPrompt seems to accomplish what you are after.

connection <- dbConnect(
    driver,
    username = rstudioapi::showPrompt(
      title = "Username", message = "Username", default = ""
    ),
    password = rstudioapi::askForPassword(prompt = "Password"),
    dbname = "my.connection.string"
)
Vidovik answered 7/9, 2017 at 23:43 Comment(1)
this worked BEAUTIFULLY! thank you so much I am having my whole department upgrade to the latestLashoh
A
7

For newer versions of rstudioapi::askForPassword I noticed the following...

  • If you just include the string username in your argument phrase, the text is unmasked.
  • Including password will strictly enforce masking.

For example:

rstudioapi::askForPassword("Enter username") # <-- UNMASKED
rstudioapi::askForPassword("Enter password") # <-- MASKED

rstudioapi::askForPassword("Enter foobar xyz123 username") # <-- UNMASKED
rstudioapi::askForPassword("Enter foobar xyz123 username password") # <-- MASKED 

I stumbled on this by accident of course, in that the pw prompted text box automatically unmasks for username... There could be more info at:

Ambient answered 11/5, 2021 at 4:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.