1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
## git2r, R bindings to the libgit2 library.
## Copyright (C) 2013 - 2022 The git2r contributors
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License, version 2,
## as published by the Free Software Foundation.
##
## git2r is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##' Create a new environmental credential object
##'
##' Environmental variables can be written to the file
##' \code{.Renviron}. This file is read by \emph{R} during startup,
##' see \code{\link[base]{Startup}}.
##' @family git credential functions
##' @param username The name of the environmental variable that holds
##' the username for the authentication.
##' @param password The name of the environmental variable that holds
##' the password for the authentication.
##' @return A list of class \code{cred_env} with entries:
##' \describe{
##' \item{username}{
##' The name of the environmental variable that holds
##' the username for the authentication.
##' }
##' \item{password}{
##' The name of the environmental variable that holds
##' the password for the authentication.
##' }
##' }
##' @export
##' @examples
##' \dontrun{
##' ## Create an environmental credential object for the username and
##' ## password.
##' cred <- cred_env("NAME_OF_ENV_VARIABLE_WITH_USERNAME",
##' "NAME_OF_ENV_VARIABLE_WITH_PASSWORD")
##' repo <- repository("git2r")
##' push(repo, credentials = cred)
##' }
cred_env <- function(username = NULL, password = NULL) {
structure(list(username = username, password = password),
class = "cred_env")
}
##' Create a new personal access token credential object
##'
##' The personal access token is stored in an envrionmental variable.
##' Environmental variables can be written to the file
##' \code{.Renviron}. This file is read by \emph{R} during startup,
##' see \code{\link[base]{Startup}}. On GitHub, personal access tokens
##' function like ordinary OAuth access tokens. They can be used
##' instead of a password for Git over HTTPS, see the \dQuote{Creating
##' a personal access token} article on GitHub Docs.
##' @family git credential functions
##' @param token The name of the environmental variable that holds the
##' personal access token for the authentication. Default is
##' \code{GITHUB_PAT}.
##' @return A list of class \code{cred_token} with entry:
##' \describe{
##' \item{token}{
##' The name of the environmental variable that holds
##' the personal access token for the authentication.
##' }
##' }
##' @export
##' @examples
##' \dontrun{
##' ## Create a personal access token credential object.
##' ## This example assumes that the token is stored in
##' ## the 'GITHUB_PAT' environmental variable.
##' repo <- repository("git2r")
##' cred <- cred_token()
##' push(repo, credentials = cred)
##' }
cred_token <- function(token = "GITHUB_PAT") {
structure(list(token = token), class = "cred_token")
}
##' Create a new plain-text username and password credential object
##'
##' @family git credential functions
##' @param username The username of the credential
##' @param password The password of the credential. If getPass is installed
##' and the only input is username, \code{getPass::getPass()} will be
##' called to allow for interactive and obfuscated interactive
##' input of the password.
##' @return A list of class \code{cred_user_pass} with entries:
##' \describe{
##' \item{username}{
##' The username of the credential
##' }
##' \item{password}{
##' The password of the credential
##' }
##' }
##' @export
##' @examples
##' \dontrun{
##' ## Create a plain-text username and password credential object
##' cred_user_pass("Random Developer", "SecretPassword")
##' }
cred_user_pass <- function(username = NULL, password = NULL) {
if (!is.null(username)) {
if (is.null(password)) {
if (requireNamespace("getPass", quietly = TRUE)) {
password <- getPass::getPass()
}
}
}
structure(list(username = username, password = password),
class = "cred_user_pass")
}
##' Create a new passphrase-protected ssh key credential object
##'
##' @family git credential functions
##' @param publickey The path to the public key of the
##' credential. Default is \code{ssh_path("id_rsa.pub")}
##' @param privatekey The path to the private key of the
##' credential. Default is \code{ssh_path("id_rsa")}
##' @param passphrase The passphrase of the credential. Default is
##' \code{character(0)}. If getPass is installed and private key
##' is passphrase protected \code{getPass::getPass()} will be
##' called to allow for interactive and obfuscated interactive
##' input of the passphrase.
##' @return A list of class \code{cred_ssh_key} with entries:
##' \describe{
##' \item{publickey}{
##' The path to the public key of the credential
##' }
##' \item{privatekey}{
##' The path to the private key of the credential
##' }
##' \item{passphrase}{
##' The passphrase of the credential
##' }
##' }
##' @export
##' @examples
##' \dontrun{
##' ## Create a ssh key credential object. It can optionally be
##' ## passphrase-protected
##' cred <- cred_ssh_key(ssh_path("id_rsa.pub"), ssh_path("id_rsa"))
##' repo <- repository("git2r")
##' push(repo, credentials = cred)
##' }
cred_ssh_key <- function(publickey = ssh_path("id_rsa.pub"),
privatekey = ssh_path("id_rsa"),
passphrase = character(0)) {
publickey <- normalizePath(publickey, mustWork = TRUE)
privatekey <- normalizePath(privatekey, mustWork = TRUE)
if (length(passphrase) == 0) {
if (ssh_key_needs_passphrase(privatekey)) {
if (requireNamespace("getPass", quietly = TRUE)) {
passphrase <- getPass::getPass()
}
}
}
structure(list(publickey = publickey,
privatekey = privatekey,
passphrase = passphrase),
class = "cred_ssh_key")
}
##' Check if private key is passphrase protected
##' @param privatekey The path to the private key of the
##' credential. Default is \code{ssh_path("id_rsa")}
##' @noRd
ssh_key_needs_passphrase <- function(privatekey = ssh_path("id_rsa")) {
private_content <- readLines(privatekey, n = 3)
contains_encrypted <- grepl("encrypted", private_content, ignore.case = TRUE)
any(contains_encrypted)
}
##' Compose usual path to ssh keys
##'
##' This function provides a consistent means across OS-types to access the
##' \code{.ssh} directory.
##'
##' On Windows-based systems,
##' \code{path.expand("~")} returns \code{"C:/Users/username/Documents"},
##' whereas the usual path to the \code{.ssh} directory is
##' \code{"C:/Users/username"}.
##'
##' On other operating systems, \code{path.expand("~")} returns the usual path
##' to the \code{.ssh} directory.
##'
##' Calling \code{ssh_path()} with no arguments will return the usual path to
##' the \code{.ssh} directory.
##'
##' @param file basename of file for which path is requested
##' @return Full path to the file
##' @export
##' @examples
##' ssh_path()
##' ssh_path("is_rsa.pub")
ssh_path <- function(file = "") {
file.path(home_dir(), ".ssh", file)
}
# Return the user's home directory regardless of operating system
home_dir <- function() {
if (.Platform$OS.type == "windows") {
home <- Sys.getenv("USERPROFILE")
} else {
home <- path.expand("~")
}
return(home)
}
|