Giter Club home page Giter Club logo

gitcreds's Introduction

gitcreds

Query git credentials from R

R build status Codecov test coverage R-CMD-check

Features

  • (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.

Installation

Install the package from CRAN:

install.packages("gitcreds")

Install the development version from GitHub:

pak::pak("r-lib/gitcreds")

Usage

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.

Usage as an end user

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.

Usage as a package author

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:

github_search <- function(query, repo = "wch/r-source") {
  token <- tryCatch(
    gitcreds::gitcreds_get(),
    error = function(e) NULL
  )

  url <- "https://api.github.com/search/code"
  q <- list(q = paste0(query, "+repo:", repo))
  token <- paste0("token ", token$password)

  httr::GET(url, query = q, httr::add_headers(Authorization = token))
}

The next example always needs a GitHub token, so it fails without one. It lists the public repositories of the current user:

msg <- function(wh) {
  msgs <- c(
    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]
}

my_private_repos <- function() {
  token <- tryCatch(
    gitcreds::gitcreds_get(),
    gitcreds_nogit_error = function(e) stop(msg("no_git")),
    gitcreds_no_credentials = function(e) stop(msg("no_creds"))    
  )

  url <- "https://api.github.com/user/repos"
  q <- list(visibility = "public")
  token <- paste0("token ", token$password)

  httr::GET(url, query = q, httr::add_headers(Authorization = token))
}

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.

Code of Conduct

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.

License

MIT © RStudio

gitcreds's People

Contributors

gaborcsardi avatar jayleetx avatar jennybc avatar jesse-ross avatar jimhester avatar melissavanbussel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gitcreds's Issues

Upkeep for gitcreds (2022)

2022

  • Handle and close any still-open master --> main issues
  • usethis:::use_codecov_badge("r-lib/gitcreds")
  • Update pkgdown site using instructions at https://tidytemplate.tidyverse.org
  • Update lifecycle badges with more accessible SVGs: usethis::use_lifecycle()

2023

  • Update copyright holder in DESCRIPTION: person("Posit Software, PBC", role = c("cph", "fnd"))
  • Run devtools::document() to re-generate package-level help topic with DESCRIPTION changes
  • usethis::use_tidy_logo()
  • usethis::use_tidy_coc()
  • Use pak::pak("r-lib/gitcreds") in README
  • Consider running usethis::use_tidy_dependencies() and/or replace compat files with use_standalone()
  • #51
  • #52
    or file an issue if you don't have time to do it now
  • #53

Eternal

  • usethis::use_mit_license()
  • usethis::use_package("R", "Depends", "3.6")
  • usethis::use_tidy_description()
  • usethis::use_tidy_github_actions()
  • devtools::build_readme()
  • Re-publish released site if needed

Created on 2023-11-03 with usethis::use_tidy_upkeep_issue(), using usethis v2.2.2.9000

Can not register github token

When I'm trying to register github tocken, I get the following error. How should I deal with it?

gitcreds::gitcreds_set()
#>Error in throw(new_error("gitcreds_nogit_error")) : 
#>  Could not find system git

Spelling mistake on message

When running gitcreds_set() then option to replace the credentials the message has a spelling mistake (2nd line):

-> Adding new credentials...
-> Removing credetials from cache...
-> Done.

Generic PAT prefix when using non-github urls

This is purely cosmetic, but I've found it has a rather profound user experience impact.

When determining which environment variable to use, non-github clients still get prefixed with GITHUB_PAT. When using codeberg or GitLab, generating a GITHUB_PAT_ENTERPRISE_COM is sort of misleading.

Although just cosmetic, this is often a huge point of confusion when onboarding more junior developers, who might be encountering private git authentication and their first non-GitHub service for the first time. At my place of work this is exacerbated by having both enterprise GitHub and its competitors serving different parts of the organization, adding to confusion about which services are for which purposes.

Using a more generic GIT_PAT_ENTERPRISE_COM would go a long way toward easing adoption.

For specific hosts, having fallbacks that reflect individual providers' recommendations could always remain as special cases.

Upkeep for gitcreds

Pre-history

  • usethis::use_readme_rmd()
  • usethis::use_roxygen_md()
  • usethis::use_github_links()
  • usethis::use_pkgdown_github_pages()
  • usethis::use_tidy_github_labels()
  • usethis::use_tidy_style()
  • usethis::use_tidy_description()
  • urlchecker::url_check()

2020

  • usethis::use_package_doc()
    Consider letting usethis manage your @importFrom directives here.
    usethis::use_import_from() is handy for this.
  • usethis::use_testthat(3) and upgrade to 3e, testthat 3e vignette
  • Align the names of R/ files and test/ files for workflow happiness.
    usethis::rename_files() can be helpful.

2021

  • usethis::use_tidy_dependencies()
  • usethis::use_tidy_github_actions() and update artisanal actions to use setup-r-dependencies
  • Remove check environments section from cran-comments.md
  • Bump required R version in DESCRIPTION to 3.4
  • Use lifecycle instead of artisanal deprecation messages, as described in Communicate lifecycle changes in your functions
  • Add RStudio to DESCRIPTION as funder, if appropriate

2022

Ship our own credential helper on Linux

This is tricky for many reasons:

  • should the credential helper call R? That is messy and not super reliable. Or should it be a standalone binary? That seems like a better option.
  • how should be actually store credentials. We need to look at what gh does Linux. It apparently installs its own handler and stores credentials internally:
    root@99340efa4c28:~# cat .gitconfig
    [credential "https://github.com"]
        helper =
        helper = !/usr/bin/gh auth git-credential
    [credential "https://gist.github.com"]
        helper =
        helper = !/usr/bin/gh auth git-credential
    

Move `master` branch to `main`

The master branch of this repository will soon be renamed to main, as part of a coordinated change across several GitHub organizations (including, but not limited to: tidyverse, r-lib, tidymodels, and sol-eng). We anticipate this will happen by the end of September 2021.

That will be preceded by a release of the usethis package, which will gain some functionality around detecting and adapting to a renamed default branch. There will also be a blog post at the time of this master --> main change.

The purpose of this issue is to:

  • Help us firm up the list of targetted repositories
  • Make sure all maintainers are aware of what's coming
  • Give us an issue to close when the job is done
  • Give us a place to put advice for collaborators re: how to adapt

message id: euphoric_snowdog

gitcreds_set() appears ineffective

Problem

gitcreds_set() doesn't seem to be setting anything:

> gitcreds::gitcreds_set()


? Enter password or token: ghp_XXXX
-> Adding new credentials...
-> Removing credentials from cache...
-> Done.
> usethis::gh_token_help()
* GitHub host: 'https://github.com'
* Personal access token for 'https://github.com': <unset>
* To create a personal access token, call `create_github_token()`
* To store a token for current and future use, call `gitcreds::gitcreds_set()`
i Read more in the 'Managing Git(Hub) Credentials' article:
  https://usethis.r-lib.org/articles/articles/git-credentials.html
> gitcreds::gitcreds_get()
Error in throw(new_error("gitcreds_no_credentials", url = url)) : 
  Could not find any credentials

Trying gitcreds_get in a new session later yields the same result

Background Information

Based partly on questions developers asked in a somewhat similar bug, I installed oskeyring and collected additional info:

> gitcreds::gitcreds_list_helpers()
[1] "manager"      "manager-core"

> gitcreds::gitcreds_list()
[[1]]
<oskeyring_windows_item: generic>
 target_name: git:https://[email protected]
 persist: local_machine
 username: RossBoylan
 credential_blob: <-- hidden -->

[[2]]
<oskeyring_windows_item: generic>
 target_name: git:https://github.com
 persist: local_machine
 username: Personal Access Token
 credential_blob: <-- hidden -->

[[3]]
<oskeyring_windows_item: generic>
 target_name: git:https://[email protected]
 persist: local_machine
 username: PersonalAccessToken
 credential_blob: <-- hidden -->

Warning message:
In gitcreds$throw(gitcreds$new_warning("gitcreds_multiple_helpers")) :
  Multiple credential helpers, only using the first

Running under
RStudio 2023.06.1+524 "Mountain Hydrangea" Release (547dcf861cac0253a8abb52c135e44e02ba407a1, 2023-07-07) for windows
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) RStudio/2023.06.1+524 Chrome/110.0.5481.208 Electron/23.3.0 Safari/537.36

