|
1 | 1 | #' Authorize with GitHub. |
2 | 2 | #' |
3 | | -#' This function is run automatically to allow gistr to access your GitHub |
| 3 | +#' This function is run automatically to allow gistr to access your GitHub |
4 | 4 | #' account. |
5 | 5 | #' |
6 | 6 | #' There are two ways to authorise gistr to work with your GitHub account: |
7 | | -#' |
| 7 | +#' |
8 | 8 | #' - Generate a personal access token with the gist scope selected, and set it |
9 | 9 | #' as the `GITHUB_PAT` environment variable per session using `Sys.setenv` |
10 | 10 | #' or across sessions by adding it to your `.Renviron` file or similar. |
|
16 | 16 | #' Using `GITHUB_PAT` is recommended. |
17 | 17 | #' |
18 | 18 | #' @export |
19 | | -#' @param app An [httr::oauth_app()] for GitHub. The default uses an |
| 19 | +#' @param app An [httr::oauth_app()] for GitHub. The default uses an |
20 | 20 | #' application `gistr_oauth` created by Scott Chamberlain. |
21 | 21 | #' @param reauth (logical) Force re-authorization? |
22 | 22 | #' @return a named list, with a single slot for `Authorization`, with a single |
|
27 | 27 | #' } |
28 | 28 |
|
29 | 29 | gist_auth <- function(app = gistr_app, reauth = FALSE) { |
30 | | - |
| 30 | + |
| 31 | + #if there is a token cached, use that |
31 | 32 | if (exists("auth_config", envir = cache) && !reauth) { |
32 | | - return(auth_header(cache$auth_config$auth_token$credentials$access_token)) |
| 33 | + return(auth_header(cache$auth_config$auth_token)) |
33 | 34 | } |
34 | | - pat <- Sys.getenv("GITHUB_PAT", "") |
35 | | - if (!identical(pat, "")) { |
36 | | - auth_config <- list(auth_token=list(credentials=list(access_token=pat))) |
37 | | - } else if (!interactive()) { |
| 35 | + #if nothing cached, use gitcreds. gitcreds will retrieve PAT stored as an |
| 36 | + #GITHUB_PAT environment variable or one set with gitcreds::gitcreds_set() |
| 37 | + #TODO use tryCatch here to customize error message to be more helpful |
| 38 | + # cli::cli_alert_warning("Please set {.field GITHUB_PAT} in your {.file .Renviron} or use the {.pkg gitcreds} |
| 39 | + creds <- try(gitcreds::gitcreds_get()) |
| 40 | + if (!inherits(creds, "try-error")) { |
| 41 | + token <- creds$password |
| 42 | + } else if (interactive()) { #if no gitcreds, try interactive authentication |
| 43 | + # try oauth direct |
| 44 | + endpt <- httr::oauth_endpoints("github") |
| 45 | + auth <- httr::oauth2.0_token(endpt, app, scope = "gist", cache = !reauth) |
| 46 | + token <- auth$credentials$access_token |
| 47 | + } else { |
38 | 48 | stop("In non-interactive environments, please set GITHUB_PAT env to a GitHub", |
39 | 49 | " access token (https://help.github.com/articles/creating-an-access-token-for-command-line-use)", |
40 | 50 | call. = FALSE) |
41 | | - } else { |
42 | | - endpt <- httr::oauth_endpoints("github") |
43 | | - token <- httr::oauth2.0_token(endpt, app, scope = "gist", cache = !reauth) |
44 | | - auth_config <- httr::config(token = token) |
45 | 51 | } |
| 52 | + |
| 53 | + #cache auth config |
| 54 | + auth_config <- httr::config(token = token) |
46 | 55 | cache$auth_config <- auth_config |
47 | | - auth_header(auth_config$auth_token$credentials$access_token) |
| 56 | + return(auth_header(auth_config$auth_token)) |
48 | 57 | } |
49 | 58 |
|
50 | 59 | auth_header <- function(x) list(Authorization = paste0("token ", x)) |
|
0 commit comments