The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
Query git credentials from R
(Re)use the same credentials in command line git, R and the RStudio IDE., etc. Users can set their GitHub token once and use it everywhere.
Typically more secure than storing passwords and tokens in
.Renviron
files.
gitcreds has a cache that makes credential lookup very fast.
gitcreds supports multiple users and multiple hosts, including Enterprise GitHub installations.
If git or git credential helpers are not available, e.g. typically on a Linux server, or a CI, then gitcreds can fall back to use environment variables, and it still supports multiple users and hosts.
Install the package from CRAN:
install.packages("gitcreds")
gitcreds is typically used upstream, in R packages that need to authenticate to git or GitHub. End users of these packages might still find it useful to call gitcreds directly, to set up their credentials, or check that they have been set up correctly.
You can also use gitcreds in an R script. In this case you are both the end user and the upstream developer.
library(gitcreds)
Use gitcreds_get()
to check your GitHub or other git
credentials. It returns a named list, with a password
entry. The password is not printed by default:
gitcreds_get()
#> <gitcreds>
#> protocol: https
#> host : github.com
#> username: gaborcsardi
#> password: <-- hidden -->
Use gitcreds_set()
to add new credentials, or replace
existing ones. It always asks you before replacing existing
credentials:
gitcreds_set()
#> -> Your current credentials for 'https://github.com':
#>
#> protocol: https
#> host : github.com
#> username: gaborcsardi
#> password: <-- hidden -->
#>
#> -> What would you like to do?
#>
#> 1: Keep these credentials
#> 2: Replace these credentials
#> 3: See the password / token
#>
#> Selection: 2
#>
#> ? Enter new password or token: secret
#> -> Removing current credentials...
#> -> Adding new credentials...
#> -> Removing credentials from cache...
#> -> Done.
Use gitcreds_delete()
to delete credentials. It always
asks you before actually deleting any credentials:
gitcreds_delete()
#> -> Your current credentials for 'https://github.com':
#>
#> protocol: https
#> host : github.com
#> username: token
#> password: <-- hidden -->
#>
#> -> What would you like to do?
#>
#> 1: Keep these credentials
#> 2: Delete these credentials
#> 3: See the password / token
#>
#> Selection: 2
#> -> Removing current credentials...
#> -> Removing credentials from cache...
#> -> Done.
If you want to use git’s credentials in your package, call
gitcreds_get()
. You probably want to handle the various
errors it can return. Here is an example for a function that optionally
neeeds a GitHub token. It searches the code of a GitHub repository:
<- function(query, repo = "wch/r-source") {
github_search <- tryCatch(
token ::gitcreds_get(),
gitcredserror = function(e) NULL
)
<- "https://api.github.com/search/code"
url <- list(q = paste0(query, "+repo:", repo))
q <- paste0("token ", token$password)
token
::GET(url, query = q, httr::add_headers(Authorization = token))
httr }
The next example always needs a GitHub token, so it fails without one. It lists the public repositories of the current user:
<- function(wh) {
msg <- c(
msgs no_git = paste0(
"No git installation found. You need to install git and set up ",
"your GitHub Personal Access token using `gitcreds::gitcreds_set()`."),
no_creds = paste0(
"No git credentials found. Please set up your GitHub Personal Access ",
"token using `gitcreds::gitcreds_set()`.")
)
msgs[wh]
}
<- function() {
my_private_repos <- tryCatch(
token ::gitcreds_get(),
gitcredsgitcreds_nogit_error = function(e) stop(msg("no_git")),
gitcreds_no_credentials = function(e) stop(msg("no_creds"))
)
<- "https://api.github.com/user/repos"
url <- list(visibility = "public")
q <- paste0("token ", token$password)
token
::GET(url, query = q, httr::add_headers(Authorization = token))
httr }
Point your users to gitcreds_set()
for adding/updating
their credentials, or write your own wrapper for this.
If you want more control or a different UI, take a look at the lower
level gitcreds_fill()
, gitcreds_approve()
and
gitcreds_reject()
functions.
See also gitcreds for package authors.
Please note that the gitcreds project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
MIT © RStudio
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.