R version 4.3.1 (2023-06-16 ucrt)
git version 2.43.0.windows.1
gitcred 0.1.2

So both RStudio and R are a little behind current.

I have accessed github in various ways, including ssh, from various applications. In Windows Credential Manager (under Windows Credentials | Generic Credentials) I see at least 5 entries associated with github.com, 3 of which were last modified today. I also see that Windows and Certificate-Based Credentials both say "have been disabled by your system administrator".

is gitcreds_set() a little too aggressive with errors?

I was exploring this (very cool) package today and was surprised that, when I ran gitcreds::gitcreds_set() and kept my existing credentials, it threw an error. I expected it to end the interaction silently without a warning or error (likely something invisible(). The main reason I expected that was because selecting the "no" path doesn't seem like an error to me. I think I've also been trained to expect that behavior from usethis and devtools

gitcreds::gitcreds_set() does not persist

Hi,

I'm experiencing a bug when using gitcreds::gitcreds_set(). This looks like #30 but reading it unfortunately did not help me.

Coming from https://usethis.r-lib.org/articles/git-credentials.html#get-a-personal-access-token-pat, I created a PAT from the GitHub web interface and then tried to set it using gitcreds:

> gitcreds::gitcreds_set()


? Enter password or token: ghp_my_token
-> Adding new credentials...
-> Removing credetials from cache...
-> Done.

> gitcreds::gitcreds_get()
Error in throw(new_error("gitcreds_no_credentials", url = url)) : 
  Could not find any credentials
> gitcreds::gitcreds_delete() #no effect, no message

While there is no error in gitcreds_set, it seemed to have no effect.

I'm not really a git expert so I have no clue how to investigate this bug, so feel free to ask for anything.

Here are more info:

> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

> gitcreds::gitcreds_list_helpers()
[1] "manager"

> gitcreds::gitcreds_list()
[[1]]
<oskeyring_windows_item: generic>
 target_name: git:https://github.com
 persist: local_machine
 username: PersonalAccessToken
 credential_blob: <-- hidden -->

> usethis::git_sitrep() 
Git config (global)
* Name: 'Dan Chaltiel'
* Email: '[email protected]'
* Global (user-level) gitignore file: 'C:/Users/Dan/.gitignore'
* Vaccinated: TRUE
* Default Git protocol: 'https'
* Default initial branch name: <unset>
GitHub
* Default GitHub host: 'https://github.com'
* Personal access token for 'https://github.com': <unset>
* To create a personal access token, call `create_github_token()`
* To store a token for current and future use, call `gitcreds::gitcreds_set()`
i Read more in the 'Managing Git(Hub) Credentials' article:
  https://usethis.r-lib.org/articles/articles/git-credentials.html
Git repo for current project
* Active usethis project: 'F:/GITHUB/crosstable'
* Default branch: 'main'
* Current local branch -> remote tracking branch:
  'main' -> 'origin/main'
GitHub remote configuration
* Type = 'maybe_ours_or_theirs'
* Host = 'https://github.com'
* Config supports a pull request = NA
* origin = 'DanChaltiel/crosstable'
* upstream = <not configured>
* Desc = 'origin' is a GitHub repo and 'upstream' is either not configured or is not a GitHub repo.
  
  We may be offline or you may need to configure a GitHub personal access
  token. `gh_token_help()` can help with that.
  
  Read more about what this GitHub remote configurations means at:
  'https://happygitwithr.com/common-remote-setups.html'

Confusing output with "See password / token"

gitcreds::gitcreds_delete()
#> -> Your current credentials for 'https://github.com':
#>
#>   protocol: https
#>   host    : github.com
#>   username: lionel-
#>   password: <-- hidden -->
#>
#> -> What would you like to do?
#>
#> 1: Keep these credentials
#> 2: Delete these credentials
#> 3: See the password / token

Selection: 3
#> Current password: usethis::git_sitrep()
#>
#>
#> -> What would you like to do?
#>
#> 1: Keep these credentials
#> 2: Delete these credentials
#> 3: See the password / token

gitcreds_set / gitcreds_get does not persist between sessions

Every time I start a new R session and want to push/pull to github, I have to use gitcreds_set(). It will work, but then it doesn't work the next time I start an R session.

─ Session info ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
setting value
version R version 4.1.3 (2022-03-10)
os Ubuntu 20.04.4 LTS
system x86_64, linux-gnu
ui RStudio
language (EN)
collate C.UTF-8
ctype C.UTF-8
tz America/Los_Angeles
date 2022-04-01
rstudio 2021.09.2+382 Ghost Orchid (desktop)
pandoc 2.5 @ /usr/bin/pandoc

─ Packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.2)
backports 1.4.1 2021-12-13 [1] CRAN (R 4.1.2)
base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.1.2)
bitops 1.0-7 2021-04-24 [1] CRAN (R 4.1.2)
brio 1.1.3 2021-11-30 [1] CRAN (R 4.1.2)
broom 0.7.12 2022-01-28 [1] CRAN (R 4.1.2)
cachem 1.0.6 2021-08-19 [1] CRAN (R 4.1.2)
callr 3.7.0 2021-04-20 [1] CRAN (R 4.0.3)
cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.1.2)
cli 3.2.0 2022-02-14 [1] CRAN (R 4.1.2)
colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.1.2)
crayon 1.5.0 2022-02-14 [1] CRAN (R 4.1.2)
curl 4.3.2 2021-06-23 [1] CRAN (R 4.1.2)
DBI 1.1.2 2021-12-20 [1] CRAN (R 4.1.2)
dbplyr 2.1.1 2021-04-06 [1] CRAN (R 4.0.3)
desc 1.4.0 2021-09-28 [1] CRAN (R 4.1.2)
devtools * 2.4.3 2021-11-30 [1] CRAN (R 4.1.2)
digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.2)
dplyr * 1.0.8 2022-02-08 [1] CRAN (R 4.1.2)
DT 0.21 2022-02-26 [1] CRAN (R 4.1.2)
ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.0.3)
fansi 1.0.2 2022-01-14 [1] CRAN (R 4.1.2)
fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.2)
forcats * 0.5.1 2021-01-27 [1] CRAN (R 4.1.2)
fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.2)
gargle 1.2.0 2021-07-02 [1] CRAN (R 4.0.3)
generics 0.1.2 2022-01-31 [1] CRAN (R 4.1.2)
ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.2)
gitcreds 0.1.1 2020-12-04 [1] CRAN (R 4.1.2)
glue * 1.6.2 2022-02-24 [1] CRAN (R 4.1.2)
googleAuthR 2.0.0 2022-01-28 [1] CRAN (R 4.1.2)
gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.2)
haven 2.4.3 2021-08-04 [1] CRAN (R 4.1.2)
hms 1.1.1 2021-09-26 [1] CRAN (R 4.1.2)
htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.2)
htmlwidgets 1.5.4 2021-09-08 [1] CRAN (R 4.1.2)
httr * 1.4.2 2020-07-20 [1] CRAN (R 4.1.2)
integral * 0.0.2.9004 2022-03-23 [1] Github (IntegralEnvision/integral@ad51da5)
janitor * 2.1.0 2021-01-05 [1] CRAN (R 4.0.3)
jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.1.2)
lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.0.3)
lubridate * 1.8.0 2021-10-07 [1] CRAN (R 4.0.3)
magrittr 2.0.2 2022-01-26 [1] CRAN (R 4.1.2)
memoise 2.0.1 2021-11-26 [1] CRAN (R 4.1.2)
modelr 0.1.8 2020-05-19 [1] CRAN (R 4.1.2)
munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.2)
pillar 1.7.0 2022-02-01 [1] CRAN (R 4.1.2)
pkgbuild 1.3.1 2021-12-20 [1] CRAN (R 4.1.2)
pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.2)
pkgload 1.2.4 2021-11-30 [1] CRAN (R 4.1.2)
prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.1.2)
processx 3.5.2 2021-04-30 [1] CRAN (R 4.0.3)
ps 1.6.0 2021-02-28 [1] CRAN (R 4.1.2)
purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.1.2)
R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.2)
Rcpp 1.0.8 2022-01-13 [1] CRAN (R 4.1.2)
RCurl * 1.98-1.6 2022-02-08 [1] CRAN (R 4.1.2)
readr * 2.1.2 2022-01-30 [1] CRAN (R 4.1.2)
readxl 1.3.1 2019-03-13 [1] CRAN (R 4.1.2)
remotes 2.4.2 2021-11-30 [1] CRAN (R 4.1.2)
reprex 2.0.1 2021-08-05 [1] CRAN (R 4.0.3)
rjson * 0.2.21 2022-01-09 [1] CRAN (R 4.1.2)
rlang 1.0.1 2022-02-03 [1] CRAN (R 4.1.2)
rprojroot 2.0.2 2020-11-15 [1] CRAN (R 4.1.2)
rrapply * 1.2.3 2021-02-08 [1] CRAN (R 4.1.2)
rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.2)
rvest 1.0.2 2021-10-16 [1] CRAN (R 4.0.3)
scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.2)
sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.2)
snakecase 0.11.0 2019-05-25 [1] CRAN (R 4.0.3)
stringi 1.7.6 2021-11-29 [1] CRAN (R 4.1.2)
stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.1.2)
testthat 3.1.2 2022-01-20 [1] CRAN (R 4.1.2)
tibble * 3.1.6 2021-11-07 [1] CRAN (R 4.0.3)
tidyr * 1.2.0 2022-02-01 [1] CRAN (R 4.1.2)
tidyselect 1.1.2 2022-02-21 [1] CRAN (R 4.1.2)
tidyverse * 1.3.1 2021-04-15 [1] CRAN (R 4.1.2)
tzdb 0.2.0 2021-10-27 [1] CRAN (R 4.1.2)
usethis * 2.1.5 2021-12-09 [1] CRAN (R 4.1.2)
utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.2)
vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.0.3)
withr 2.4.3 2021-11-30 [1] CRAN (R 4.1.2)
xml2 1.3.3 2021-11-30 [1] CRAN (R 4.1.2)

