R

How to use password and token securely in R

Secure credtentials in R

Here is how to use password and token securely in R scripts using the keyring package and RStudio. This might be more helpful for Windows users as I explain how the password is accessible outside the R script.

A token is not always a mandatory requirement to authenticate. On the other hand, it might be in some situations, and I will cover that.

First of all, to use password and token securely in R, you should install a keyring package.

install.packages('keyring')

Use password and token securely in R scripts

Set up your username and password by using the key_set function from keyring.

require(keyring)

key_set(service = 'MyExample', username = 'dummy_username')

At the same time, when setting up a username, there will be a prompt where you can enter the password.

RStudio password prompt

To use your username and password in R scripts, you can access that by using the key_list and key_get function.

username <- key_list('MyExample')[1, 2]
username
#[1] "dummy_username"

password <-  key_get('MyExample', username)
password
#[1] "12345"

If you want to secure you’re token in R, you can skip the part with username and do it simpler.

key_set(service = 'MyToken')

When setting up a token, there will be a prompt where you can enter that. After storing your token outside the R script you can test what you can get.

token <-  key_get('MyToken')
token
#[1] "thisistoken"

 

Access keyring stored credentials in Windows

In Windows, the keyring package is making credentials available in the Credentials Manager.

In situations when something is changing, you can access them in the Windows Credentials section. If you have to change your password, you can do that right there.

access R credentials in Windows Credential Manager

 

More about securing your credentials in R script

Here is a great explanation about the best practices of managing credentials by RStudio. There is also an option to prompt for the password, and you can use an additional layer of security with keyring_create() that requires password do access credentials.

The previously shown process with an additional security layer to the access token would look like this.

require(keyring)

keyring_create("MyKeyRing")

key_set(service = 'MyToken', keyring = "MyKeyRing")

token <-  key_get('MyToken', keyring = "MyKeyRing")

 

Are you working with the dplyr package? Check out my favorite dplyr tips and tricks.




0 comments on “How to use password and token securely in R

Leave a Reply

Your email address will not be published. Required fields are marked *