[1] /usr/local/lib/R/site-library
[2] /usr/lib/R/site-library
[3] /usr/lib/R/library

──────────────────

`gitcreds_set()` should check to make sure a credential helper is configured

Hi! Fresh-starting a machine and was following the usethis guide, but ran into an issue where I forgot to set the credential helper to something before trying to set credentials via gitcreds::gitcreds_set():

R> gitcreds::gitcreds_list_helpers()
Error in new_git_error("git_error", args = args, stdout = out, status = attr(out,  : 
  System git failed: 
R> gitcreds::gitcreds_set()


? Enter password or token: my_github_token
-> Adding new credentials...
-> Removing credentials from cache...
-> Done.
R> gitcreds::gitcreds_get()
Error in throw(new_error("gitcreds_no_credentials", url = url)) : 
  Could not find any credentials

Should gitcreds_set() check to make sure at least one credential helper is configured before trying to set the credential?

gitcreds() does not seem to store the credentials

🚨This issue is not done yet, just posting to make sure I don't lose it while I try some more stuff to fix it🚨

I am trying to get a Shiny app running again that used to use {git2r} but that I now switched to {gert} (also see this thread on Mastodon).

I currently use the following code (also see here on Codeberg):

    userName <- "PsyConstructor";
    userMail <- "[email protected]";
    gitForge <- "https://gitlab.com";
    repoUrl <- paste0(gitForge, "/psy-ops/psycore-one.git");

    ###-------------------------------------------------------------------------
    ### Clone git repo
    ###-------------------------------------------------------------------------
    
    tempPath <- tempfile(pattern = "gertTempRepo-");
    
    tempRepo <- gert::git_clone(
      repoUrl,
      path = tempPath
    );
    
    ### What can I say, I saw this in some examples and I'm desperate
    setwd(tempPath);
    
    # Sys.getenv("PSYCORE_PSYCONSTRUCTOR_GITLAB_PAT");
    # Sys.setenv(PSYCORE_PSYCONSTRUCTOR_GITLAB_PAT = "token");
    
    system(paste0("git config --global credential.helper cache"));
    system(paste0("git config --global credential.username ", userName));
    system(paste0("git config --local user.name ", userName));
    system(paste0("git config --local user.email ", userMail));
    gitcreds::gitcreds_approve(list(
      url = "https://gitlab.com",
      username = userName,
      password = Sys.getenv("PSYCORE_PSYCONSTRUCTOR_GITLAB_PAT")
    ))
    
    ###-------------------------------------------------------------------------
    ### Write, stage, and commit file, and push to repo
    ###-------------------------------------------------------------------------

    ### {gert} wants the path relative to the repo root
    tempFile <-
      file.path("data", "dctspecs", paste0(ucid(), ".dct.yaml"));

    psyverse::save_to_yaml(
      dctSpec(),
      file = file.path(tempPath, tempFile)
    );

    gert::git_add(
      files = tempFile,
      repo = tempRepo
    );

    gert::git_commit(
      paste("Submission of DCT specification with UCID ", ucid()),
      repo = tempRepo
    );
    
    gert::git_push(
      repo = tempRepo
    );

I get the following pop-up asking for my password:

image

Once I provide it, the git push works as expected. If I then stage/commits/push again, locally I do not get another pop-up, and the push proceeds succesfully. However, I still see:

Looking up https credentials for https://gitlab.com/psy-ops/psycore-one.git
info: detecting host provider for 'https://gitlab.com/'...
info: detecting host provider for 'https://gitlab.com/'...
fatal: credential-cache unavailable; no unix socket support
Transferred 4 of 4 objects...done!
[status] refs/heads/master: unchanged
[updated] b9be2bbaef..9a9fec3d3c refs/remotes/origin/master

Maybe the 'regular' Git credential manager takes over (suggested by both the pop-up and that the cache one seems unavailable yet the password was stored and re-used)?

Weird PATH stuff with gitcreds

I started this as an issue on happygitwithr, but Jenny suggested it would be a better issue here. git is installed on a machine (well really, a bunch of machines-- these are managed by ITS at my academic institution) in a somewhat non-standard way. Sadly, this non-standard way seems to be making it impossible to use gitcreds.

I can

  • find the git.exe file
  • set the path to that executable in my RStudio global options
  • see git documentation if I type git in my RStudio terminal
  • see "version control" as an option when I am making a new RStudio project

Unfortunately, I cannot

  • use things in gitcreds, such as
gitcreds::gitcreds_set()
Error in throw(new_error("gitcreds_nogit_error")) : 
  Could not find system git

Any other suggestions for getting gitcreds to recognize my system git, or am I stuck trying to convince my IT folks to install git differently?

Is there a way to get gitcreds working on RStudio server (linux)?

Hi,

I'm having a bit of a problem with gitcreds on an RStudio Server 1.4.1103 that I'm using for teaching a class this week. I'm able to create a GitHub token and store it with gitcreds.

> usethis::create_github_token()
● Call `gitcreds::gitcreds_set()` to register this token in the local Git credential store
  It is also a great idea to store this token in any password-management software that you useOpening URL 'https://github.com/settings/tokens/new?scopes=repo,user,gist,workflow&description=R:GITHUB_PAT'
> gitcreds::gitcreds_set()


? Enter password or token: a358aac525e5b498editedETCETERA
-> Adding new credentials...
-> Removing credetials from cache...
-> Done.
> gitcreds::gitcreds_get("https://github.com")
<gitcreds>
  protocol: https
  host    : github.com
  username: PersonalAccessToken
  password: <-- hidden -->
> gitcreds::gitcreds_get("https://github.com")$password
[1] "a358aac525e5b498editedETCETERA"

I can change R projects, restart R, and well, I get the same info back from gitcreds::gitcreds_get("https://github.com").

> gitcreds::gitcreds_get("https://github.com")
<gitcreds>
  protocol: https
  host    : github.com
  username: PersonalAccessToken
  password: <-- hidden -->
> gitcreds::gitcreds_cache_envvar("https://github.com")
[1] "GITHUB_PAT_GITHUB_COM"
> gitcreds::gitcreds_list_helpers()
[1] "cache"
> gitcreds::gitcreds_list()
Error in gitcreds$throw(gitcreds$new_error("gitcreds_unknown_helper",  : 
  Unknown credential helper: `cache`, cannot list credentials

However, at some point, I'm no longer able to retrieve the information. It's almost like wherever the token is cached gets deleted. Is there a way to find what would the cache location be? I haven't been able to figure out what triggers the break in behavior. I mean, after gitcreds::gitcreds_set() I really only use commands like usethis::use_github() and none of the gitcreds commands.

> Sys.getenv("GITHUB_PAT_GITHUB_COM")
[1] "protocol:https:host:github.com:username:PersonalAccessToken:password:a358aac525e5b498editedETCETERA"

After reading https://gitcreds.r-lib.org/reference/gitcreds_get.html#arguments (Credential helpers section) I see that but most Linux distributions do not set up a default credential helper. which makes me guess that at some point the environment variable GITHUB_PAT_GITHUB_COM gets un-set. For now I told students to use usethis::edit_r_environ() although I'm aware that this is no longer the suggested setup. Though hmm, maybe I'm missing something else that could be done to get gitcreds working on this linux environment.

(at some point this happens:)

> gitcreds::gitcreds_get("https://github.com")
Error in throw(new_error("gitcreds_no_credentials", url = url)) : 
  Could not find any credentials
> traceback()
4: stop(cond)
3: throw(new_error("gitcreds_no_credentials", url = url))
2: gitcreds_parse_output(out, url)
1: gitcreds::gitcreds_get("https://github.com")

Let me know if there's any other information I can provide that would be useful.

Best,
Leo

Related to lcolladotor/rnaseq_LCG-UNAM_2021@e15bb0f.

> options(width = 120)
> sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.0.3 (2020-10-10)
 os       Ubuntu 20.04.1 LTS          
 system   x86_64, linux-gnu           
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/Mexico_City         
 date     2021-02-23Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────── 
 package     * version    date       lib source                             
 assertthat    0.2.1      2019-03-21 [2] CRAN (R 4.0.3)                     
 bookdown      0.21       2020-10-13 [2] CRAN (R 4.0.3)                     
 cli           2.3.0      2021-01-31 [2] CRAN (R 4.0.3)                     
 data.table    1.14.0     2021-02-21 [2] CRAN (R 4.0.3)                     
 digest        0.6.27     2020-10-24 [2] CRAN (R 4.0.3)                     
 ellipsis      0.3.1      2020-05-15 [2] CRAN (R 4.0.3)                     
 evaluate      0.14       2019-05-28 [2] CRAN (R 4.0.3)                     
 generics      0.1.0      2020-10-31 [2] CRAN (R 4.0.3)                     
 gitcreds      0.1.1      2020-12-04 [2] CRAN (R 4.0.3)                     
 glue          1.4.2      2020-08-27 [2] CRAN (R 4.0.3)                     
 hms           1.0.0      2021-01-13 [1] CRAN (R 4.0.3)                     
 htmltools     0.5.1.1    2021-01-22 [2] CRAN (R 4.0.3)                     
 knitr         1.31       2021-01-27 [2] CRAN (R 4.0.3)                     
 lifecycle     0.2.0      2020-03-06 [2] CRAN (R 4.0.3)                     
 lubridate     1.7.9.2    2020-11-13 [1] CRAN (R 4.0.3)                     
 magrittr      2.0.1      2020-11-17 [2] CRAN (R 4.0.3)                     
 pkgconfig     2.0.3      2019-09-22 [2] CRAN (R 4.0.3)                     
 Rcpp          1.0.6      2021-01-15 [2] CRAN (R 4.0.3)                     
 rlang         0.4.10     2020-12-30 [2] CRAN (R 4.0.3)                     
 rmarkdown     2.7        2021-02-19 [2] CRAN (R 4.0.3)                     
 rsthemes      0.2.1.9000 2021-02-23 [1] Github (gadenbuie/rsthemes@521572b)
 rstudioapi    0.13       2020-11-12 [2] CRAN (R 4.0.3)                     
 sessioninfo   1.1.1      2018-11-05 [2] CRAN (R 4.0.3)                     
 suncalc       0.5.0      2019-04-03 [1] CRAN (R 4.0.3)                     
 vctrs         0.3.6      2020-12-17 [2] CRAN (R 4.0.3)                     
 withr         2.4.1      2021-01-26 [2] CRAN (R 4.0.3)                     
 xfun          0.20       2021-01-06 [2] CRAN (R 4.0.3)                     
 yaml          2.2.1      2020-02-01 [2] CRAN (R 4.0.3)                     

[1] /home/compu2/R/x86_64-pc-linux-gnu-library/4.0
[2] /usr/local/lib/R/site-library
[3] /usr/lib/R/site-library
[4] /usr/lib/R/library
> system("git --version")
git version 2.25.1
RStudio Server
Version 1.4.1103
© 2009-2021 RStudio, PBC
"Wax Begonia" (458706c3, 2021-01-06) for Ubuntu Bionic
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36

Set token non-interactively

I am installing packages from GitHub in a docker container and running into rate limiting. How does one set token non-interactively from an environment variable?

I am thinking of something like this..

gitcreds_set(url = "https://github.com", token = Sys.getenv("TOKEN"))

Perhaps, I am missing something.

gitcreds can inadvertently use oauth token deposited by github's gh cli

I think GitHub's gh CLI may deposit its (oauth!) token in such a way that it may be (inadvertently?) used by gitcreds:

gh auth refresh
Rscript -e "gitcreds::gitcreds_get()"
<gitcreds>
  protocol: https
  host    : github.com
  username: maxheld83
  password: <-- hidden -->
gh::gh_whoami()
#> {
#>   "name": "Max Held",
#>   "login": "maxheld83",
#>   "html_url": "https://github.com/maxheld83",
#>   "scopes": "gist, read:org, repo, workflow",
#>   "token": "gho_...NEf6"
#> }

as per the token prefix, this is an oauth access token, not a PAT.

This has happened to me as I first installed and played around with GitHub's gh (!= r-lib/gh), and then tried to deposit another PAT into gitcreds::gitcreds_set().

Because the two are username-keyed (maxheld83@https://github.com in this case), it appears that setting one (oauth token from gh, or PAT from github ui) will always replace the other.

I'm not sure this is a problem per se, just thought it was a bit odd/unexpected.
Coming from a PAT-world, perhaps it is possible that this can lead to unexpectedly elevated scopes under some scenarios.

(A related problem with more discussion arises in credentials, which doesn't expect username-keyed tokens r-lib/credentials#16)

gitcreds_fill() inconsistent

gitcreds::gitcreds_fill(list(url="https://impossible.com"))

[1] "protocol=dummy" "host=dummy" "username=dummy" "password=dummy get"

gitcreds::gitcreds_fill(list(url="https://impossible.com"))

Error in new_git_error("git_error", args = args, stdout = out, status = attr(out, : System git failed:

gitcreds::gitcreds_fill(list(url="https://impossible.com"))

[1] "protocol=dummy" "host=dummy" "username=dummy" "password=dummy get"

gitcreds::gitcreds_fill(list(url="https://impossible.com"))

Error in new_git_error("git_error", args = args, stdout = out, status = attr(out, : System git failed:

I have traced the problem back to inconsistent results coming from the system2() command in git_run(). Some times it's blank and sometimes its the protocol=dummy:

$ 'git' -c credential.helper="! echo protocol=dummy;echo host=dummy;echo username=dummy;echo password=dummy" credential fill 2> '/tmp/RtmpdAswNP/gitcreds-stderr-52cf16a5050a' < '/tmp/RtmpdAswNP/gitcreds-stdin-52cfb48c91f'
$ 'git' -c credential.helper="! echo protocol=dummy;echo host=dummy;echo username=dummy;echo password=dummy" credential fill 2> '/tmp/RtmpdAswNP/gitcreds-stderr-52cf16a5050a' < '/tmp/RtmpdAswNP/gitcreds-stdin-52cfb48c91f'
host=dummy
username=dummy
password=dummy get
$ 'git' -c credential.helper="! echo protocol=dummy;echo host=dummy;echo username=dummy;echo password=dummy" credential fill 2> '/tmp/RtmpdAswNP/gitcreds-stderr-52cf16a5050a' < '/tmp/RtmpdAswNP/gitcreds-stdin-52cfb48c91f'
host=dummy
username=dummy
password=dummy get
$ 'git' -c credential.helper="! echo protocol=dummy;echo host=dummy;echo username=dummy;echo password=dummy" credential fill 2> '/tmp/RtmpdAswNP/gitcreds-stderr-52cf16a5050a' < '/tmp/RtmpdAswNP/gitcreds-stdin-52cfb48c91f'

Any ideas of what might be causing this?

Thanks,

Tarj.

Release 0.1.1

Prepare for release:

  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • revdepcheck::revdep_check(num_workers = 4)
  • Polish NEWS
  • If new failures, update email.yml then revdepcheck::revdep_email_maintainers()

Perform release:

  • Bump version (in DESCRIPTION and NEWS)
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Tag release
  • Bump dev version
  • Tweet

Template from r-lib/usethis#338

gitcreds_set() repeats forever and unable to set credentials

I recently updated my PAT due to previously using an outdated format (had put off update for a bit).

I'm now having trouble connecting to github. When I do gitcreds_set() it shows everything as "dummy" values and selecting "2: Replace these credentials" is unable to progress and just keeps asking me the same thing:

> gitcreds::gitcreds_set()

-> Your current credentials for 'https://github.com':

  Logon failed, use ctrl+c to cancel basic credential prompt.: Logon failed, use ctrl+c to cancel basic credential prompt.
  protocol                                                   : dummy
  host                                                       : dummy
  username                                                   : dummy
  password                                                   : <-- hidden -->

-> What would you like to do? 

1: Keep these credentials
2: Replace these credentials
3: See the password / token

Selection: 2

-> Removing current credentials...

!! Found more matching credentials!

-> Your current credentials for 'https://github.com':

  Logon failed, use ctrl+c to cancel basic credential prompt.: Logon failed, use ctrl+c to cancel basic credential prompt.
  protocol                                                   : dummy
  host                                                       : dummy
  username                                                   : dummy
  password                                                   : <-- hidden -->

-> What would you like to do? 

1: Keep these credentials
2: Replace these credentials
3: See the password / token

Selection: 2

-> Removing current credentials...

!! Found more matching credentials!

-> Your current credentials for 'https://github.com':

  Logon failed, use ctrl+c to cancel basic credential prompt.: Logon failed, use ctrl+c to cancel basic credential prompt.
  protocol                                                   : dummy
  host                                                       : dummy
  username                                                   : dummy
  password                                                   : <-- hidden -->

-> What would you like to do? 

1: Keep these credentials
2: Replace these credentials
3: See the password / token

and so on...

> sessioninfo::session_info()
- Session info -----------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.5.1 (2018-07-02)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_United States.1252  
 ctype    English_United States.1252  
 tz       America/Los_Angeles         
 date     2022-02-08                  

- Packages ---------------------------------------------------------------------------------------------------------------------------------------
 package     * version date       lib source        
 cli           3.1.0   2021-10-27 [1] CRAN (R 3.5.1)
 gitcreds      0.1.1   2020-12-04 [1] CRAN (R 3.5.1)
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.5.2)
 withr         2.4.3   2021-11-30 [1] CRAN (R 3.5.1)
 yaml          2.2.1   2020-02-01 [1] CRAN (R 3.5.3)

[1] C:/Users/BSHALLOW/Documents/R/win-library/3.5
[2] C:/Program Files/R/R-3.5.1/library

Release gitcreds 0.1.2

Prepare for release:

  • git pull
  • Check current CRAN check results
  • Polish NEWS
  • devtools::build_readme()
  • urlchecker::url_check()
  • devtools::check(remote = TRUE, manual = TRUE)
  • devtools::check_win_devel()
  • rhub::check_for_cran()
  • revdepcheck::revdep_check(num_workers = 8)
  • Update cran-comments.md
  • git push

Submit to CRAN:

  • usethis::use_version('patch')
  • devtools::submit_cran()
  • Approve email

Wait for CRAN...

  • Accepted 🎉
  • git push
  • usethis::use_github_release()
  • usethis::use_dev_version()
  • git push

Provide device-authentication workflow for GitHub?

Apologies if this is already supported here or elsewhere! Please let me know! I think it would be great if gitcreds could provide the device authentication workflow for GitHub, such as we see in the Python package https://github.com/jupyterhub/gh-scoped-creds (and also built into MS's Code Server I believe), in which a user requests a short-lived (8hr) scoped credential authenticated with a OTP.

This provides a much simpler workflow for users creating tokens than learning to do so manually in the GitHub web interface. It also promotes the use of short-lived tokens, consistent with the best-practices we see in other systems, especially with the rise of hosted or cloud-based compute platforms.

you know I'm no security expert, but it seems that current practices in our R community are still built around more long-lived and widely scoped tokens, while elsewhere everything is migrating towards more short-lived and narrowly scoped ones. (I recognize that this request is more GitHub specific than being generally about git, just wasn't sure where best to raise it).

gitcreds::gitcreds_set doesn't store credentials

This is a strange effect and I'm not sure if this is the right place to report it, maybe it is better on renv:

When opening a project A (in rstudio) coming from a project B that uses renv and use gitcreds::gitcreds_set() to store credentials on project A, the credentials are not stored and are missing the next time I open project A.

My solution, open project A directly and then use gitcreds::gitcreds_set()

gitcreds should check git version

I was trying to follow along with the Github credentials guide on usethis and when I got to the point of running gitcreds::gitcreds_set() I got the following error:

> gitcreds::gitcreds_set()
Error in throw(new_error("git_error", args = args, stdout = out, status = attr(out,  : 
  System git failed

In this functions, there's a tryCatch() intending to grab gitcreds_no_credentials and return NULL, however the error being thrown here is not a gitcreds_no_credentials condition and so the error was seeping through. The code goes on to run gitcreds_set_new(url) if the condition is caught and I was getting the same error response.

It took me way too long to realise my git version was out of date and after updating the error went away.

It would be useful for gitcreds to check a user's git version at some point in the process because the error I was receiving was very uninformative.

Notes from early usage

I'm going to plumb this into usethis experimentally and will take a few notes here as I go.

If this is going to be a "drop in" file (I think that's what #3 is about), some of the function names are problematic:

  • is_interactive() masks rlang::is_interactive(), which usethis imports (in its entirety, i.e. the whole rlang namespace)
  • %||% masks rlang's version
  • is_string() masks an internal helper in usethis (that also has a slightly different signature)

Two other functions that seem ripe for collision (but that aren't currently a problem for me, that I know of):

  • parse_url(url)
  • re_match(text, pattern, perl = TRUE, ...)

Error in `gitcreds_list()`

gitcreds::gitcreds_list()
#> Error in switch(credential_helper, osxkeychain = gitcreds_list_osxkeychain(url,  :
#>   EXPR must be a length 1 vector
#> In addition: Warning message:
#> In throw(new_warning("gitcreds_multiple_helpers")) :
#>   Multiple credential helpers, only using the first

Selecting 1 (Keep these credentials) in `gitcreds_set()` throws an error

@DavisVaughan confirmed as well. Everything works fine, and the PAT is set, but when selecting keep these credentials, an error is thrown (where I don't think there is one).

# -> What would you like to do? 

# 1: Keep these credentials
# 2: Replace these credentials
# 3: See the password / token

# Selection: 1
# Error in throw(new_error("gitcreds_abort_replace_error")) : 
#  User aborted updating credentials

Backtrace

# <error/rlang_error>
# User aborted updating credentials
# Backtrace:
#  1. gitcreds::gitcreds_set()
#  2. gitcreds:::gitcreds_set_replace(url, current)
#  3. gitcreds:::throw(new_error("gitcreds_abort_replace_error"))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.